Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Some newbie VB6 questions

  • 01-05-2003 9:24am
    #1
    Registered Users, Registered Users 2 Posts: 3,779 ✭✭✭


    I am creating a multidemensional array

    Dim mArray (1000000,10)

    First - do I have to define in stone the number of elements in a multidemsional array ?

    then later a fill a certain amount of the elements then attempt to check these elements later within a loop

    for each mRecord in mArray()
    then do stuff
    next mRecord

    ' used a simple for / next loop for this now, but still would to use a better loop method.

    now this seems to cycle over the full 1000000 elements regardless of if there is data within them.

    Is there a better loop to use ?

    also, in the lang I normal use there is a statement -

    if element = unset then leave endif

    which would take you out of the level of code - is there something simalar in VB6?

    please help this newbie :)


Comments

  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Originally posted by Ping Chow Chi
    First - do I have to define in stone the number of elements in a multidemsional array ?

    Look at ReDim.
    now this seems to cycle over the full 1000000 elements regardless of if there is data within them.

    Is there a better loop to use ?

    Well, you created an array with X entries, and then told it ot iterate over all X entries, so what else would you expect it to do :)

    A better way? Look at reDim, or keep track elsewhere of the highest index used to date in the array (unless, of course, you are not filling it sequentially from the start).

    if element = unset then leave endif

    which would take you out of the level of code - is there something simalar in VB6?

    Have a look at the Break statement and (I think) the keyword Nothing (although it may be a function IsNothing...been a while since I used VB).


  • Registered Users, Registered Users 2 Posts: 3,779 ✭✭✭Ping Chow Chi


    cheers bonkey, I'll have a look into ReDim


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Your unset thing seems to be the equivalent of:
    If element Is Nothing Then Exit For
    

    (VB uses Is to compare whether objects are the same as each other - i.e. two references to the same object - or are the same as Nothing - not set, null pointer, various other terms for there not being any object.
    If you try to use = to compare objects VB will look for default methods that return built-in types (or default methods that return objects with default methods that return built-in types, and so on) and then compare the results of calling those methods, this is unlikely to be what you want to do (and will always error with Nothing since it has no default methods).

    ReDim may well be your friend here bonkey suggests. Or it may be possible to re-think your method altogether. Creating an array of 10×1000000 variants = 160,000,000 bytes = 152.5 MB of memory (possibly more if strings are involved), plus a little extra VB uses for bounds-checking. Even with a well-spec'd machine that's going to be a pain and it would have to be a very well-spec'd machine for it not to require a hell of a lot of paging.

    It's also a good idea to never use Variants unless you need to. Variants are VBs default so
    Dim mArray (1000000,10)
    
    is the same as
    Dim mArray (1000000,10) As Variant
    

    Variants are large, and they force operations on objects they contain to be late-bound which increases the chance of error (because a lot of stuff that would be caught when compiling with early-bound calls would be allowed to get into the code and not error until runtime) and massively increases the time it takes to execute (especially important when iterating through a large collection of objects like here.

    If you do something like:
    [code]Dim mArray (1000000,10) As XXX[code]
    Then the code will be faster (generaly much faster) and only enough memory needed for an XXX will be used by each element

    With built-in types this is varies from 1 byte for a Byte up to a variable amount for a String, though they are always smaller than a Variant containing the same value.
    With object types this is 4 bytes for a pointer (though unlike in C, most of the time VB handles all the pointer stuff for you) plus a variable amount of extra size depending on the object.

    Still while explicit typing would help here 10,000,000 anything is going to be a pain.


Advertisement