i don't know what is the error

BegProg

New member
Joined
Jun 8, 2012
Messages
3
Programming Experience
Beginner
good evening everybody
i have a problem in my project, but i dont know where is the error
code:
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox3.Text = ""
        Dim cols As Integer = TextBox1.Text
        Dim plaintext As String = TextBox2.Text
        Dim plainarray As Array = plaintext.ToCharArray()
        Dim textarraylength As Integer = plainarray.Length
        TextBox3.Text = Ceiling(textarraylength / cols)
        arrayOfPlain = New String(Ceiling(textarraylength / cols), cols) {}
        For i As Integer = 0 To arrayOfPlain.GetUpperBound(0)
            For j As Integer = 0 To arrayOfPlain.GetUpperBound(1)
                arrayOfPlain(i, j) = "A"
            Next
        Next
        arrayOfPlain(0, 0) = ""
        '****************
        For i As Integer = 0 To arrayOfPlain.GetUpperBound(0)
            For j As Integer = 0 To arrayOfPlain.GetUpperBound(1)
                TextBox4.Text += arrayOfPlain(i, j)
            Next
        Next
        '****************
        For i As Integer = 0 To arrayOfPlain.GetUpperBound(0)
            For j As Integer = 0 To arrayOfPlain.GetUpperBound(1)
                TextBox4.Text += arrayOfPlain(i, j) + "  "
            Next
            TextBox4.Text += vbCrLf
        Next



    End Sub

and the output is :
VB.NET:
textbox1:3

textbox2:abcdefghijk

textbox3:4

textbox4:AAAAAAAAAAAAAAAAAAA  A  A  A  
A  A  A  A  
A  A  A  A  
A  A  A  A  
A  A  A  A

arrayofplain(0,0) must = a, but it is: AAAAAAAAAAAAAAAAAAA

where is the error ?
help me please
thanks in advance :)
 
BegProg said:
arrayofplain(0,0) must = a, but it is: AAAAAAAAAAAAAAAAAAA
No, it is not. You can put a breakpoint after you have filled the array and use the debugger to see all values in it, you can also enter this in Immediate window to have it evalute a specific value:
?arrayOfPlain(0, 0)
Then, when you have debugged the array and learned its true values, you realize you must debug the code that sets TextBox4.Text. You have two code blocks that does that, and their code differ slightly, can you see how your two loops are different, in how they assign text to TextBox4 ?

I also recommend you turn on Option Strict and fix your implicit type conversions.
 
other 1 if possible :)
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox3.Text = ""
        Dim cols As Integer = TextBox1.Text - 1
        Dim plaintext As String = TextBox2.Text
        Dim plainarray As Array = plaintext.ToCharArray()
        Dim textarraylength As Integer = plainarray.Length
        TextBox3.Text = Ceiling(textarraylength / cols)
        arrayOfPlain = New String(Ceiling(textarraylength / cols), cols) {}
        Dim count As Integer = 0
        For i As Integer = 0 To arrayOfPlain.GetUpperBound(0)
            For j As Integer = 0 To arrayOfPlain.GetUpperBound(1)
                arrayOfPlain(i, j) = "A"
            Next
        Next
        '*****************
        For i As Integer = 0 To arrayOfPlain.GetUpperBound(0)
            For j As Integer = 0 To arrayOfPlain.GetUpperBound(1)
                TextBox4.Text += arrayOfPlain(i, j) + "  "
            Next
            TextBox4.Text += vbCrLf
        Next
        '*****************
        For i As Integer = 0 To arrayOfPlain.GetUpperBound(0)
            For j As Integer = 0 To arrayOfPlain.GetUpperBound(0)
                [COLOR=#ff0000]arrayOfPlain(0, 0) = plainarray(count)[/COLOR]
                count += 1
            Next
        Next

    End Sub

when i debug the code
it says that "Index was outside the bounds of the array"
textbox1 = 3
textbox2=ab...m
but i dont know why index count is outside bounds
any help ?
 
When debugger stops with that exception you can see what value count variable is, debugger can also tell you how large plainarray array is. Obviously count variable has a value that exceeds the size of the array, so you have to reevalute your code to see why that happens.
 

Latest posts

Back
Top