Link to home
Start Free TrialLog in
Avatar of sfun28
sfun28

asked on

C# Constructor - chain AFTER iniitalization?

Folks,

Is it possible to chain a constructor after some initialization instead of before?

For example:

public MyClass(int myParam)
{
   _myParam = myParam
}

public MyClass (SomeObject myObject)
{
// first check if myObject is null.
// if not, then call MyClass(int) - perhaps this(myObject.GetInt)
}

SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In the above, the constructor that takes an object as its parameter calls the constructor that takes a string (using the "this" operator).
You can use private methods, and call them from your constructors.
public class MyClass
{
  private int _myParam;
  private object _myObject;

  private void MyInit(object MyObject, int MyInt)
  {
    if(MyObject == null)
      _myObject = MyObject;
    else
      _myParam = MyInt;
  }

  public MyClass(int myParam)
  {
    MyInit(null, myParam);
  }

  public MyClass(object MyObject)
  {
    MyInit(MyObject, 0);
  }
}

Open in new window

Avatar of sfun28
sfun28

ASKER

folks - please don't take my example literally..i'm trying to express my point.  I completely understand how chaining works, I'm trying to see if there's a mechanism to initialize before chaining.

if not, then there's the possibility that the constructor throws a NullReferenceException instead of an ArgumentNullException. Here's a more well-formed example:

var test = Class1("abc", null)
this will throw a NullReferenceException, instead of an ArgumentNullException, which is more desireable
public class Class1
{
    public Class1(string a)
    {
    }

    public Class1(string b, MyObject c) : this( b + c.ToString() )
    {
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And then do the actual work of initializing your object in myInit instead of directly in the constructor.
Avatar of sfun28

ASKER

tgerbert - what if I want myInit(string b) to be public?  its a completely valid publically available constructor in my case.
Avatar of sfun28

ASKER

Ah! I could make myInit(string b) public, and have it call a private function that validates and stores the parameters.  the second constructor could do the same.  instead of chaining, it can call the same private function.  i guess this is the best I could do?
myInit isn't a constructor.  It's a private "worker" method that is called by the already publically accessible constructors.  This way, your constructor can make decisions and call the appropriate worker.  So in my little example above you can do either:

Class1 myObj = new Class1("Hello World", null);

or

Class1 myObj = new Class1("Hello", "world");
I think you've got it - just to clarify, in my code snippet above line 7 is the public constructor, which is calling the private function myInit.
Avatar of sfun28

ASKER

tgerbert - correct, I understood the purpose of the private function.  What I was saying is that the private function itself defines a valid set of Constructor Parameters.  In your example, there was no pubic constructor that took just one string, which is a valid constructor in my case.

Basically the answer is that there's no good way to error-check and chain in my example.  You either chain and get NullReference or you Initializes and lose the benefits of chaining.