String Array with Dynamic Size

I

Ivan Weiss

Okay here is one I cannot figure out. I need to declare an array to
hold the values for a row in my database that I am going to populate a
listview control with.

However, the code is in a class so I can re-use it for any table in my
database so the array length is variable at run time.

How can I declare a variable like this? Currently my code is the
following:

Public Sub fillListView(ByVal argControlForm As Object, ByVal argSql As
String)
Dim myConnection As New OleDbConnection(dbConnString)
Dim myCommand As New OleDbCommand()
Dim myDataReader As OleDbDataReader
Dim myRow() As String
Dim numCols As Integer
Dim i As Integer

With myCommand
.Connection = myConnection
.CommandType = CommandType.Text
.CommandText = argSql
End With

'argControlForm.ListView.clear()

Try
myConnection.Open()
myDataReader = myCommand.ExecuteReader

numCols = myDataReader.FieldCount - 1
While myDataReader.Read
For i = 0 To numCols
myRow(i) = myDataReader.GetString(i)
Next
argControlForm.ListView.Items.Add(New
ListViewItem(myRow))
End While
Catch
DisplayErrorMessage("clsDatabase:fillListView")
Finally
myConnection.Close()
End Try
End Sub

-Ivan
 
S

Sueffel

Ivan Weiss said:
Okay here is one I cannot figure out. I need to declare an array to
hold the values for a row in my database that I am going to populate a
listview control with.

However, the code is in a class so I can re-use it for any table in my
database so the array length is variable at run time.

How can I declare a variable like this? Currently my code is the
following:

Public Sub fillListView(ByVal argControlForm As Object, ByVal argSql As
String)
Dim myConnection As New OleDbConnection(dbConnString)
Dim myCommand As New OleDbCommand()
Dim myDataReader As OleDbDataReader
Dim myRow() As String
Dim numCols As Integer
Dim i As Integer

With myCommand
.Connection = myConnection
.CommandType = CommandType.Text
.CommandText = argSql
End With

'argControlForm.ListView.clear()

Try
myConnection.Open()
myDataReader = myCommand.ExecuteReader

numCols = myDataReader.FieldCount - 1
While myDataReader.Read
For i = 0 To numCols
myRow(i) = myDataReader.GetString(i)
Next
argControlForm.ListView.Items.Add(New
ListViewItem(myRow))
End While
Catch
DisplayErrorMessage("clsDatabase:fillListView")
Finally
myConnection.Close()
End Try
End Sub

-Ivan

You must ReDim the array for the dimensions you will need. So, before you
can use
myRow(i) you must set it's size, for example, ReDim myRow(numCols), or, if
you have information you need to save, ReDim Preserve myRow(numCols)

That Should Do it.
 
B

Bernie Yaeger

Hi Ivan,

There are several ways of dealing with this. First, you can redim, as you
have been told - simply get the current array length, increment for every
add, and redim preserve each time you add to it (before) so that it always
has one more element into which you can load your data.

Probably a better way - and less tedious - is to use an arraylist instead.
An arraylist increments itself so you never have to redimension the list.
It also has all of the same methods, even some that arrays don't have, so
working with it after the load is very easy.

HTH,

Bernie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top