Results 1 to 4 of 4

Thread: Clearing an array which doesn't have an upper bound

  1. #1
    Elvarg is offline VB.NET Forum Newbie
    .NET Framework
    Join Date
    Nov 2005
    Posts
    4
    Reputation
    0

    Question Clearing an array which doesn't have an upper bound

    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.

  2. #2
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,362
    Reputation
    974
    waitlist.Clear
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  3. #3
    Elvarg is offline VB.NET Forum Newbie
    .NET Framework
    Join Date
    Nov 2005
    Posts
    4
    Reputation
    0
    Quote Originally Posted by JuggaloBrotha
    waitlist.Clear
    Argument not specified for parameter 'length' of Public Shared Sub Clear(array as System.Array, index as Integer, length as Integer)

  4. #4
    jmcilhinney's Avatar
    jmcilhinney is online now VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    12,746
    Reputation
    1618
    There is no such thing as an array without an upper bound. An array is a reference type so when you do this:
    Code:
    Dim waitlist() As String
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking