• Register
0 votes
620 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?

8 4 2
2,300 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.
5 2 1
4,980 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.

11 5 2
3,890 points

Related questions

0 votes
1 answer 14 views
0 votes
1 answer 12 views
12 views
Problem: I need someone to understand this problem and find out the solution. an interface with no fields or methods is known as a
asked Mar 25 Ifra 24.4k points
0 votes
1 answer 4 views
4 views
Problem: Getting &ldquo;type or namespace name could not be found&rdquo; but everything seems ok?
asked Mar 16 ummesalma 22k points
0 votes
1 answer 12 views
12 views
Problem: Why i am getting this error: Error is > Type or namespace definition, or end-of-file expected
asked Mar 16 ummesalma 22k points
0 votes
1 answer 5 views
5 views
Problem: When I try to compile the code, it returns the following error: The namespace global already contains a definition for Workflow From the Helper.cs file. Does anyone know what this means?
asked Mar 20 ummesalma 22k points
0 votes
1 answer 2 views
2 views
Problem: Do you have any ideas to solve this problem,"two or more methods in a class may have the same name as long as."
asked 20 hours ago Humaira ahmed 7.7k points
0 votes
1 answer 31 views
31 views
Hello Friends, I&rsquo;m in a bit of a pickle... So I&rsquo;m making a game and the code here is supposed to help my player pick up the coins on my map with this trigger response but every time I save it I get this error THE ERROR ... : Character { } void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.CompareTag("CanBePickedUp")) { collision.gameObject.SetActive(false); } }
asked Aug 14, 2020 Chihuahua Man
0 votes
1 answer 331 views
331 views
Problem: I am little bit confused and need help regarding my query? Methods that operate on an object's fields are called:? a. instance variables b. instance methods c. private methods d. public methods
asked Feb 23, 2020 maddi86 5.4k points
0 votes
1 answer 2 views
2 views
Problem: Task going wrong here > does not contain a definition for getawaiter
asked 2 days ago Azlan2020 2.3k points
0 votes
2 answers 758 views
758 views
Problem : I am unable to figure out what is wrong with my code. When I try to compile my code I get the below message: &ldquo;Program does not contain a static 'main' method suitable for an entry point&rdquo;
asked Jan 29, 2020 jwilliam 3.9k points