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

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 1
3,890 points

Related questions

0 votes
1 answer 12 views
0 votes
0 answers 12 views
12 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 Raphael Pacheco 4.9k points
0 votes
1 answer 2 views
2 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 47 minutes ago psandprop 2.4k points
1 vote
1 answer 135 views
135 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 morrisBson 3.2k points