Calling one Constructor from another

Is this correct? or how should I do this.

The first constructor is called, it does some manipulation of the varialbes and then calls the other constructor overload, which in turn calls the base class's constructor

public class MySoapException : SoapException
{
public MySoapException(string message, Exception e)
{
//Edit message and other varibles before calling overload
.............
new MySoapException("Fault occurred", SoapException.ClientFaultCode, "http://localhost/SoapException/Default.aspx" , node);


}
public MySoapException(string message, XmlQualifiedName name, string absoluteUri, XmlNode node)
: base(message, name , absoluteUri, node)
{ }
}
[779 byte] By [ireland] at [2007-11-20 11:30:06]
# 1 Re: Calling one Constructor from another
I don't think you can do it like that, since that would create a new object of it. You can use this the same way you use base, however.
Homogenn at 2007-11-9 11:36:45 >
# 2 Re: Calling one Constructor from another
But I do not have the variables set when I first call the constructor, they only get set within the constructor, calling : base(...) from the first constructor would not have the correct values!!!
ireland at 2007-11-9 11:37:46 >
# 3 Re: Calling one Constructor from another
[/code]
public class MySoapException : SoapException
{
public MySoapException(string message, Exception e)
:this(message, null, "", null) // <-- calls other constructor
{
//Edit message and other varibles before calling overload
.............
new MySoapException("Fault occurred", SoapException.ClientFaultCode, "http://localhost/SoapException/Default.aspx" , node);


}
public MySoapException(string message, XmlQualifiedName name, string absoluteUri, XmlNode node)
: base(message, name , absoluteUri, node)
{ }
}
[/code]

You can do it like above, where you set some bogus values and then set the values yourself, or you'll have to refactor your code so it's done elsewhere.
Homogenn at 2007-11-9 11:38:45 >
# 4 Re: Calling one Constructor from another
Hm, I do not understand for what this kind of behavior should be good for. Maybe I am wrong but in general you call a base constructor to set initial values before you set properties inside of the derived class. It seems you are trying to set values before you initialize the default values. This is very confusing for me.

If I have a look at your code I believe that it do not work. I have to say that I do not test it but in my opinion you set default values and call then another constructor. So the class instance from the first constructor call will be disposed and the instance from the second will be used, or not? So the initialized values should be not used when you call the second constructor.

Difficult topic but I believe there is a mistake in the design. Maybe you should rethink what want and refactoring your classes.
torrud at 2007-11-9 11:39:51 >
# 5 Re: Calling one Constructor from another
public class MySoapException : SoapException
{
public MySoapException(string message, Exception e)
: base ()
{

this.message = "fault occured";
this.**** = ***;
etc.
}
}

You always have to call the base constructor first. If you don't have an explicit call to the base constructor (as i've added), then by default the parameterless constructor is called. If there is no parameterless constructor in your base object, then the code won't compile until you explicitly call one of it's other constructors.
Mutant_Fruit at 2007-11-9 11:40:51 >
# 6 Re: Calling one Constructor from another
First of all does the calling of the second constructor using new actually work?
I didn't think the new was required, I expected the this(....) to work, same as a method call, but that wouldn't compile.

I understand the requirement for calling the base(..) first. The use of null value arguments followed by a later setter makes sense but I had thought there would be another way.
ireland at 2007-11-9 11:41:52 >
# 7 Re: Calling one Constructor from another
Just take whatever code you have in the second constructor and create a new function with it. This function can be called from anywhere and won't be subject to the rules constructors have to follow.

class MyObj
{
public MyObj(int arg1)
{
//Edit varibles before calling InitMyObj
}

public MyObj(int arg1, string arg2)
{
//Edit varibles before calling InitMyObj
}

private InitMyObj(int arg1, string arg2)
{
//
}
}
tpcampb at 2007-11-9 11:42:47 >
# 8 Re: Calling one Constructor from another
Yes and where do you call the base constructor from, with these variables I've just changed in InitMyObj(int arg1, string arg2) ?
ireland at 2007-11-9 11:43:51 >