Results 1 to 15 of 15

Thread: Empty an array

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    8

    Talking Empty an array

    Hello,

    I'm looking for a way to empty a global variable, declared as an array of a user-defined type, and this several times in the program

    Example:

    Type BSCSCustomer
    customer_id As Long
    MatchCustomerNumber As Integer
    MatchInvoice As Integer
    MatchExternalReference As Integer
    MatchGSMNumber As Integer
    MatchAccountNumber As Integer
    MatchName As Integer
    MatchAddress As Integer
    End Type

    Global PossibleCust (0 To 200) As BSCSCustomer

    I already tried REDIM PRESERVE, but I get the compile error 'Array already Dimensioned'. The goal is to be sure that the variable is empty before I re-use it.

    Thanks!!

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Erase ARRAYNAME
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    8
    I tried it but it works only on procedure level.
    It concerns a global variable used in whole the program.

  4. #4
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    You can only erase a dynamic array.

    When the array is declared as:
    Global PossibleCust (0 To 200) As BSCSCustomer

    Erase PossibleCust ' will produce an error

    But when the array is declared like this:

    Global PossibleCust () As BSCSCustomer
    ReDim PossibleCust(0 to 200)

    You can erase the array

    Erase PossibleCust ' will produce no error anymore

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    8

    Smile

    Works perfectly, Thanks!

  6. #6
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Originally posted by Frans C
    You can only erase a dynamic array.

    When the array is declared as:
    Global PossibleCust (0 To 200) As BSCSCustomer

    Erase PossibleCust ' will produce an error

    But when the array is declared like this:

    Global PossibleCust () As BSCSCustomer
    ReDim PossibleCust(0 to 200)

    You can erase the array

    Erase PossibleCust ' will produce no error anymore
    Damn, learn something new everyday...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  7. #7
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    I have to correct myself (sorry James, you were correct )

    You can erase a fixed dimensioned array, but it doesn't have the same effect as erasing a dynamic array. Erase reinitializes the elements of fixed-size arrays and releases dynamic-array storage space.

    When the array is declared as:
    Global PossibleCust (0 To 200) As BSCSCustomer

    Erase PossibleCust

    This will reset all elements to the defaults for it's variabletype (integers to 0, strings to "" etc.) , but the array is still dimensioned from 0 to 200

    But when the array is declared like this:

    Global PossibleCust () As BSCSCustomer
    ReDim PossibleCust(0 to 200)

    Erase PossibleCust

    Now the array is completely empty, you have to redimension it before you can use it.


    Damn, learn something new everyday...

  8. #8
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    sorry James, you were correct
    Woo-hoo. That always nice to hear, but at least you figured it out...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  9. #9
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Empty an array

    What am I doing wrong?
    I have a bunch of arrays that are all declared as
    VB Code:
    1. global xx(9) as single

    Why is it that when I erase them using

    VB Code:
    1. erase xx()

    Most of the arrays are emptied, which is what I want.
    But a few are erased to return values of zero which is not what I want.

    I don't see why they are being treated differently.
    Last edited by sgrya1; Sep 25th, 2006 at 03:58 AM.

  10. #10
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Empty an array

    well, erasing a fixed dimension array shouldn't empty it (i.e. remove all the members) only reset the values to their default.

    without more code it's hard to say what's going on.

  11. #11
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Empty an array

    String arrays go fully empty, but with numeric datatype arrays the values are made zero. This is actually what happens to string arrays as well, but with strings zeros are treated as null strings. By default the actual string array value for an item is a pointer to the actual string data in memory. You just can't see this directly in VB6.

    If you are to handle numeric data, then you should retain the numeric datatype you use. If you use numbers as strings, you'll get unexpected problems in some cases.

  12. #12
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Empty an array

    Attached is a very simple vb project which highlights my problem. Can someone please explain why the two variables get treated differently and how I can get them all to operate in the same way.

    Hope it's not just the version of VB6.0 that I'm using.
    One variable gets emptied while the other sets to 0.
    Wierd thing is that when I remove all the other arrays and just leave the two then they both get set to 0.
    Attached Files Attached Files
    Last edited by sgrya1; Sep 25th, 2006 at 05:14 AM.

  13. #13
    Lively Member
    Join Date
    Aug 2005
    Posts
    77

    Re: Empty an array

    It has to do with the way you declared the arrays. Some arrays you specify with a type, others not. The correct way to do it is:
    VB Code:
    1. Global LeftY1_(9) As Single, LeftY2_(9) As Single, ..., BottomX2_(9) As Single
    instead of
    VB Code:
    1. Global LeftY1_(9), LeftY2_(9) , ..., BottomX2_(9) As Single
    Only BottomX2_ is of the type Single.

    Pieter

  14. #14
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Empty an array

    That's been killing me. Thankyou.
    I take it that the array defaults to string because you can empty it?
    Don't know what As Single is yet anyway.

  15. #15

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