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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rediming dynamic arrays - problems with erased and defaults

Status
Not open for further replies.

stefanRusterholz

Programmer
Aug 24, 2001
19
0
0
CH
Hi

I'm programming in VB for about 2 weeks now and can't get around following obstacle:
I have got an array (public because it can grow up to a few MB) which is at start erased.

Question 1:
How can I test if that array is still erased or has got redimmed? At the moment I use the workaround of having a secondary (Boolean) Variable 'arrayIsEmpty' which is set to true if the array is erased and fals if not. Is there a better solution for that (something like is_empty(myArray) was great)

Question 2:
Is there a way to 'fill' an array with default values within a given range (e.g. myArray(3) to myArray(209) get filled with "hello")?

below is a sample of how I'm doing it at the moment:
Code:
 ' declaring variables
    dim arrayIsEmpty as Boolean
    dim max as integer, dim lastUBound as integer
    dim myArray() as string

 ' set variables to default
    erase myArray
    arrayIsEmpty = true
    max = 0
    lastUBound = -1

 ' do our stuff
    For i = bottom To top

     ' do some stuff here
     ' ...
     ' ...
     ' stuff done

     ' smartly increase array size
        If arrayIsEmpty Then
            arrayIsEmpty = False
            ReDim Preserve myArray(0)
        End If
        If entryAtIndex > UBound(myArray) Then ' NOTE 1: let's suppose entryAtIndex as here a value of 7 in first loop
            max = entryAtIndex
            ReDim Preserve myArray(max)
            If Abs(max - lastUBound) > 1 Then
                For j = (lastUBound + 1) To (max - 1)
                    myArray(j) = "myDefaultValue"
                Next j
            End If
            lastUBound = max
        End If
    Next i

I hope I made myself clear and someone is able to help me.
Thanks in advance

Stefan
 
Uhm, seems like UBound = -1 is not true for erased arrays. I get an error: "Index ausserhalb des gültigen Bereiches" (I think the adaequate english error message is "Subscript out of boundaries") when I do
Code:
    dim myArray() as String
    dim myUBound as Integer

    erase String
    myUBound = UBound(myArray)

BTW: I use VB 6.0
 
Don't give the arrary a type, Let it be a Variant. Slower but...
Dim MyArray()

When you want it empty.
[tt]redim MyArray(0)
Set MyArray(0) = Nothing [/tt]

When you want to know if it is empty
[tt] If MyArray(0) is Nothing then[/tt]





Wil Mead
wmead@optonline.net

 
Will,
If you tested this you would get "object required".
Neither MyArray(0) nor MyArray is an object.
Stay away from variants if you want to move to VB.Net more easily and you do not want to mess with Empty and NULL.
But if you must
Code:
Dim ary
   If ary = Empty Then
       ReDim ary(5)
   End If
   ....
   ary = Empty  ' instead of erase
   End If
Compare Code (Text)
Generate Sort in VB or VBScript
 
Johnn,

My bad. :-( Incomplete testing.

When did Empty come out? (Likes it)
How on earth did I miss it.

Wil Mead
wmead@optonline.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top