473,398 Members | 2,525 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,398 software developers and data experts.

Stringbuilder error

I am trying to implement the stringbuilder class by taking a LARGE
multiline text box and looping through the lines and build a string
and include \r\n in it.

I get : Index was outside the bounds of the array.

The code is simply:

StringBuilder str = new StringBuilder();

for (int counter = 0; counter <= tempArray.Length; counter++)
{

str.Append(tempArray[counter] + @"\r\n");

}

Is there a size limit on the size of my stringbuilder object?

Oct 26 '07 #1
8 3785
In article
<11**********************@t8g2000prg.googlegroups. com>Soulless
<dg*******@gmail.comwrote:
Is there a size limit on the size of my stringbuilder object?
No. The problem is that you are letting your counter equal
tempArray.Length, when in fact it should only be used when it's less
than tempArray.Length.

Pete

--
I'm trying a new usenet client for Mac, Nemo OS X.
You can download it at http://www.malcom-mac.com/nemo

Oct 26 '07 #2
On Oct 26, 3:50 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
In article
<1193431094.036794.176...@t8g2000prg.googlegroups. com>Soulless

<dgmsal...@gmail.comwrote:
Is there a size limit on the size of my stringbuilder object?

No. The problem is that you are letting your counter equal
tempArray.Length, when in fact it should only be used when it's less
than tempArray.Length.

Pete

--
I'm trying a new usenet client for Mac, Nemo OS X.
You can download it athttp://www.malcom-mac.com/nemo
wow, that was it... thank you so much!

Oct 26 '07 #3
Hi Soulless,

The exception happens here:
tempArray[counter]

when the value of the counter reaches tempArray.Length.

Note that if you have an array of a given length, e.g. 6, its last element's
index will be 5.

Therefore, you should rewrite your for loop like this:
for (int counter = 0; counter < tempArray.Length; counter++)
Hope this helps,
--
_____________
Adam Bieganski
http://godevelop.blogspot.com
"Soulless" wrote:
I am trying to implement the stringbuilder class by taking a LARGE
multiline text box and looping through the lines and build a string
and include \r\n in it.

I get : Index was outside the bounds of the array.

The code is simply:

StringBuilder str = new StringBuilder();

for (int counter = 0; counter <= tempArray.Length; counter++)
{

str.Append(tempArray[counter] + @"\r\n");

}

Is there a size limit on the size of my stringbuilder object?

Oct 26 '07 #4
Soulless <dg*******@gmail.comwrote:
I am trying to implement the stringbuilder class by taking a LARGE
multiline text box and looping through the lines and build a string
and include \r\n in it.

I get : Index was outside the bounds of the array.

The code is simply:

StringBuilder str = new StringBuilder();

for (int counter = 0; counter <= tempArray.Length; counter++)
{

str.Append(tempArray[counter] + @"\r\n");

}

Is there a size limit on the size of my stringbuilder object?
Others have said what was causing the problem. Personally, however, I'd
go about it differently:

StringBuilder builder = new StringBuilder();

foreach (string line in tempArray)
{
builder.Append(line);
builder.Append(@"\r\n");
}

That has two advantages:
1) Using foreach is more straightforward in both code and conveying
intention than using the array index

2) By using string concatenation to construct the argument of the call
to Append, you are creating an extra copy of a string for no good
reason. (An alternative to calling Append twice would be to use
AppendFormat.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 26 '07 #5
Would StringBuilder.AppendLine() have any disadvantages that one should be
aware?

"Jon Skeet [C# MVP]" wrote:
Soulless <dg*******@gmail.comwrote:
I am trying to implement the stringbuilder class by taking a LARGE
multiline text box and looping through the lines and build a string
and include \r\n in it.

I get : Index was outside the bounds of the array.

The code is simply:

StringBuilder str = new StringBuilder();

for (int counter = 0; counter <= tempArray.Length; counter++)
{

str.Append(tempArray[counter] + @"\r\n");

}

Is there a size limit on the size of my stringbuilder object?

Others have said what was causing the problem. Personally, however, I'd
go about it differently:

StringBuilder builder = new StringBuilder();

foreach (string line in tempArray)
{
builder.Append(line);
builder.Append(@"\r\n");
}

That has two advantages:
1) Using foreach is more straightforward in both code and conveying
intention than using the array index

