Results 1 to 12 of 12

Thread: [RESOLVED] VB6 QUESTION: Quickest way to initialise an array

  1. #1

    Thread Starter
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,235

    Resolved [RESOLVED] VB6 QUESTION: Quickest way to initialise an array

    In VB6 we don't have:

    DIM myString as STRING = "Dumbledore"

    So we cannot initialise a string var without an extra assignment:

    DIM myString as STRING
    myString = "Dumbledore"

    I do try to initialise all my vars prior to use as I am told this is good practice especially when it comes to learning and preparing to use other languages. I do forget to do this a LOT.

    When I remember, I tend to DIM all my vars in a group first, then initialise them afterwards otherwise it can look messy.

    My question, some variable types are difficult to initialise. Specifically for arrays, do you have any easy or clever methods of initialising an array, do you do a simple initialise at startup then REDIM later? What is good practice for VB6?

    I want to adopt a good practice that will be suitable for VB6 if not the other languages that do it better. Practical advice preferred.
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  2. #2
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: VB6 QUESTION: Quickest way to initialise an array

    For Variant arrays you can use the Array function.

    I think that similar, specific type, functions can be added like:

    Code:
    Private Function ArrayLong(ParamArray Params()) As Variant
        Dim c As Long
        Dim a() As Long
        
        ReDim a(UBound(Params))
        For c = 0 To UBound(Params)
            a(c) = Params(c)
        Next
        ArrayLong = a
    End Function
    
    Private Function ArrayString(ParamArray Params()) As Variant
        Dim c As Long
        Dim a() As String
        
        ReDim a(UBound(Params))
        For c = 0 To UBound(Params)
            a(c) = Params(c)
        Next
        ArrayString = a
    End Function
    
    Private Function ArraySingle(ParamArray Params()) As Variant
        Dim c As Long
        Dim a() As Single
        
        ReDim a(UBound(Params))
        For c = 0 To UBound(Params)
            a(c) = Params(c)
        Next
        ArraySingle = a
    End Function
    
    Private Function ArrayByte(ParamArray Params()) As Variant
        Dim c As Long
        Dim a() As Byte
        
        ReDim a(UBound(Params))
        For c = 0 To UBound(Params)
            a(c) = Params(c)
        Next
        ArrayByte = a
    End Function
    
    Private Function ArrayDouble(ParamArray Params()) As Variant
        Dim c As Long
        Dim a() As Double
        
        ReDim a(UBound(Params))
        For c = 0 To UBound(Params)
            a(c) = Params(c)
        Next
        ArrayDouble = a
    End Function
    
    Private Function ArrayDate(ParamArray Params()) As Variant
        Dim c As Long
        Dim a() As Date
        
        ReDim a(UBound(Params))
        For c = 0 To UBound(Params)
            a(c) = Params(c)
        Next
        ArrayDate = a
    End Function
    Quick test:

    Code:
    Private Sub Form_Load()
        Dim l() As Long
        Dim s() As String
        Dim b() As Byte
        Dim d() As Date
        
        l = ArrayLong(1, 2, 3)
        s = ArrayString(1, 2, 3)
        b = ArrayByte(1, 2, 3)
        d = ArrayDate("1/2/3", "2/2/3", "3/2/3")
        
        Debug.Print l(1), TypeName(l(1))
        Debug.Print s(1), TypeName(s(1))
        Debug.Print b(1), TypeName(b(1))
        Debug.Print d(1), TypeName(d(1))
    End Sub

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 QUESTION: Quickest way to initialise an array

    It usually isn't an issue for most arrays. If you do have some large tables you need for something you can always compile them in as binary data resources and use CopyMemory to copy the resource into the array after allocating it.

  4. #4
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: VB6 QUESTION: Quickest way to initialise an array

    PS: not sure what the OP means by "quickest", quickest execution or quickest to write the code. I assumed the second meaning.

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: VB6 QUESTION: Quickest way to initialise an array

    What about ZeroMemory/FillMemory-API?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: VB6 QUESTION: Quickest way to initialise an array

    It's a shame we don't have some more direct way to fill arrays, other than what Eduardo put together. (And Eduardo, what you did is great. I'm just thinking of a way that'd be more like a direct CopyMemory of the data.)

    The only way I can think of to do that is to save our data into a Binary file or possibly in the Resources area, and both of those are inconvenient. It'd be nice if we had some way to do it directly in our source code. Just something like the following would be sooo nice:

    Code:
    int arr[] = {10, 20, 30, 40};
    Or, since we have dynamic arrays, something like:

    Code:
    Dim arr() As Long
    Redim arr(3)
    arr = {10&, 20&, 30&, 40&}
    You know? Maybe we could do something with a pipe. Hmm.

    EDIT: Upon further playing around, I don't think this pipe idea is workable. There's just no way to output a series of constant literals to a pipe (or file), such as: Put #1,, 1,2,3,4
    I almost want the old Data/Read statements. When was that? Back in PDS-Basic?
    Last edited by Elroy; Oct 19th, 2021 at 12:01 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  7. #7

    Thread Starter
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,235

    Re: VB6 QUESTION: Quickest way to initialise an array

    Of course I really meant quickest in the coding sense although I realise using a loop to initialise an enormous array might take time... REDIM

    I know it isn't an issue as such but it feels wrong to initialise all my normal variables and then just leave the recently DIMmed arrays to themselves.

    READ/DATA is no more? I haven't thought about that since QB45. It could have been of some use?

    The ZeroMemory/FillMemory-API sounds like a thing.
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  8. #8
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: VB6 QUESTION: Quickest way to initialise an array

    Quote Originally Posted by yereverluvinuncleber View Post
    I realise using a loop to initialise an enormous array might take time... REDIM
    Are you afraid of loops?
    Loops like this are fast.
    Loops are everywhere in the code even when you don't see them. How do you think that the VB Array function might work internally? Even a single CopyMemory must have a some kind of loop until it copies all the bytes.

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: VB6 QUESTION: Quickest way to initialise an array

    Eduardo, just building on your ideas, it can actually all be done with one procedure:

    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
        Dim la() As Long
        la = LetArray(1&, 2&, 3&, 4&)
        MsgBox la(0&) & "  " & la(1&) & "  " & la(2&) & "  " & la(3&)
    End Sub
    
    Public Function LetArray(ParamArray data()) As Variant
        ' The "TYPE" of the first element will determine the array "TYPE".
        ' If you're not sure, cast it to make sure, example: currArr = LetArray(CCur(1), 2, 3, 4)
        ' This puts the entire array into a single variant.
        ' Be SURE that the receiving dynamic array is of the same type as the first passed in element.
        '
        Dim y() As Long, b() As Boolean, t() As Date, i() As Integer
        Dim l() As Long, c() As Currency, f() As Single, d() As Double
        Dim s() As String, v() As Variant, ptr As Long
        Select Case VarType(data(0&))
        Case vbByte:         ReDim y(UBound(data)): For ptr = 0& To UBound(data): y(ptr) = data(ptr): Next: LetArray = y
        Case vbBoolean:      ReDim b(UBound(data)): For ptr = 0& To UBound(data): b(ptr) = data(ptr): Next: LetArray = b
        Case vbDate:         ReDim t(UBound(data)): For ptr = 0& To UBound(data): t(ptr) = data(ptr): Next: LetArray = t
        Case vbInteger:      ReDim i(UBound(data)): For ptr = 0& To UBound(data): i(ptr) = data(ptr): Next: LetArray = i
        Case vbLong:         ReDim l(UBound(data)): For ptr = 0& To UBound(data): l(ptr) = data(ptr): Next: LetArray = l
        Case vbCurrency:     ReDim c(UBound(data)): For ptr = 0& To UBound(data): c(ptr) = data(ptr): Next: LetArray = c
        Case vbSingle:       ReDim f(UBound(data)): For ptr = 0& To UBound(data): f(ptr) = data(ptr): Next: LetArray = f
        Case vbDouble:       ReDim d(UBound(data)): For ptr = 0& To UBound(data): d(ptr) = data(ptr): Next: LetArray = d
        Case vbString:       ReDim s(UBound(data)): For ptr = 0& To UBound(data): s(ptr) = data(ptr): Next: LetArray = s
        Case vbVariant:      ReDim v(UBound(data)): For ptr = 0& To UBound(data): v(ptr) = data(ptr): Next: LetArray = v
        End Select
    End Function
    
    And, as a caveat, there are several ways you can make any of this generate type mismatch runtime errors. But, if used correctly, it shouldn't generate errors.
    Last edited by Elroy; Oct 19th, 2021 at 03:07 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  10. #10
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: VB6 QUESTION: Quickest way to initialise an array

    Yes Elroy, that is another approach, with pros and cons.

  11. #11
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: VB6 QUESTION: Quickest way to initialise an array

    I'm still wondering if there's a way to have the compiler make binary data of a series of constant literals that could then be CopyMemoried into an array of the correct size.

    I guess the only thing that might do that would be an Enum. But that'd only work for longs.

    I suspect that the Array() function even uses the ParamArray mechanism to do its work.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  12. #12
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: VB6 QUESTION: Quickest way to initialise an array

    Quote Originally Posted by Elroy View Post
    I'm still wondering if there's a way to have the compiler make binary data of a series of constant literals that could then be CopyMemoried into an array of the correct size.

    I guess the only thing that might do that would be an Enum. But that'd only work for longs.
    I don't think so since the ParamArray is of Variants and they need to be converted to the proper type.

    Quote Originally Posted by Elroy View Post
    I suspect that the Array() function even uses the ParamArray mechanism to do its work.
    Probably (or the same in functionality). The Array() function has the advantage that it needs to return also an array of Variants, so may be it does something more direct.

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