Results 1 to 3 of 3

Thread: Problem with Microsoft Example textBox.Lines

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Posts
    127

    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

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Posts
    127
    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
  •  



Click Here to Expand Forum to Full Width