Click to See Complete Forum and Search --> : [2005] string.Format with mulitple arguments


pdowling
November 23rd, 2006, 03:37 PM
Hi

I want to create a class whose constructor takes a format pattern as the first argument and then potentially infinite format arguments. e.g.

public class ZenException : Exception
{
public ZenException()
{
}

public ZenException(string message)
: base(message)
{
}

public ZenException(string format, object arg0)
: base(string.Format(format, arg0))
{
}

public ZenException(string format, object arg0, object arg1)
: base(string.Format(format, arg0, arg1))
{
}

etc.....
}

But I don't want to write a constructor for each possible number of arguments. The .NET string.Format() method itself can take virtually any number of arguments, yet when I used Reflector to see how it did this it only showed a maximum of 3 args. Is there some way of allowing a constructor to take any number of arguments without having to write a constructor for each quantity?

Cheers for any help.

mcmcom
November 23rd, 2006, 04:44 PM
why dont you just use an array of arguments
public ZenExcepotion(string[] args)

hth,
mcm

NoHero
November 23rd, 2006, 05:10 PM
public class ZenException : Exception
{
public ZenException()
{
}

public ZenException(string message)
: base(message)
{
}

public ZenException(string format, params object[] args)
: base(string.Format(format, args))
{
}
}

Works fine: "param" says that a variable amount of parameters may follow, like C's va_list. You may also drop the constructor with only one string parameter, since the second constructor may also being called if no additional parameters are being passed. It might cost a little bit runtime calling String.Format() on a string which does not have any formating marks though.

And AFAIK a non-critical application defined exception should derive from ApplicationException rather than from Exception.

Regards,
Florian

pdowling
November 23rd, 2006, 06:25 PM
Thanks for your replies. I understand I could pass a pre-defined array of params, but this obviously means predefining the array, which is something else I want to avoid.

What I want my constructor to do is similar to what the string.Format() method does when you say:

string s = string.Format("The days of the week are: {0} {1} {2} {3} {4} {5} {6}", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" );

I've experimented with this and successfully passed in well over 100 comma-separated arguments to string.Format(). Therefore the string.Format() method seems able to take any number of arguments. But when you look at it in Reflector, only a maximum of 3 are defined. How could this be done?

NoHero
November 24th, 2006, 03:36 AM
Just read my post... You need the params keyword and you can do something like this:

ZenException exception = new ZenExceptin("My format: {0}, {1}", "hello world", 0x1337);

pdowling
November 24th, 2006, 12:17 PM
Doh! Sorry mate I missed that params keyword! It was late last night when I read that. So thanks very much for solving that one for me - exactly what I was looking for afterall!!