Calling one Constructor from another
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Calling one Constructor from another

  1. #1
    Join Date
    Aug 2002
    Location
    ireland
    Posts
    291

    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

    Code:
    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)
        { }
    }

  2. #2
    Join Date
    Dec 2006
    Posts
    203

    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.
    Sincerely,

    Martin Svendsen

  3. #3
    Join Date
    Aug 2002
    Location
    ireland
    Posts
    291

    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!!!

  4. #4
    Join Date
    Dec 2006
    Posts
    203

    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.
    Sincerely,

    Martin Svendsen

  5. #5
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    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.
    Useful or not? Rate my posting. Thanks.

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    Re: Calling one Constructor from another

    Quote Originally Posted by ireland
    Code:
    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.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  7. #7
    Join Date
    Aug 2002
    Location
    ireland
    Posts
    291

    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.

  8. #8
    Join Date
    Oct 2007
    Posts
    3

    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)
    {
    //
    }
    }

  9. #9
    Join Date
    Aug 2002
    Location
    ireland
    Posts
    291

    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) ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center