1

I have implemented a class as below:

public class Person
{
    public int d, e, f;
    public Person()
    {
    }

    public Person(int a)
    {
    }

    public Person(int a, int b)
    {
        new Person(40, 6, 8);
    }

    public Person(int a, int b, int c)
    {
        d = a; e = b; f = c;
    }
}   

public  class Program
{
    static void Main(string[] args)
    {
        Person P = new Person(100, 200);

        Console.WriteLine("{0},{1},{2}", P.d, P.e, P.f);// it prints 0,0,0
    }
}

Now if I create the instance of Person class with two arguments I am unable to set the values of d,e,f which is because in the third constructor a new object of Person is declared all together.

So the previous object does not have any idea about this new one.

Is there any way I can get hold of this new object and assign values to d,e,f from there?

John Willemse
  • 6,608
  • 7
  • 31
  • 45
ABCD
  • 249
  • 1
  • 4
  • 17

4 Answers4

7

I think you're actually trying to chain constructors together, so that one constructor passes arguments to another:

public Person(int a, int b) : this(40, 6, 8)
{
}

It's odd that you're ignoring a and b though... normally you'd just default one value, e.g.

public Person(int a, int b) : this(a, b, 8)
{
}

See my article on constructor chaining for more details.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Hi Jon, this is not an inheritance scenario. I i try to do this I get compile time error as "Person.Person cannot call itself". All I want to do is in main when I try to print the values of d,e,f I want it should be printed as 40, 6, 8. Is this possible without creating another reference of person class which has this new object, something like this:public Person x = null; public Person(int a, int b) { x = new Person(40, 6, 8); } Console.WriteLine("{0},{1},{2}", P.x.d, P.x.e, P.x.f); – ABCD May 16 '13 at 14:26
  • @Saurabh: Then you must have done something wrong, because the code I've posted *does* compile. I've just verified it myself. Just changing your existing two-parameter constructor overload to either of the ones I've shown is fine. I never mentioned inheritance. Unfortunately it's hard to tell exactly what you've done wrong... you should get away from the notion of having another `Person` variable in your `Person` class... I'm pretty sure you *don't* want that. You should concentrate on working out what you've done compared with the code I've shown (which really does work). – Jon Skeet May 16 '13 at 14:28
  • @Saurabh: I think I've found out what you've done wrong. I suspect you've added the `: this(40, 6, 8)` to the constructor which takes *3* parameters, rather than the one which takes *two* parameters. Look carefully at the code I've provided. – Jon Skeet May 16 '13 at 14:31
  • Sorry Jon. You are correct. this code works for me. Thanks a lot. – ABCD May 16 '13 at 14:33
3
    public Person()
       : this(0,0,0)
    {
    }
    public Person(int a)
       : this(a,0,0)
    {
    }
    public Person(int a, int b)
       : this(a,b,0)
    {
    }
    public Person(int a, int b, int c)
    {
        d = a; e = b; f = c;
    }
Uzzy
  • 550
  • 3
  • 14
  • No explanation whatsoever? – Jon Skeet May 16 '13 at 14:05
  • Just show what the error is. Author has to overload his contructors. – Uzzy May 16 '13 at 14:15
  • 1
    The author was already overloading the constructors. He wasn't chaining one overload to another though. I find code-only answers are *very* rarely as useful as ones which explain what's going on. – Jon Skeet May 16 '13 at 14:17
1

The default value of an int is 0. Use int? and test if it has a value.

e.g.

var d = P.d.HasValue ? P.d : "";
var e = P.e.HasValue ? P.e : "";
var f = P.f.HasValue ? P.f : "";
Console.WriteLine("{0},{1},{2}", d, e, f);
DaveDev
  • 41,155
  • 72
  • 223
  • 385
1

You can write this

    public Person(int a, int b)
        : this(40, 6, 8)
    {
    }

to call the other constructor.

MBender
  • 5,395
  • 1
  • 42
  • 69