1

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);
    }
}
2
  • Why would you want to call a constructor twice using the same instance? do you just want to reset the contents? Commented Jan 31, 2018 at 9:43
  • The parameterless constructor calls : this("No firstname","No lastname"), so you're already executing the code of both constructors. What are you trying to achieve? Commented Jan 31, 2018 at 9:53

2 Answers 2

1

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);
// ...
Sign up to request clarification or add additional context in comments.

4 Comments

Okay. So to call different constructors in the class we only have the option of creating different instances . Can we use Singleton Pattern for this ?
@UV009 a singleton Customer sounds like a terrible idea, unless you're writing a client-side system that only runs in the context of a single Customer
@UV009 I think this is an x/y problem; you're asking about specific things like constructors and singletons, but you haven't told us what you're actually trying to achieve; so... it is very hard to comment
Yes. I am learning the concepts right now. Thanks for your help.@Marc Gravell
0

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);
    }
}

5 Comments

it isn't at all clear to me how - if at all (which I suspect it doesn't) this answers the question at the top: "How can i call the two constructors using single instance C1 of Customer class created in Main?"
I think he wasn't sure how to put up the question since he might has not being familiar with the subject, so i assume he was referring to that.
This has helped me in understanding the concept. Thank you @Gilad Reich
@UV009 Glad it helped :)
I even Understood how this keyword works correctly by seeing the above code. @GiladReich

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.