chevron_left

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 the above error for the past couple of days.
Can someone guide me in fixing the above error?

Solution :

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

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

I have written the 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 the above changes in your code.
The above code will execute properly without any errors.

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. The above code will help to continue your execution without any error.

Thanks

More Posts

A counter used in a For Loop cannot be initialized within the for loop statement. true or false? sakshi - Sep 7
Program does not contain a static 'main' method suitable for an entry point Alecxe01 - Oct 27
How to fix C# Error Class does not contain a constructor that takes 0 arguments mhfaisal - May 5
memperbaiki excel cannot open the file because the file format or file extension is not valid SharadMagar450 - Sep 4
A structure __ contain members of the same data types. Lalit Kumar - Sep 21, 2020
Creating a ruby nested hash with array as inner value Tushar Shuvro - Jul 19, 2020
Cannot be accessed with an instance reference, qualify it with a type name instead Alecxe01 - Nov 12
Importerror: libcudnn.so.6: cannot open shared object file: no such file or directory Alecxe01 - 6 days
system.nullreferenceexception: Object reference not defined as an instance of an object. Farkhanda Athar - Aug 5, 2020
Static methods can be accessed directly in non-static methods Jeevani - May 31