UV009
UV009

Reputation: 11

How to call different constructors in class using single instance of that class in C#?

Code description

Here, I have created two classes Program in which Main() is present and Customer Class in which I have two Customer class constructors 1. without arguments 2. with arguments. How can i call the two constructors using single instance C1 of Customer class created in Main?

Code

using System;
public class Program
{
    public static void Main()
    {

        Customer C1 = new Customer();
        C1.PrintFullName();
        C1 = new Customer();
        C1.PrintFullName();
    }
}

class Customer
{

    string _firstName;
    string _lastName;

    public Customer() : this("No firstname","No lastname")
    {
    }

    public Customer(string FirstName, string LastName)
    {
        this._firstName = FirstName;
        this._lastName = LastName;
    }

    public void PrintFullName()
    {
        Console.WriteLine("Full Name is {0}", this._firstName+" "+this._lastName);
    }
}

Upvotes: 1

Views: 544

Answers (2)

user7867434
user7867434

Reputation:

You can find more information here:

using System;

public class Program
{
    public static void Main()
    {
        Customer C1 = new Customer("Bob", "Robert", "BobyBob 23, 41423");
        C1.PrintFullName();
        C1 = new Customer("Bob", "Robert");
        C1.PrintFullName();
    }
}

class Customer
{

    string _firstName;
    string _lastName;
    string _address;


    public Customer()
    {
        Console.WriteLine("I'm the last one :)");
        Console.WriteLine("Full Name is {0}", this._firstName+" "+this._lastName);
    }

    public Customer(string FirstName, string LastName) : this()
    {
        Console.WriteLine("I'm gonna call the constructor within the same class that has no parameters.");      
        _firstName = FirstName;
        _lastName = LastName;
    }

    public Customer(string FirstName, string LastName, string address) : this(FirstName, LastName)
    {
        Console.WriteLine("I'm gonna call the constructor within the same class that has 2 parameters that are of type string.");
        _address = _address;
    }

    public void PrintFullName()
    {
        Console.WriteLine("Full Name is {0}", this._firstName+" "+this._lastName);
    }
}

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062492

A class constructor inherently is involved in creating a new object - so there is no inbuilt concept of running a constructor against an existing instance, unless you do some very nasty things that you shouldn't do.

If you want the ability to set all the properties like with a constructor, then perhaps add a method instead:

var obj = new SomeType();
obj.Init();
// ...
obj.Init("foo", "bar", 123);
// ...

Alternatively: just use the existing constructors and just accept that it'll be a different instance with a different reference:

var obj = new SomeType();
// ...
obj = new SomeType("foo", "bar", 123);
// ...

Upvotes: 1

Related Questions