HowTo: Wrap the String.Format method?

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
 
G

Guest

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
 
M

Matthias S.

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
 

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

Top