473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

String Array with Dynamic Size

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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #1
2 1351

"Ivan Weiss" <iv*****@optonline.net> wrote in message
news:ur**************@TK2MSFTNGP11.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


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.
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.538 / Virus Database: 333 - Release Date: 11/10/2003
Nov 20 '05 #2
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

"Ivan Weiss" <iv*****@optonline.net> wrote in message
news:ur**************@TK2MSFTNGP11.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an...
2
by: James | last post by:
Hi, I'm hoping someone can help me out. If I declare a class, eg. class CSomeclass { public: var/func etc..... private varfunc etc..
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
10
by: Xinyi Yang | last post by:
Hi, I have to read information out of a file. The format will be string1,string2,..., string3,string4,..., .... (the string sould not contain ' ' anyway) the size of each string is uncertain....
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change...
10
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char*...
10
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! Got a simple question. I am new to c# but this is not making me any sence. If i declare: string myStringArray = new string; How the heck could i fill it with more than one element? ...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.