2

C# question: How can I use the constructor:

AcctHolder ah1 = new AcctHolder("Dumitru", "St", "Bucharest");

and be able to obtain ah1.Fname? (instead of null)

using System;

    namespace ConsoleApplication1
    {
        class ATM
        {
            public static void Main(string[] args)
            {
                AcctHolder ah1 = new AcctHolder("Dumitru", "St", "Bucharest");
                Console.WriteLine(ah1.FName); //returns null - why???

                AcctHolder ah2 = new AcctHolder();
                ah2.FName = "Dumi";
                Console.WriteLine(ah2.FName); // returns "Dumi"

                Console.ReadKey();
            }


            public class AcctHolder
            {
                private string fname, lname, city;
                public string FName { get; set; }
                public string LName { get; set; }
                public string City {
                    get { return city; }
                    set { city = value; }

                }
                public AcctHolder(string a, string b, string c)
                {
                    fname = a;
                    lname = b;
                    city = c;
                }
                public AcctHolder()
                {

                }
            }

        }
    }

5 Answers 5

10

returns null - why???

Because you are initializing unrelated fields in the constructor not the backing fields of the properties. You don't need them with auto-implemented properties:

public class AcctHolder
{
    public string FName { get; set; }
    public string LName { get; set; }
    public string City { get; set; }
    public AcctHolder(string a, string b, string c)
    {
        FName = a;
        LName  = b;
        City = c;
    }
    public AcctHolder()
    {

    }
}

If you want to keep the backing fields:

public class AcctHolder
{
    private string fname;
    public string FName 
    {
        get { return fname; }
        set { fname = value; }
    }

    private string lname;
    public string LName 
    {
        get { return lname; }
        set { lname = value; }
    }

    private string city;
    public string City
    {
        get { return city; }
        set { city = value; }
    } 
    public AcctHolder(string a, string b, string c)
    {
        fname = a;
        lname = b;
        city = c;
    }
    public AcctHolder()
    {

    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Though, the constructor still needs to set the public fields, right (bottom example)?
4

You are setting the wrong (private) fname in your constructor. Use the public one.

change

public AcctHolder(string a, string b, string c)
{
    fname = a;
    lname = b;
    city = c;
}

to

public AcctHolder(string a, string b, string c)
{
    Fname = a;
    LName = b;
    City = c;
}

1 Comment

This answer suggests that the private fields fname, lname, city were of any use. Actually they are redundant if you suggest to use the properties. Initializing the private backing fields of (non-auto implemented) properties in the constructor actually is best-practice, because you can be sure that it has no side-effects as it has if a property contains other code.
2

In the constructor, set

FName = a;

instead of

fname = a;

The properties FName and LName don't need the bo backed with fields since you're using get;set;-syntax to declare them. They get that automatically, so you can just remove fname and lname from your code completely.

Comments

2

Since you use autoproperties you dont need the private fields. Simplify:

public class AcctHolder
{
    public string FName { get; set; }
    public string LName { get; set; }
    public string City { get;set;}

    public AcctHolder(string a, string b, string c)
    {
        FName = a;
        LName = b;
        City = c;
    }
    public AcctHolder()
    {

    }
}

Comments

1

Alternatively, change the class to use the private strings as you have with City.

        public class AcctHolder
        {
            private string fname, lname, city;
            public string FName { get {return fname;} set {fname = value; }
            public string LName { get {return lname;} set {lname = value;} }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.