pass n params like String.Format()

S

sklett

I have a simple debug and logging class and I would like to be able to send
a non-formatted string to my various printing methods, much like
string.format().

I have tried to implement this like so:
public void PrintDebugError(string format, object[] parameters)

{

string buff = string.Format(format, parameters);

m_outputBox.Items.Add(buff);

}


Then when calling it like this:
int someValue = 666;

m_debug.PrintDebugError("Loaded {0} shots from the database...", someValue);



I get the obvious error:
The best overloaded method match for 'CAppDebug.PrintDebugError(string,
object[])' has some invalid arguments


So looking at the string.format(), how do they do it? If I create an object
array, then store the int into the array and pass that, it works fine.
I guess what I'm looking for is a way to specify n parameters..

Any ideas?
-SK
 
S

sklett

perfect, thanks!!


Rakesh Rajan said:
Hi sklett,

Use the params keyword.
http://msdn.microsoft.com/library/en-us/csref/html/vclrfparams.asp

For example:
public void Format(params string[] args)

HTH,
Rakesh Rajan

sklett said:
I have a simple debug and logging class and I would like to be able to send
a non-formatted string to my various printing methods, much like
string.format().

I have tried to implement this like so:
public void PrintDebugError(string format, object[] parameters)

{

string buff = string.Format(format, parameters);

m_outputBox.Items.Add(buff);

}


Then when calling it like this:
int someValue = 666;

m_debug.PrintDebugError("Loaded {0} shots from the database...", someValue);



I get the obvious error:
The best overloaded method match for 'CAppDebug.PrintDebugError(string,
object[])' has some invalid arguments


So looking at the string.format(), how do they do it? If I create an object
array, then store the int into the array and pass that, it works fine.
I guess what I'm looking for is a way to specify n parameters..

Any ideas?
-SK
 
G

Guest

Anytime sklett :)

- Rakesh Rajan


sklett said:
perfect, thanks!!


Rakesh Rajan said:
Hi sklett,

Use the params keyword.
http://msdn.microsoft.com/library/en-us/csref/html/vclrfparams.asp

For example:
public void Format(params string[] args)

HTH,
Rakesh Rajan

sklett said:
I have a simple debug and logging class and I would like to be able to send
a non-formatted string to my various printing methods, much like
string.format().

I have tried to implement this like so:
public void PrintDebugError(string format, object[] parameters)

{

string buff = string.Format(format, parameters);

m_outputBox.Items.Add(buff);

}


Then when calling it like this:
int someValue = 666;

m_debug.PrintDebugError("Loaded {0} shots from the database...", someValue);



I get the obvious error:
The best overloaded method match for 'CAppDebug.PrintDebugError(string,
object[])' has some invalid arguments


So looking at the string.format(), how do they do it? If I create an object
array, then store the int into the array and pass that, it works fine.
I guess what I'm looking for is a way to specify n parameters..

Any ideas?
-SK
 
C

Chris Jobson

sklett said:
I have a simple debug and logging class and I would like to be able to send
a non-formatted string to my various printing methods, much like
string.format().

I have tried to implement this like so:
public void PrintDebugError(string format, object[] parameters)

{

string buff = string.Format(format, parameters);

m_outputBox.Items.Add(buff);

}


Then when calling it like this:
int someValue = 666;

m_debug.PrintDebugError("Loaded {0} shots from the database...",
someValue);



I get the obvious error:
The best overloaded method match for 'CAppDebug.PrintDebugError(string,
object[])' has some invalid arguments


So looking at the string.format(), how do they do it? If I create an
object
array, then store the int into the array and pass that, it works fine.
I guess what I'm looking for is a way to specify n parameters..

Any ideas?

Look up the "params" keywword. If you define your method as
public void PrintDebugError(string format, params object[] parameters)
then it should work.

Chris Jobson
 

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