954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array problem

Hi

I'm having some problems with an array
i have in the definitions

Dim x(4)() as string


then when it comes to my assignments I have

x(4)(ss) = (TextBox5.Text)


but it throws an exception (Null Reference)

Can anyone help me with this?

Thx in advance

FrodoBaggins
Newbie Poster
15 posts since Dec 2010
Reputation Points: 10
Solved Threads: 1
 

hello !
array in vb.net is defined like this

dim array(5) as string


hope this will work fine :P

Regards

waqasaslammmeo
Posting Pro in Training
472 posts since Aug 2011
Reputation Points: 38
Solved Threads: 82
 

Hi,

Thanks for your answer, but what i'm trying to do is define a two-dimentional array with the 2nd dimention as an undefined.

FrodoBaggins
Newbie Poster
15 posts since Dec 2010
Reputation Points: 10
Solved Threads: 1
 

try this may be this will help you


Regards

waqasaslammmeo
Posting Pro in Training
472 posts since Aug 2011
Reputation Points: 38
Solved Threads: 82
 
waqasaslammmeo
Posting Pro in Training
472 posts since Aug 2011
Reputation Points: 38
Solved Threads: 82
 

Hi,

I still can't quite figure it out:

Public Class Test
    Dim x(6)() As String
    
    Dim y As String
    Dim ss As Integer

    Private Sub KryptonCheckButton2_Click(sender As System.Object, e As System.EventArgs) Handles KryptonCheckButton2.Click

    End Sub

    Private Sub KryptonCheckSet1_CheckedButtonChanged(sender As System.Object, e As System.EventArgs) Handles KryptonCheckSet1.CheckedButtonChanged

    End Sub

    Private Sub KryptonGroupBox1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles KryptonGroupBox1.Paint

    End Sub

    Private Sub KryptonButton1_Click(sender As System.Object, e As System.EventArgs) Handles KryptonButton1.Click
        Dim ans As ans


        If KryptonRadioButton5.Checked = True Then
            ans = Test.ans.A1


        ElseIf KryptonRadioButton6.Checked = True Then
            ans = Test.ans.A2



        ElseIf KryptonRadioButton7.Checked = True Then
            ans = Test.ans.A3



        ElseIf KryptonRadioButton8.Checked = True Then
            ans = Test.ans.A4
        Else
            MsgBox("Error")
        End If

        x(0)(ss) = (KryptonTextBox1.Text)
        x(1)(ss) = (KryptonTextBox2.Text)
        x(2)(ss) = (KryptonTextBox3.Text)
        x(3)(ss) = (KryptonTextBox4.Text)
        x(4)(ss) = (KryptonTextBox5.Text)
        x(5)(ss) = (ans)

        MsgBox(x)
       
        ss = (ss + 1)
    End Sub

    Enum ans
        A1
        A2
        A3
        A4
    End Enum

    Private Sub Test_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ss = 1
    End Sub
End Class
FrodoBaggins
Newbie Poster
15 posts since Dec 2010
Reputation Points: 10
Solved Threads: 1
 

>>i'm trying to do is define a two-dimentional array with the 2nd dimention as an undefined.
See if this helps regarding the 2D Array.

Public Class Form1
    Private x(6, 0) As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        x(0, 0) = "test"
        MsgBox(x(0, 0))
        x(1, 0) = "test again"
        MsgBox(x(1, 0))
    End Sub
End Class
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

Hi,

When I tried to write to it the 2nd time (so 0, 1) it was outside the bounds of the array (IndexOutOfRange Exception)

FrodoBaggins
Newbie Poster
15 posts since Dec 2010
Reputation Points: 10
Solved Threads: 1
 

Try Private x(6, 5) As String and that should give you plenty more Indexes inside the bounds of the array.

codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

Hi,

I need an unlimited amount in the second dimention, so like a dynamic array, because as the user types something a corrosponding value has to be added, and I need there not to be a limit to the amount they can add to the array.

Thanks

FrodoBaggins
Newbie Poster
15 posts since Dec 2010
Reputation Points: 10
Solved Threads: 1
 
Public Class Form1
    Private x(6, 0) As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        reDimAndPreserveArray(1)
        MsgBox("x(" & x.GetUpperBound(0) & ", " & x.GetUpperBound(1) & ")") '// FOR.TESTING.
    End Sub

    Private Sub reDimAndPreserveArray(ByVal iNumberOfArraysToAdd As Integer)
        Static iCurrentNumberOfArrays As Integer = 0
        iCurrentNumberOfArrays += iNumberOfArraysToAdd
        ReDim Preserve x(6, iCurrentNumberOfArrays)
    End Sub
End Class

Click the btn a few times for testing and anytime you need to add more arrays, just use that Sub and specify the amount of arrays to add.
.btw: it will Preserve all the previous values for the array.
Hope this helps.:)

codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

Hi,

Thanks this solved the problem. I used the redim command on every button press.

Also is it possible to save an array to file as an array rather than a series of strings? I want to be able to read from it easily without preforming splitting operations.

Thanks

FrodoBaggins
Newbie Poster
15 posts since Dec 2010
Reputation Points: 10
Solved Threads: 1
 

Solving the problem caused another. Dam.n you God!:D

I personally have no idea how to save a 2D array to a file without having to loop thru all the arrays.
You should start a thread regarding this new issue and hopefully someone will be able to shine some light on this matter.

codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: