Say I split a string(strTemp) into 5 parts as myStuff(a variant. So now I have myStuff(0 to 4), right? Is there a way to add on a myStuff(5) to the end?
Printable View
Say I split a string(strTemp) into 5 parts as myStuff(a variant. So now I have myStuff(0 to 4), right? Is there a way to add on a myStuff(5) to the end?
Sure thing. Here's a little example:
Code:<%
Dim strTemp
Dim arrTemp
strTemp = "s'f'd'g'f"
arrTemp = Split(strTemp, "'")
'Add new element to the array
ReDim Preserve arrTemp(UBound(arrTemp) + 1)
arrTemp(UBound(arrTemp)) = "My New String"
%>
knew there had to be a way, thanx!
An error says "Method or Data Member not found", what's up with that?
On what line? By the way are you using VB6 (InterDev6) ???
VB hightlights "ReDim Preserve myStuff" then the (UBound...
is left alone. I'm using VB6.0 Enterprise Edition.
Works for me...
You get that error when you try to redim a listbox right? (this is just a guess) Serge's code works only for arrays.
no, I used it to redim an array. Here's the thing:
when the combobox loses focus, the error comes upCode:Private Sub Combo1_LostFocus()
For x = 0 to Combo1.ListCount - 1
If(Combo1.Text = Combo1.List(x)) Then
matchFound = True
End If
Next x
if(matchFound = True) Then
Redim Preserve myStuff(UBound(myStuff) + 1)
myStuff(UBound(mystuff)) = "Whatever"
End If
End Sub
I think I know what the problem is. I think the array is not populated at all. So, of course, the line on Ubound will fail. Try checking if the array is dimmensioned before:
Code:If(matchFound = True) Then
On Error Resume Next
'This line will be true if array is populated
If IsNumeric(UBound(myStuff)) Then
Redim Preserve myStuff(UBound(myStuff) + 1)
Else 'Otherwise redim your array with the first element
Redim Preserve myStuff(0)
End If
myStuff(UBound(mystuff)) = "Whatever"
End If
Regards,
alright, wait, let's take two steps back.
Does the redim approach have to be used on something dimmed as an array, because I'm doing it on a variant that becomes an array.
BTW - The array is populated to start.
'try it this way
[code]
Option Explicit
Option Compare Text
Public mystuff
Private Sub Combo1_LostFocus()
Dim x As Integer, matchFound As Boolean
For x = 0 To Combo1.ListCount - 1
If (Combo1.Text = Combo1.List(x)) Then
matchFound = True
End If
Next x
If (matchFound = True) Then
ReDim Preserve mystuff(UBound(mystuff) + 1)
mystuff(UBound(mystuff)) = "Whatever"
MsgBox mystuff(5)
End If
End Sub
Private Sub Form_Load()
Dim strTemp
Dim i As Integer
strTemp = "s'f'd'g'f"
mystuff = Split(strTemp, "'")
For i = LBound(mystuff) To UBound(mystuff)
Combo1.AddItem mystuff(i)
Next i
Combo1.ListIndex = 1
End Sub
[code]
That one worked HeSaidJoe, now let me ask, can the variant be public on another form, or does it need to be dimmed on the form that its used?
ubound failure causes subscript out of range, serge, theres no way to avoid or check it but error handling
I think there's another possiblity, crptcblade, you have a class or standard module called mystuff, that would make the call ambigous, and it prefers the classname instead
Yes the variant can be public on another form, but you have to call it by Formname.Mystuff
sorry, kedaman, that's not an issue
the above post wasn't for your last post, kedaman, and I did use the format : formName.MyStuff
Hmm, is it really public?
eh sorry, now i get the problem!
What vb does is creates a property of the public variant, that means you can't use functions and statements like redim as it would be a variable, it's not, it's a propety.
What you need to do is store the variable in a temporray variant, redim that, and pass it back to the form
:) hope it works now :rolleyes:
cool, I'll give it a shot
Sorry, was off watching my Leafs clobber the Bruings.
If you make it public in a bas module it works.
ie.
if you have Public myStuff in a bas module
and then do your stuff on form1 and then open form2
and msgbox mystuff(5) you will get whatever.
Later.