1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Rep Power

    Post Consider calling the default constructor

    I'm learning C# on my own. Here is the code that is in error:

    using System; 
    public struct Person 
    public Person(string name, int age) 
    this.Name = name; 
    this.Age = age; 
    public string Name 
    { get; set; } 
    public int Age 
    { get; set; } 
    public override string ToString() { return String.Format("{0} is {1} years old", Name, Age); } } 
    public class CSharpApp { 
    static void Main() 
    Person p1 = new Person("Beky", 18); 
    Person p2 = p1; 
    p2.Name = "Jane"; 
    p2.Age = 17; 
    Console.WriteLine(p2); Console.WriteLine(p1); 
    The error is:
    "Backing field for automatically implemented property CSharpApp.Person.Age' mus t be fully assigned before control is returned to the caller. Consider calling the default constructor from a constructor initializer."

    -Same for person.age

    The problem is that I really cannot understand it! haha. What I mean to say it, there should be nothing wrong with the get or set methods. hmm. I really can't figure it out. Please tell me how should I get at the correct answer or something. Really appreciated.
    Last edited by raniele3050; February 8th, 2013 at 12:16 AM. Reason: Lack of explanation.

IMN logo majestic logo threadwatch logo seochat tools logo