• Register
0 votes
430 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 12 views
0 votes
1 answer 61 views
61 views
When trying to create a user using claim identity asp.net this error showed while creating claims identity user ApplicationUser user = new ApplicationUser { EmailConfirmed = true, UserName = model.myUser.Email, Email = model.myUser.Email , PhoneNumber = model.myUser.PhoneNumber, ... model.myUser.Email, Email = model.myUser.Email , }; it worked perfectly, so i want to know what is wrong.
asked Oct 30, 2020 psandprop 2.4k points
0 votes
1 answer 15 views
1 vote
1 answer 319 views
319 views
Problem : I am very new to C#. I am trying to execute my code related to windows service but I am unable to execute the code I am facing below error. Cannot start service from the command line or debugger. A winwows Service must first be installed(using installutil. ... looking for expert advice on fixing above error as I have already spent a lot of time in fixing it but with no positive results.
asked Apr 10, 2020 morrisBson 3.2k points
0 votes
1 answer 82 views
82 views
Problem : I have one entity type called product which is generated by the entity framework. I have tried to write below query public IMyQueryable<Product> GetProducts(int myCategoryID) {     return from prod in db.Products     where prod.CategoryID== myCategoryID     select ... one select new Product { Name = p.Name}; it miraculously works correctly. How can I perform the custom select section?
asked Jan 27, 2020 jwilliam 3.9k points
0 votes
0 answers 21 views
21 views
Problem: I have basic knowledge on ASP.Net and Database Connection. Currently I am receiving the below error if I try to run my code: System.Configuration.ConfigurationErrorsException: 'The configuration section cannot contain a CDATA or text element'. My application ... SSPI;Initial Catalog=TestData" providerName="System.Data.SqlClient"/> Kindly help me in fixing above ASP.Net related error ASAP.
asked Jun 22, 2020 Raphael Pacheco 4.9k points
0 votes
2 answers 504 views
504 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
0 votes
1 answer 66 views
66 views
Problem: I am new, I need help, can anyone help me by filling in the blanks? C#, C++, C, and Java use the symbol ____ as the logical OR operator?
asked Feb 19, 2020 maddi86 5.4k points
0 votes
1 answer 29 views
29 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 13 views
13 views
Problem: I was working on creating a registration form and here is my view file <%= form_for @user do |f| %> <fieldset> <legend>Registration</legend> <div class="reg_form"> <label for="first_name">First Name</label> <%= f.text_field :first_name %> </div> <div ... on the server, I get an error The first argument on the form cannot contain nil or be empty on line 1 Any help is appreciated! thanks
asked Dec 1, 2020 sasha 5.3k points