Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic arrays 2

Status
Not open for further replies.

tg2003

IS-IT--Management
Feb 6, 2003
270
IL
Hi all,

How can I know if a dynamic array contains something?

I'm using it as follows:

Code:
dim strings() as string
dim i as integer

for i = ....
  if something...
    ReDim Preserve strings(i) As String
  end if
next i

' Here I want to know if strings() contains something

since it's conditioned, so I can't know if something is added (preserved) to the array.

at the end of this flow, I would like to check it (see comment inside the code). I tried ubound, lbound, isarray and isempty function, and none of them returns what I need.
Currently I'm using a boolean variable that indicates it to me, but I would like to know if there is some better way to find it.

Thanks in advance!
 
No problem. We've covered this on a number of occassions in this forum, and there are several different solutions. Most of them are mentioned or described in the following small sample of posts (and you'll get more insight into VB arrays than you might want ;-)): thread222-836312 (and the older variant of my code in that in thread222-443102)
thread222-1181777
 
I was wondering this same thing a week ago and based on what
I found posted in tek tips I decided to go the non api route, and made a function called "IsArrayGood" well I shouldnt say I came up with it but based on postings in tektips.
Here is some code to toss around.

Code:
Dim MyArray() As String


Private Sub Command1_Click()
    Dim AString As String
    BadArray MyArray
    'GoodArray MyArray
    
    If Not IsArrayGood(MyArray) Then: MsgBox "Array is Bad!": Exit Sub

    For x = 0 To UBound(MyArray)
        AString = AString & MyArray(x)
    Next

    MsgBox AString
End Sub


Public Function IsArrayGood(AnyArray() As String) As Boolean
    Dim TempVal As Long
    
    On Error GoTo ArrayErr
    
    TempVal = UBound(AnyArray)
    IsArrayGood = True
    Exit Function

ArrayErr:
    If Err.Number = 9 Then
        IsArrayGood = False
    End If
End Function

Public Sub BadArray(AnyArray() As String)
    'DO NOTHING
End Sub

Public Sub GoodArray(AnyArray() As String)
    ReDim AnyArray(0 To 5) As String
    
    AnyArray(0) = "AR"
    AnyArray(1) = "RA"
    AnyArray(2) = "Y "
    AnyArray(3) = "IS"
    AnyArray(4) = " GO"
    AnyArray(5) = "OD"

End Sub

[reading]
 
Thank you very much!

I see that the solutions are quite similar to mine, so I keep it as is.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top