Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Constructors with complex logic

Tags:

c#

.net

In C#, if you have multiple constructors, you can do something like this:

public MyClass(Guid inputId, string inputName){
    // do something
}

public MyClass(Guid inputId): this(inputId, "foo") {}

The idea is of course code reuse. However, what is the best approach when there is a bit of complex logic needed? Say I want this constructor:

public MyClass(MyOtherClass inputObject)
{
    Guid inputId = inputObject.ID;
    MyThirdClass mc = inputObject.CreateHelper();
    string inputText = mc.Text;
    mc.Dispose();
    // Need to call the main Constructor now with inputId and inputText
 }

The caveat here is that I need to create an object that has to be disposed after use. (Clarification: Not immediately, but I have to call Dispose() rather than waiting for Garbage Collection)

However, I did not see a way to just call the base constructor again if I add some code inside my overloaded constructor. Is there a way to call the base constructor from within an overloaded one?

Or is it possible to use

public MyClass(MyOtherClass inputObject): this(inputObject.ID,
                                               inputObject.CreateHelper().Text) 
{}

Would this automatically Dispose the generated Object from CreateHelper()?

Edit: Thanks so far. Two problems: I do not control MyOtherClass and I do not have Extension Methods (only .NET 3.0...). I do control my own class though, and since I've just started writing it, I have no problem refactoring the constructors if there is a good approach.

like image 780
Michael Stum Avatar asked Sep 20 '08 23:09

Michael Stum


People also ask

Can we use multiple constructors in a class?

A class can have multiple constructors that assign the fields in different ways. Sometimes it's beneficial to specify every aspect of an object's data by assigning parameters to the fields, but other times it might be appropriate to define only one or a few.

Can a class have multiple constructors with different names?

The technique of having two (or more) constructors in a class is known as constructor overloading. A class can have multiple constructors that differ in the number and/or type of their parameters. It's not, however, possible to have two constructors with the exact same parameters.

What is the purpose of multiple constructors?

In other words, you want a class that implements multiple constructors. This kind of class comes in handy when you need to create instances using different types or numbers of arguments. Having the tools to provide multiple constructors will help you write flexible classes that can adapt to changing needs.

Can a class have multiple constructors C++?

Constructor Overloading in C++ In C++, We can have more than one constructor in a class with same name, as long as each has a different list of arguments. This concept is known as Constructor Overloading and is quite similar to function overloading.


1 Answers

The most common pattern used to solve this problem is to have an Initialize() method that your constructors call, but in the example you just gave, adding a static method that you called like the code below, would do the trick.

public MyClass(MyOtherClass inputObject): this(inputObject.ID, GetHelperText(inputObject) {}

private static string GetHelperText(MyOtherClass o)
{
   using (var helper = o.CreateHelper())
      return helper.Text;
}
like image 72
Stefan Rusek Avatar answered Nov 14 '22 22:11

Stefan Rusek