How can I get length of 2d array?

trend

Centurion
Joined
Oct 12, 2004
Messages
171
I setup an array like:

Visual Basic:
dim test(,) as string
test = functionname("test")

msgbox(test.length)

and hten I get error:

An unhandled exception of type 'System.NullReferenceException' occurred in Trigger v2.exe

Additional information: Object reference not set to an instance of an object.


Why? I know there are 10 or 20 some records that should be returned by functioname("test").

I have tried test.length(1).. and a lot of other test.* ones..

But seem to get the same errror above.. Why?

thanks!
 
PlausiblyDamp said:
It looks as though the function isn't returning an array hence the null reference. Could you post the relevant code from the method in question?

Visual Basic:
Private Function test(ByVal SP As String, ByVal db As String)
        Dim SafeUsername As String = "whaetver"
        Dim SafePassword As String = "password"
        Dim PendingAccounts(,) As String

        Dim MyConnection As New OleDbConnection("Provider=SQLOLEDB.1;User Id=" & SafeUsername & ";Password=" & SafePassword & ";Initial Catalog=" & db & ";server=servername.com;Use Encryption for Data=False")

        Dim myCmd As New OleDb.OleDbCommand(SP)
        Dim myReader As OleDb.OleDbDataReader


        myCmd.Connection = MyConnection
        myCmd.CommandType = CommandType.StoredProcedure



        Try
            MyConnection.Open()
            myReader = myCmd.ExecuteReader()



            ' If myReader.Read Then '= True Then
            Dim X As Integer = 0
            Do While myReader.Read()
                PendingAccounts(X, 0) = myReader.GetString(0) 
                PendingAccounts(X, 1) = myReader.GetString(1) 
                PendingAccounts(X, 2) = myReader.GetInt32(2).ToString
                PendingAccounts(X, 3) = myReader.GetString(3)
                X = X + 1

            Loop
            'End If

            myReader.Close()
            MyConnection.Close()
        Catch

            MsgBox( Err.Description)
            End Try
        TextBox1.Text = TextBox1.Text & "Done!"

        Return PendingAccounts
    End Function
Yeah, you are right.. this function isn't working right.. my msgbox(err.description) is throwing a similiar error .. null references or something of the like..

Am I utilizing the array wrong?

thanks!
 
You need to instantiate the array before you can use it, effectively that means you know how many elements it will hold when you create it. Otherwise you could use the RedDim command to resize it, although that can result in poor performance.

You might be better of creating a class or structure to hold the 4 values you are storing for each row, and then creating an ArrayList of these. Also if you are using .Net 2 then a List<T> would be even better.
 
Back
Top