Clearing an array which doesn't have an upper bound

Elvarg

New member
Joined
Nov 29, 2005
Messages
4
Programming Experience
Beginner
I'm writing a small program which has the 1-dimensional upperboundless array waitlist() declared class-level. It needs to be able to store an arbitrary amount of members (string datatype), from 0 to however many the user enters. I need to write a sub procedure that deletes all members of the array.

I tried the following sub:

So I have:

Public Class Form1
Inherits System.Windows.Forms.Form
...
Dim waitlist() As String
...
Sub ResetWaitList(ByRef waitlist() As String)
waitlist(100)
...
End Sub

It gives me the error "ReDim statement requires a parenthesized list of the new bounds of each dimension in the array". While I can dim a boundless array, it seems I cannot redim one.

I also tried the following:


Public Class Form1
Inherits System.Windows.Forms.Form
...
Dim waitlist() As String
...
Sub ResetWaitList(ByRef waitlist() As String)
Erase waitlist
Dim waitlist() As String

End Sub


Which then gives me the error "'waitlist' is already declared as a parameter in this method", even though I just erased.


How can I empty/erase/dim/redim/whatever a class-level boundless array from a sub procedure?


Thanks.
 
There is no such thing as an array without an upper bound. An array is a reference type so when you do this:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] waitlist() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
you are creating a variable that can refer to a String array but you are not creating an Aray object. You use ReDim to specifically set the upper bound of an array. What it actually does is create a new array object and assign a reference to that object to the variable. You cannot "clear" an array. If an array exists then it has the specific number of elements that you gave it. You can set each of those elements to a null reference but the elements themselves still exist. It sounds to me like what you actually want is a collection. You can use the ArrayList class, which is like an array in some ways but you can add and remove items dynamically. If you are specifically using String objects though, I would recommend that you use the Specialized.StringCollection instead. If you're using VB 2005 you could utilise Generics instead and use a List Of String.
 
Back
Top