|
-
Dec 16th, 2002, 01:01 PM
#1
Thread Starter
Lively Member
Problem with Microsoft Example textBox.Lines
Ok I am still learning C# going from VB and basically just playing around with the different controls and what they do. But I found an inconsistency on the Dynamic Help and the actual code that works. So can you tell me what is the proper way of doing this?
Ok a simple textbox, the lines are put into an array automatically. this I understand and well I like it and now I am trying to use it. So if you click on the line property in the properties window of a textBox then the help and example for that is this next code, however if I use that code I get an error, when I reach the end of the loop it bombs because I am going over the Upper bound limit of the array.
Code:
Microsofts code
// Create a string array and store the contents of the Lines property.
string[] tempArray = new string [textBox1.Lines.Length];
tempArray = textBox1.Lines;
// Loop through the array and send the contents of the array to debug window.
for(int counter=0; counter <= tempArray.Length;counter++)
{
System.Diagnostics.Debug.WriteLine(tempArray[counter]);
}
Now My code just a simple change in the for loop parameters and my code works, now is microsoft wrong in the documentation sample or am I just missing something
Code:
my Code
string[] textLines = new string [textWindow.Lines.Length];
textLines = textWindow.Lines;
for (int loopcount=0; loopcount < textLines.Length; loopcount++)
{
MessageBox.Show(textLines[loopcount]);
}
The secret to creativity is knowing how to hide your sources.
-- Albert Einstein
-
Dec 16th, 2002, 01:58 PM
#2
Frenzied Member
Code:
for(int counter=0; counter <= tempArray.Length;counter++)
Thats the problem. Remember the first index of an array is 0. So if you declare an array like this.
Code:
string[] myString = new string [5];
//These are valid
myString[0] = 1;
myString[1] = 2;
myString[2] = 3;
myString[3] = 4;
myString[4] = 5;
//This is not. Will get out of bounds error
myString[5] = 6
In VB6, this would be ok, since the first index of an array in VB6 is 1 or what you set it to.
So the correct header for the for loop would be
Code:
for(int counter=0; counter < tempArray.Length;counter++)
MS probably made a mistake in the documentation.
Dont gain the world and lose your soul
-
Dec 16th, 2002, 02:27 PM
#3
Thread Starter
Lively Member
Thanks DevGrp I kinda figured it was an MS error in the docs, the arrays there make sense, still getting used to the Syntax and how it does the things it does.
The secret to creativity is knowing how to hide your sources.
-- Albert Einstein
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|