none
calling one constructor from other in same class RRS feed

  • Question

  • public DataAccessFailureAuditEvent(String message, Object eventSource, Int32 eventCode, String methodName)
                : base(message, eventSource, eventCode)
            {
                this.methodName = methodName;
               
                this.userID = Thread.CurrentPrincipal.Identity.Name;
            }

            public DataAccessFailureAuditEvent(String message, Object eventSource, Int32 eventCode,Int32 eventDetailCode, String methodName)
                : base(message, eventSource, eventCode, eventDetailCode)
            {
                //Here i want to call 4 parameter constructor declared above so that i can pass 4 parameters to that constructor and  process     
                //    last  parameter here in this constructor

                //How is that possible ??????????????????

            }
    Programmer
    Wednesday, April 1, 2009 11:44 AM

Answers

  • You can't.  If you have to re-type too much initialization code, use a smaller private helper method.
    Hans Passant.
    • Marked as answer by Sanjay Sutar Thursday, April 2, 2009 3:49 AM
    Wednesday, April 1, 2009 11:47 AM
    Moderator
  • Another approach would be to invoke the constructor with an extra parameter from the one without it, passing a reasonable default value for the missing parameter.  e.g.:

    public DataAccessFailureAuditEvent(String message, Object eventSource, Int32 eventCode, String methodName)
    	: this(message, eventSource, eventCode, 0, methodName)
    {
    }
    
    public DataAccessFailureAuditEvent(String message, Object eventSource, Int32 eventCode,Int32 eventDetailCode, String methodName)
    	: base(message, eventSource, eventCode, eventDetailCode)
    {
    	this.methodName = methodName;
    	this.userID = Thread.CurrentPrincipal.Identity.Name;
    }
    • Marked as answer by Sanjay Sutar Thursday, April 2, 2009 3:49 AM
    Wednesday, April 1, 2009 12:10 PM

All replies

  • You can't.  If you have to re-type too much initialization code, use a smaller private helper method.
    Hans Passant.
    • Marked as answer by Sanjay Sutar Thursday, April 2, 2009 3:49 AM
    Wednesday, April 1, 2009 11:47 AM
    Moderator
  • Another approach would be to invoke the constructor with an extra parameter from the one without it, passing a reasonable default value for the missing parameter.  e.g.:

    public DataAccessFailureAuditEvent(String message, Object eventSource, Int32 eventCode, String methodName)
    	: this(message, eventSource, eventCode, 0, methodName)
    {
    }
    
    public DataAccessFailureAuditEvent(String message, Object eventSource, Int32 eventCode,Int32 eventDetailCode, String methodName)
    	: base(message, eventSource, eventCode, eventDetailCode)
    {
    	this.methodName = methodName;
    	this.userID = Thread.CurrentPrincipal.Identity.Name;
    }
    • Marked as answer by Sanjay Sutar Thursday, April 2, 2009 3:49 AM
    Wednesday, April 1, 2009 12:10 PM