2) By using string concatenation to construct the argument of the call
to Append, you are creating an extra copy of a string for no good
reason. (An alternative to calling Append twice would be to use
AppendFormat.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 27 '07 #6
Family Tree Mike <Fa************@discussions.microsoft.comwrote:
Would StringBuilder.AppendLine() have any disadvantages that one should be
aware?
No - indeed, that's a really good option that I unfortunately forgot :(

(I'd still use StreamWriter directly in preference though.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 27 '07 #7
On 27 oct, 09:57, Family Tree Mike
<FamilyTreeM...@discussions.microsoft.comwrote:
Would StringBuilder.AppendLine() have any disadvantages that one should be
aware?

"Jon Skeet [C# MVP]" wrote:Soulless <dgmsal...@gmail.comwrote:
I am trying to implement the stringbuilder class by taking a LARGE
multiline text box and looping through the lines and build a string
and include \r\n in it.
I get : Index was outside the bounds of the array.
The code is simply:
StringBuilder str = new StringBuilder();
for (int counter = 0; counter <= tempArray.Length; counter++)
{
str.Append(tempArray[counter] + @"\r\n");
}
Is there a size limit on the size of my stringbuilder object?
Others have said what was causing the problem. Personally, however, I'd
go about it differently:
StringBuilder builder = new StringBuilder();
foreach (string line in tempArray)
{
builder.Append(line);
builder.Append(@"\r\n");
}
That has two advantages:
1) Using foreach is more straightforward in both code and conveying
intention than using the array index
2) By using string concatenation to construct the argument of the call
to Append, you are creating an extra copy of a string for no good
reason. (An alternative to calling Append twice would be to use
AppendFormat.)
--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Hi I think the problem is in the for condicion condicion.

for (int counter = 0; counter <= tempArray.Length; counter++)

Should be

for (int counter = 0; counter < tempArray.Length; counter++)

only "minus" without equal, becouse for example if the array has 5
elements the array begins in "0" position. In the example from 0 to 4
= 5 elements (position 5 does not exist).
Bye.

Oct 27 '07 #8
Maximiliano <mn*****@gmail.comwrote:

<snip>
Hi I think the problem is in the for condicion condicion.

for (int counter = 0; counter <= tempArray.Length; counter++)

Should be

for (int counter = 0; counter < tempArray.Length; counter++)
Absolutely - as I said, others had pointed out the reason the original
code was going "bang" - I was proposing alternative code which would
have avoided the problem in the first place.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 27 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: hplloyd | last post by:
I am using some code off the web that requires a string builder in a script within a web page. In order to get the script to work I need to include the relevant library in the references section...
16
by: Alvin Bruney | last post by:
Is string builder intelligent enough to handle concats without behaving like string? Consider myStringBuilder.Append("one" + "two") what does the '+' do here? Because this syntax is also...
0
by: Mo | last post by:
I am having problem with marshaling struct in C#. //the original C++ struct typedef struct _tagHHP_DECODE_MSG { DWORD dwStructSize; // Size of decode structure. TCHAR ...
3
by: Salvador | last post by:
Hi, I am using an old Win32 DLL that expects a LPBYTE as a parameter. The Stringbuilder appears to be working, the problem is that the component is sending me a string with NULL characters in the...
2
by: Brent | last post by:
I'm constructing a long SQL string using StringBuilder. Every 100 or so iterations, I write the SQL to the database, and use the StringBuilder.Remove method to "zero out" the string: e.g.,...
7
by: Larry Bird | last post by:
I want to use the "replace" in the string builder class. I've imported the "system.text.stringbuilder" class into my project. However, I don't see the method call for...
8
by: Henning M | last post by:
Hi, I'm trying to use stringbuilder to collect a list of strings. (as suggested by Claes Bergefall) Declare Auto Function CM_Get_Device_ID_List Lib "cfgmgr32.dll" (ByVal pszFilter As String,...
26
by: Hardy Wang | last post by:
Hi all, I know it is better to handle large string with a StringBuilder, but how does StringBuilder class improve the performance in the background? Thanks! -- WWW:...
8
by: Arjan | last post by:
Hello, I have been looking for a couple of days now, but I can't find anything about how to deal with StringBuilder and releasing the memory used by it. When I use stringbuilder, the memory...
4
by: raylopez99 | last post by:
I see that String and StringBuilder in C# / C++ do not have an easy way to set a string to null or zero length, once it is instantiated. Apparently some variant of the .NET languages do (reading...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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...

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.