Calling inherited constructor with formatted parameters

  • Thread starter Thread starter Claire
  • Start date Start date
C

Claire

My descendent constructor takes a string and an integer as parameters.
I'd like to format the message string before passing it to it's parent class
but it looks as though base(message,ErrorCode) has to be called too early.
Is this possible please?

Parent constructor

public class CommException : System.ApplicationException

{

public UInt32 m_nErrorCode;

public CommException(string message, UInt32 ErrorCode):

base(message)

{

m_nErrorCode = ErrorCode;

}

}

Descendent constructor

public LastErrorException(string message, UInt32 ErrorCode):

base(message,ErrorCode)<< original call

{

String str;

str = String.Format("File system reported error %d while %s",
(UInt32)ErrorCode, (String)message);

base(str,ErrorCode); <<<< doesn't compile

}
 
Claire said:
My descendent constructor takes a string and an integer as parameters.
I'd like to format the message string before passing it to it's parent class
but it looks as though base(message,ErrorCode) has to be called too early.
Is this possible please?

Sure:

public LastErrorException (string message, UInt32 errorCode) :
base (String.Format("File system reported error {0} while {1}",
message, errorCode), errorCode)
{
}
 
...
My descendent constructor takes a string and
an integer as parameters. I'd like to format
the message string before passing it to it's
parent class but it looks as though base(message,
ErrorCode) has to be called too early.
Is this possible please?

There's two ways to accomplish what I think you want.

Format the message already in the call to the base constructor:

public LastErrorException(string message, UInt32 ErrorCode) :
base( String.Format("File system reported error {0} while {1}",
ErrorCode, message) ,
ErrorCode) {}


....or just override the Message property.

// Bjorn A
 
Back
Top