By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,179 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,179 IT Pros & Developers. It's quick & easy.

string.Format with arrays

P: n/a
Bob
I'm having trouble the string.Format() throwing exceptions and I can't figure
out what I am doing wrong.

Given the following setup code:
string[] str = { "one", "two", "three", "four" };
double[] val = { 1.0, 2.0, 3.0, 4.0 };
string fmt = "{0} {1} {2} {3}";

The following variations of string.Format() will work just fine with strings:
string s1 = String.Format(fmt, str[0], str[1], str[2], str[3]); // works
string s2 = String.Format(fmt, str); // also
works

And I can format a list of doubles like this:
string s3 = String.Format(fmt, val[0], val[1], val[2], val[3]); // works

But when I pass an array of doubles as the object[] parameter I get an
exception:
string s4 = String.Format(fmt, val); // fails

Throws exception:
"Index (zero based) must be greater than or equal to zero and less than the
size of the argument list."

Why does the array version work with strings but not doubles? Please
enlighten me? Thanks much!

Bob
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Bob <Co*********@nospam.nospam.com> wrote:
I'm having trouble the string.Format() throwing exceptions and I can't figure
out what I am doing wrong.

Given the following setup code:
string[] str = { "one", "two", "three", "four" };
double[] val = { 1.0, 2.0, 3.0, 4.0 };
string fmt = "{0} {1} {2} {3}";

The following variations of string.Format() will work just fine with strings:
string s1 = String.Format(fmt, str[0], str[1], str[2], str[3]); // works
string s2 = String.Format(fmt, str); // also
works

And I can format a list of doubles like this:
string s3 = String.Format(fmt, val[0], val[1], val[2], val[3]); // works

But when I pass an array of doubles as the object[] parameter I get an
exception:
string s4 = String.Format(fmt, val); // fails

Throws exception:
"Index (zero based) must be greater than or equal to zero and less than the
size of the argument list."

Why does the array version work with strings but not doubles? Please
enlighten me? Thanks much!


The problem is that string.Format takes a string and an array of object
references. An array of doubles isn't an array of object references, so
it assumes that it's just the first parameter in itself. In other
words, you're effectively calling:

string s1 = String.Format (fmt, (Object[]) str);
but
string s2 = String.Format (fmt, new Object[]{val});

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

P: n/a
Hi Bob,
Answer is simple. Try this code and u will see the answer easily.

double[] val = { 1.0, 2.0, 3.0, 4.0 };
String fmt = "{0}";
Console.WriteLine(fmt,val);
Console.ReadKey();

This will print "System.Double[]"... This is way u get an index exception

Now try this;

object[] val = { 1.0, 2.0, 3.0, 4.0 };
String fmt = "{0} {1} {2} {3}";
Console.WriteLine(fmt,val);
Console.ReadKey();

This will print "1 2 3 4".

Try this;

decimal[] val = { 1.0M, 2.0M, 3.0M, 4.0M };
String fmt = "{0} {1} {2} {3}";
Console.WriteLine(fmt,val);
Console.ReadKey();

float[] val = { 1.0F, 2.0F, 3.0F, 4.0F };
String fmt = "{0} {1} {2} {3}";
Console.WriteLine(fmt,val);
Console.ReadKey();
....

Try this for all numeric data types u will get same result it caused by the
implementation of the numeric arrays. I think this is why we call them as
value types.
Obect[] example is a simple implementation of boxing....

--
HTH

Thanks,
Yunus Emre ALP÷ZEN
BSc, MCSD.NET

"Bob" <Co*********@nospam.nospam.com> wrote in message
news:18**********************************@microsof t.com...
I'm having trouble the string.Format() throwing exceptions and I can't
figure
out what I am doing wrong.

Given the following setup code:
string[] str = { "one", "two", "three", "four" };
double[] val = { 1.0, 2.0, 3.0, 4.0 };
string fmt = "{0} {1} {2} {3}";

The following variations of string.Format() will work just fine with
strings:
string s1 = String.Format(fmt, str[0], str[1], str[2], str[3]); // works
string s2 = String.Format(fmt, str); // also
works

And I can format a list of doubles like this:
string s3 = String.Format(fmt, val[0], val[1], val[2], val[3]); // works

But when I pass an array of doubles as the object[] parameter I get an
exception:
string s4 = String.Format(fmt, val); // fails

Throws exception:
"Index (zero based) must be greater than or equal to zero and less than
the
size of the argument list."

Why does the array version work with strings but not doubles? Please
enlighten me? Thanks much!

Bob

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.