Error accessing C # object

I need to write an address book program in C # 2008. It is assumed that the user must specify the person’s name, email address and selected color (only by color in the listing). Then it is supposed to save contacts for future use.

This is the code causing the error .:

class Contact
{
    string Name; //This string represents the person Name.
    string Email; //This string represents the person Email.

    System.Drawing.KnownColor Favoritecolor
    {
        get;
    }
    static void Request()
    //  This function requests the user to type in information about the person.
    {
        Console.WriteLine("Please enter the person name, e-mail, and favorite color");
        Console.Write; string Name; string Email; ;
        Console.ReadLine();
    }
}

Mistake:

'Lab02.Program.Contact.Favoritecolor': property or indexer must have at least one accessor
+3
source share
3 answers
 System.Drawing.KnownColor Favoritecolor
 {
     get;
     set;
 }

Now you have the FavoriteColor property, but not where it is ever installed, so it will never be able to return the actual value.

If you want to implement the auto property, you need to add a set. Otherwise, create a support field and return it.

 private System.Drawing.KnownColor _favoriteColor = someValue;
 System.Drawing.KnownColor Favoritecolor
 {
     get { return _favoriteColor; }
 }
+10
source

The Favoritecolor property must have both get and set accessor. Like this:

System.Drawing.KnownColor Favoritecolor
        {
            get;
            set;
        }

, - :

class Program
{
    static void Main(string[] args)
    {
        Contact contact = new Contact();

        Console.WriteLine("Please enter the person name:");
        contact.Name = Console.ReadLine();

        Console.WriteLine("Please enter the person e-mail address:");
        contact.Email = Console.ReadLine();

        while (contact.Favoritecolor == 0)
        {
            Console.WriteLine("Please enter the person favorite color:");
            string tempColor = Console.ReadLine();

            try
            {
                contact.Favoritecolor = (System.Drawing.KnownColor)(Enum.Parse(typeof(System.Drawing.KnownColor), tempColor, true));
            }
            catch
            {
                Console.WriteLine("The color \"" + tempColor + "\" was not recognized. The known colors are: ");
                foreach (System.Drawing.KnownColor color in Enum.GetValues(typeof(KnownColor)))
                {
                    Console.WriteLine(color);
                }
            }
        }
    }

    class Contact
    {
        //This string represents the person Name.
        public string Name { get; set; }

        //This string represents the person Email.
        public string Email { get; set; } 

        public System.Drawing.KnownColor Favoritecolor
        {
            get;
            set;
        }
    }
}

, System.Drawing.KnownColor . , .

+2

You might want to put something in your main method, since we will not point to anything and therefore nothing will load when the application starts

0
source

Source: https://habr.com/ru/post/1713188/


All Articles