|
-
Jan 25th, 2013, 08:52 AM
#1
Thread Starter
Junior Member
[RESOLVED] Dynamic array related question
Hi folks!
I know how to work with fixed arrays, but I would like to make it more clear and ask a question regarding fixed arrays. For example, what's the difference when declaring:
Code:
Dim str(0 to 100) as string
and
Code:
Dim str (100) as string
Is it just for specify the lower limit of the array or there is another explanation?
The main question is about Dynamic arrays. I usually work with Winsock and I never know the data arrival how many strings will provide. For example:
Code:
Dim str() as string
Then when doesn't know the dimension, how to allocate the actual number of elements with a ReDim statement??
Redim str(???) as string?? Or should i write the supposed value for it? That every time can exceed the limit?
Thanks in advance
-
Jan 25th, 2013, 09:01 AM
#2
Re: Dynamic array related question
Yes, whenever you need to dimension array you can use either Redim or Redim Preserve statement - kim that Redim Preserve may be slow depending on array size.
Redim clears the array sets new dimension and Redim Preserve keeps all current values and also sets new dimension.
Typically you would initialize array with one element Redim myArray(0) As String.
Every time you need to populate it you can use Ubound function:
myArray(Ubound(myArray)) = "abc"
Redim Preserve myArray(Ubound(myArray) + 1)
-
Jan 25th, 2013, 09:12 AM
#3
Re: Dynamic array related question
I have not worked with Winsock, so I don't know how the data (strings) look, and what the delimiters will be like.
But it is possible you could use one the useful features that VB6 introduced - Split
What that does is split the contents of a string variable into elements in an array. You have to tell it what the delimiter is that it will use to 'divide' the string into the elements of the array.
Here is an example -
Code:
Option Explicit
Private Sub Form_Click()
Dim sArr() As String
Dim sData As String
Dim i As Integer
sData = "AA, BB, CC, DD, EE, FF, GG"
sArr = Split(sData, ",")
For i = 0 To UBound(sArr)
Debug.Print Trim(sArr(i))
Next i
End Sub
-
Jan 25th, 2013, 09:19 AM
#4
Re: Dynamic array related question
And if you need a different lower bound value Redim accepts that too.
For these sorts of questions you ought to check the manual first. There are lots of factors involved and nobody is going to cover them exhaustively in a forum post.
-
Jan 25th, 2013, 09:23 AM
#5
Thread Starter
Junior Member
Re: Dynamic array related question
Dear Bobbles
I'm talking about Chat clients, thay get every time new data, like users, direct messages or main screen messages. So, the split function helps me to divide username and its icon and put them on listview. For example, a listview that has one subitem for usernames and other one displaying icons of each user. So, for the chat messages and users, I need to preserve data and each time appears new message, add this message to the existing data without losing data
Dear RhinoBull
Thanks a lot, that' what I need exactly! Much appreciated
Well dilettante
If i don't read the tutorial I would never know the type of arrays and their composition. But sometimes the tutorials doesn't contain everything. I may read 1, 2, 3 and get an idea but always is better when someone is able to explain all doubts within a simple and clear post like RhinoBull did.
Thanks!
Last edited by david_707; Jan 25th, 2013 at 09:27 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|