HowTo: Wrap the String.Format method?

  • Thread starter Thread starter Matthias S.
  • Start date Start date
M

Matthias S.

Hi,

I'm writing a logging utility and I need to wrap the string.Format
method. I'd like to call the Trace method of MyLog like this:

MyLog.Trace("Var1:{0};Var2:{1};", MyVar1, MyVar2);

The Trace method should then use the string.Format function to produce a
properly looking string. I thought the signature for my Trace method
should be:

public void Trace(string s, object[] args){};

But the compiler complains:
cannot convert from 'string' to 'object[]'
when coding:

string sFun = "Hello World!";
MyLog.Trace("Say {0}", sFun);


How can this be done? Any help is greatly appreceated.

Matthias
 
see console.writeline method in documentation for example :-)

You can Use

public void Trace(
string format,
params object[] arg
)
{
....
}

inside this method, use String.Format(format, arg) to get the formatted string & then
write it to the appropriate stream

does this help ?

Kalpesh
 
see console.writeline method in documentation for example :-)

You can Use

public void Trace(
string format,
params object[] arg
)
{
....
}

inside this method, use String.Format(format, arg) to get the formatted
string & then
write it to the appropriate stream

does this help ?

Kalpesh

Thanks a lot. Excactly what I needed!

Matthias
 
Back
Top