473,785 Members | 3,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(f mt, str[0], str[1], str[2], str[3]); // works
string s2 = String.Format(f mt, str); // also
works

And I can format a list of doubles like this:
string s3 = String.Format(f mt, 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(f mt, 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 36870
Bob <Co*********@no spam.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(f mt, str[0], str[1], str[2], str[3]); // works
string s2 = String.Format(f mt, str); // also
works

And I can format a list of doubles like this:
string s3 = String.Format(f mt, 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(f mt, 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.co m>
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.WriteLi ne(fmt,val);
Console.ReadKey ();

This will print "System.Dou ble[]"... 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.WriteLi ne(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.WriteLi ne(fmt,val);
Console.ReadKey ();

float[] val = { 1.0F, 2.0F, 3.0F, 4.0F };
String fmt = "{0} {1} {2} {3}";
Console.WriteLi ne(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*********@no spam.nospam.com > wrote in message
news:18******** *************** ***********@mic rosoft.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(f mt, str[0], str[1], str[2], str[3]); // works
string s2 = String.Format(f mt, str); // also
works

And I can format a list of doubles like this:
string s3 = String.Format(f mt, 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(f mt, 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
4044
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 object); What happens if i will use String.Format("{0} and {1}", "one", "two") instead of String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0} and {1}", "one", "two")?
5
3892
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". Everything I have tried so far causes the program I am calling to fail (I know it can accept long file names as I have tried it from the cmdline). The problem I am having is using the Format command and getting "
5
11156
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 so: public void PrintDebugError(string format, object parameters) { string buff = string.Format(format, parameters);
7
6496
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 where I can look up explaination on the String.Format, the format string part like "("{0:.#0}". What's the trailing 0 and what is that "0:" means? I assume the "#" is the place holder character for th value obtain after the comma in the code. ...
6
5238
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 that very same format string from an xml file: ....load xmlNode WorkNode...
8
4992
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 string and print it. but i want to use the String.Format() method if possible to do it.
1
10697
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 Message="Input string was not in a correct format." Source="mscorlib"
15
2609
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 both and I am wondewring if I should standardize or not ??
8
2224
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 that it is because the "mystr" it is already a string. Anyone can help me? Sorry my english....
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.