• Register
0 votes
740 views

Problem :

I am beginner to Xamarin. I am trying to learn Xamarin with the help of a tutorial, But now if I try to execute my following code I face below error.

A namespace cannot directly contain members such as fields or methods”

Please find below my code for your reference.
using System.Collections.ObjectModel;
using UIKit;

namespace myplaylist
{
    static void Main(string[] args)
    { 
        var mylistView = new ListView();
        var myFavourites = new ObservableCollection<string>() 
        { 
            "MySong 1",
            "MySong 2",       
        };

        mylistView.ItemsSource = myFavSongs; 

        myFavourites.Add("MySongsr");
    }

I am struggling with above error from past couple of days.

Can someone guide me in fixing above error?

2.3k points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

I found your question interesting so now I am trying to help you in fixing your error.

I guess you are beginner to C#. That’s why you might have forgotten to write Class in your code.

I have written below code for you.

namespace myplaylist
{

  public class MyPlaylistClass {

    static void Main(string[] args)
    { 
        var mylistView = new ListView();
        var myFavourites = new ObservableCollection<string>() 
        { 
            "MySong 1",
            "MySong 2",       
        };

        mylistView.ItemsSource = myFavSongs; 

        myFavourites.Add("MySongsr");
    }

}

I hope you have understood why I have made above changes in your code.
Above code will execute properly without any errors.
5k points
0 votes

To solve this error, first, you must have to know what is are the rules for namespace;

Rules for namespace:

  • A namespace cannot directly contain members such as fields and methods.
  • A namespace can contain many other namespaces, structs, and classes.

Usage of the namespace:

The namespace keyword is used to declare a scope that contains a set of related objects. The namespace is used to organize code elements and to create globally unique data types.

Reason:

The reason behind this is an incomplete script. Your code has missed some closing brackets and class names. You are facing this error due to misplaced or extra closing brackets (}), as well as due to missing class name. Your all code must be within the class name except the property declaration.

Solution:

 Try the following method, it will help to solve your problem.

using System.Collections.ObjectModel;

using UIKit;

namespace myplaylist

{

    Public class MyPlaylistClass {



      static void Main(string[] args)

        {

         var mylistView = new ListView();

         var myFavourites = new ObservableCollection<string>()

         {

          "MySong 1",

          "MySong 2",

};

mylistView.ItemsSource = myFavSongs;

 myFavourites.Add("MySongsr");

 }
}
}

I hope now you understand the error. Above code will help to continue your execution without any error.

3.9k points