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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top