?
Solved

C# Constructor - chain AFTER iniitalization?

Posted on 2009-12-29
11
Medium Priority
?
287 Views
Last Modified: 2012-05-08
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)
}

0
Comment
Question by:sfun28
  • 5
  • 4
  • 2
11 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 400 total points
ID: 26139167
In short, no.

You chaining is incorrect. The syntax for chaining is below. In your code, you are *attempting* to create a new object within the active constructor. I say "attempting" because you don't call constructors like traditional methods--they are called using the "new" keyword.
// Chaining example

public class Class1
{
    public Class1(string a)
    {

    }

    public Class1(object b) : this(b.ToString())
    {

    }
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 26139178
In the above, the constructor that takes an object as its parameter calls the constructor that takes a string (using the "this" operator).
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26139197
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

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:sfun28
ID: 26139248
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

0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 1600 total points
ID: 26139281
You can use private methods and call them from your constructors. ;)
public class Class1
{
  private myInit(string b)
  {
  }

  public Class1(string b, object c)
  {
    if(c != null)
      myInit(b + c.ToString());
    else
      myInit(b);
  }
}

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26139285
And then do the actual work of initializing your object in myInit instead of directly in the constructor.
0
 
LVL 1

Author Comment

by:sfun28
ID: 26139326
tgerbert - what if I want myInit(string b) to be public?  its a completely valid publically available constructor in my case.
0
 
LVL 1

Author Comment

by:sfun28
ID: 26139353
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?
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26139366
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");
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26139381
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.
0
 
LVL 1

Author Comment

by:sfun28
ID: 26139425
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question