CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Arrays

  1. #1
    Join Date
    Feb 2000
    Location
    Germany
    Posts
    2

    Arrays

    Is there a possiblity to check wether a dynamic array is initialized or not. (the empty function does not work)

    Example:

    option explicit
    public h() as integer


    private sub tes

    redim h(1 to 10)
    if h "is initialized" then
    msgbox "Hello"

    erase h
    if h "is initialized" then
    msgbox "Nope" 'this mustn't occur!!!




    end sub





  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Arrays

    What exactly do you want to aceive?

    Do you want that your array is empty after the redim, or do you want the array to hold it's previous values?

    If you use redim, all the values are erased, and the array will be empty, also, whenever you declare an array (using dim or redim) the array is 'initialized', that is, the array will be empty (unless specified else) and you will be able to address an element of the array (unlike in java).

    Hope this makes it a bit more clear to you.

    Tom Cannaerts
    [email protected]

    The best way to escape a problem, is to solve it.
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    Feb 2000
    Location
    Germany
    Posts
    2

    Re: Arrays

    Back to our example:

    Let us think that the dynamic array f can vary like that.
    .
    .
    redim f(1 to 10)
    call test()
    .
    .
    erase f
    'any attempt like f(1) = 234 ends up in an error message
    'so i want my my sub routine to check wether f
    'is "dimensioned/initialized" or erased!
    call test



    option explicit
    private f() as integer


    private sub test()


    end sub



  4. #4
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: Arrays

    this is closest i can do to what you're looking for:


    option Explicit
    Dim s() as string

    private Sub Form_DblClick()
    Erase s
    call tester
    End Sub

    private Sub Form_Load()
    ReDim s(1 to 10)
    Call tester
    End Sub

    Sub tester()
    on error GoTo handler
    If Not UBound(s) then
    MsgBox "valid array"
    else
    MsgBox "invalid array"
    End If

    Exit Sub

    handler:
    If Err.Number = 9 then 'subscript out of range
    MsgBox "the array is empty/non-initialized."
    else
    MsgBox error
    End If

    End Sub




    You can easily tell if the array is just empty by checking the ubound property (0 means there are no elements in the array).

    Hope this helps,
    John


    John Pirkey
    MCSD
    www.ShallowWaterSystems.com
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured