473,322 Members | 1,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

string.Format with arrays

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
2 36720
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: san | last post by:
Hello, all! I have question about String.Format method. There are two variants: public static string Format(string, params object); and public static string Format(IFormatProvider, string, params...
5
by: raffelm | last post by:
I'm struggling to find a way to include long path names in a command line argument string that I have to build at runtime. I need to create a string like -o:"c:\my documents\my file.txt". ...
5
by: sklett | last post by:
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...
7
by: Alpha | last post by:
Hi, I'm maintaining C# code and am fairly new with C# programming. I'm looking for codes that's droping the 2nd digit of a nuber printed out and I suspect it's the code below. Can someone tell me...
6
by: Scewbedew | last post by:
Suppose I have the following code: string myFormat = "Line1/nLine 2"; string formattedString = string.Format(myFormat); ....that would produce a 2-line output as expected. But if I load...
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
1
by: Remi THOMAS | last post by:
Hi, When you execute this line of code string script = string.Format("foreach (GroupDoc gdoc in {0}.SetGroup(\"{1}\") {\r\n", "p1", "p2"); You get System.FormatException was unhandled...
15
by: James | last post by:
Which is better, which is faster, which is easier etc... ????? String.Format ( "yadda {0} yadda {1}", x, y ) "yadda" + x.ToString() + " yadda" + y.tostring(); My code has a mish mash of...
8
by: Armando Rocha | last post by:
Hi, Hi have a string with 16 chars "25DD68EDEB8D5E11" and i want show it in form like this "25DD-68ED-EB8D-5E11", i try String.Format("{0:####-####-####-####}", mystr), but not work, i think...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.