Results 1 to 25 of 25

Thread: [RESOLVED] Dynamic array: has it been initialized?

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [RESOLVED] Dynamic array: has it been initialized?

    I'm not sure I've chosen a proper title for this thread, anyway, I want to test if a dynamic array has already been ReDim-ed. In other words, what should I replace '???' by?

    Code:
    Dim MyArray() As Integer
    ReDim MyArray(10)
    
    If ??? Then
        'Do this
    Else
        'Do that
    End If
    ??? means that the bold statement has been executed.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Dynamic array: has it been initialized?

    I'm sure there's a better way, but try this:

    Code:
        Dim MyArray() As Integer
        ReDim MyArray(10)
    
        On Error Resume Next
        MsgBox UBound(MyArray)
        If Err.Number <> 0 Then
            MsgBox Err.Description
        End If
    Then comment out the "ReDim" and run it again.

  3. #3

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Dynamic array: has it been initialized?

    Quote Originally Posted by vbfbryce View Post
    I'm sure there's a better way, but try this:

    Code:
        Dim MyArray() As Integer
        ReDim MyArray(10)
    
        On Error Resume Next
        MsgBox UBound(MyArray)
        If Err.Number <> 0 Then
            MsgBox Err.Description
        End If
    Then comment out the "ReDim" and run it again.
    Trapping the error, yes, there's always that, but I was thinking of something more elegant, you know, IsNull, IsNothing, IsEmpty, IsWhatNot or similar.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  4. #4

  5. #5

  6. #6
    Hyperactive Member Teseng's Avatar
    Join Date
    Sep 2007
    Location
    Tupelo, MS
    Posts
    281

    Re: Dynamic array: has it been initialized?

    I've used this, I've never ran into a problem with it. The Assert is there to prevent some buggy things from happening.


    Code:
    Function CheckArray(MyInt() As Integer) As Boolean
    Dim i As Long
    i = Not Not MyInt
    Debug.Assert App.hInstance
    If Not i = 0 Then
    CheckArray = True
    Else
    CheckArray = False
    End If
    End Function
    Usage:

    Code:
    Sub TestSub()
    Dim MyInt1() As Integer, MyInt2() As Integer
    ReDim MyInt2(3 To 5)
    MsgBox CheckArray(MyInt1) 'Return False, the Array IS NOT initialized
    MsgBox CheckArray(MyInt2) ' Return True, the array IS initialized
    End Sub

  7. #7
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Dynamic array: has it been initialized?

    Would this help?

    Code:
    Dim MyArray() As Integer
    ' ReDim MyArray(10)
    
    ' Check if Array is empty
    If (Not MyArray()) = -1 Then
        MsgBox "Array Empty/Not Dimensioned"
    Else
        'Do that
    End If

  8. #8

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Dynamic array: has it been initialized?

    Quote Originally Posted by MartinLiss View Post
    If 0 isn't valid in MyArray(0) then you can do this

    Code:
    If MyArray(LBound(MyArray)) = 0 Then
        MsgBox "empty"
    End If
    Gives "subscript out of range" because MyArray hasn't been initialized yet.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  9. #9

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Dynamic array: has it been initialized?

    Quote Originally Posted by Teseng View Post
    I've used this, I've never ran into a problem with it. The Assert is there to prevent some buggy things from happening.


    Code:
    Function CheckArray(MyInt() As Integer) As Boolean
    Dim i As Long
    i = Not Not MyInt
    Debug.Assert App.hInstance
    If Not i = 0 Then
    CheckArray = True
    Else
    CheckArray = False
    End If
    End Function
    Usage:

    Code:
    Sub TestSub()
    Dim MyInt1() As Integer, MyInt2() As Integer
    ReDim MyInt2(3 To 5)
    MsgBox CheckArray(MyInt1) 'Return False, the Array IS NOT initialized
    MsgBox CheckArray(MyInt2) ' Return True, the array IS initialized
    End Sub
    Well, it works, but what buggy things do you mean?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  10. #10

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Dynamic array: has it been initialized?

    Quote Originally Posted by Edgemeal View Post
    Would this help?

    Code:
    Dim MyArray() As Integer
    ' ReDim MyArray(10)
    
    ' Check if Array is empty
    If (Not MyArray()) = -1 Then
        MsgBox "Array Empty/Not Dimensioned"
    Else
        'Do that
    End If
    Yes it does!
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  11. #11
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Dynamic array: has it been initialized?

    Seems to me that you would have to check all the integer elements of the array. Only if all were 0 would it then be safe to ReDim it unless you purposely want to reset all of the elements to 0.
    Doctor Ed

  12. #12

  13. #13

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Dynamic array: has it been initialized?

    Quote Originally Posted by Code Doc View Post
    Seems to me that you would have to check all the integer elements of the array. Only if all were 0 would it then be safe to ReDim it unless you purposely want to reset all of the elements to 0.
    Well, I'm not sure I follow you. The idea is, some code must be skipped if the array hasn't been yet redim-ed and given values.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  14. #14
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: [RESOLVED] Dynamic array: has it been initialized?

    A strange use of "Not Not" that Tseng mentioned in post#6
    Code:
        Dim MyArray() As Integer
        
        If (Not Not MyArray) = 0 Then
            MsgBox "MyArray was Not Initialized"
        Else
            MsgBox "MyArray was Initialized with UBound = " & UBound(MyArray)
        End If
        
        ReDim MyArray(10)
        
        If (Not Not MyArray) = 0 Then
            MsgBox "MyArray was Not Initialized"
        Else
            MsgBox "MyArray was Initialized with UBound = " & UBound(MyArray)
        End If
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  15. #15

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Dynamic array: has it been initialized?

    Quote Originally Posted by RhinoBull View Post
    If you don't like the error trapping way (simplest and bulletproof) then try this:

    Determining Array Initialization State and Dimensions
    Thank you, I bookmark the site for possible future reference. However I like edgemels's solution in just one statement.

    Trapping the error is a matter of taste, offhand I sort of dislike it as it looks like you're mending some faulty code, but I imagine it's just a matter of getting used to it.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  16. #16
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: [RESOLVED] Dynamic array: has it been initialized?

    Quote Originally Posted by krtxmrtz View Post
    Trapping the error is a matter of taste, offhand I sort of dislike it as it looks like you're mending some faulty code, but I imagine it's just a matter of getting used to it.
    Nope, not in this case at least.

  17. #17

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Dynamic array: has it been initialized?

    Quote Originally Posted by anhn View Post
    A strange use of "Not Not" that Tseng mentioned in post#6...
    I assume

    If (Not MyArray()) = -1 Then

    is the same as

    If (Not Not MyArray) = 0 Then
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  18. #18

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Dynamic array: has it been initialized?

    Quote Originally Posted by RhinoBull View Post
    Nope, not in this case at least.
    Well, perhaps you've convinced me that error trapping is ok, still it takes one more statement (On Error Resume Next)
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  19. #19
    Fanatic Member DrUnicode's Avatar
    Join Date
    Mar 2008
    Location
    Natal, Brazil
    Posts
    631

    Re: [RESOLVED] Dynamic array: has it been initialized?

    Have your tried API SafeArrayGetDim?

    Code:
    Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long
    
    Private Type ProjectType
        ProjectCode As String
        Priority As Integer
    End Type
    
    Private Sub Form_Load()
       Dim l() As Long
       Debug.Print "IsArrayInitiaized l()", SafeArrayGetDim(l) > 0
       ReDim l(0)
       Debug.Print "IsArrayInitiaized l(0)", SafeArrayGetDim(l) > 0
       Debug.Print
       '
       Dim s() As String
       Debug.Print "IsArrayInitiaized s()", SafeArrayGetDim(s) > 0
       ReDim s(0)
       Debug.Print "IsArrayInitiaized s(0)", SafeArrayGetDim(s) > 0
       Debug.Print
       '
       Dim v() As Variant
       Debug.Print "IsArrayInitiaized v()", SafeArrayGetDim(v) > 0
       ReDim v(0)
       Debug.Print "IsArrayInitiaized v(0)", SafeArrayGetDim(v) > 0
       Debug.Print
       
       Dim p() As ProjectType
       Debug.Print "IsArrayInitiaized p()", SafeArrayGetDim(p) > 0
       ReDim p(0) As ProjectType
       Debug.Print "IsArrayInitiaized p(0)", SafeArrayGetDim(p) > 0
    End Sub

  20. #20
    Hyperactive Member Teseng's Avatar
    Join Date
    Sep 2007
    Location
    Tupelo, MS
    Posts
    281

    Re: Dynamic array: has it been initialized?

    Quote Originally Posted by krtxmrtz View Post
    Well, it works, but what buggy things do you mean?
    For some reason, the Not Not Method, does something to the array pointer, and unless you do the assert, it can cause some wacky things, such as "i = 1 + 1" will give the error "math too complex" The assert fixes it completely, i don't understand it completely, but it seems to work fine.

  21. #21

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Dynamic array: has it been initialized?

    Quote Originally Posted by DrUnicode View Post
    Have your tried API SafeArrayGetDim?
    ...
    I like it,

    I have used much the API function compilation at allapi
    but it's no longer maintained and up to date so, this function I didn't know about.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  22. #22
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: [RESOLVED] Dynamic array: has it been initialized?

    Not Not Arrayname

    This returns the address of the Safearray Descriptor used by VB to describe arrays. If an Array is uninitialised this pointer is null hence If Not Arrayname <> -1 Then initialised

    I don't think this feature is there by design and there is a bug. The bug only happens in the IDE and only in the first floating point calculation following the first use of Not Arrayname. The bug can be avoided by calling any internal class after the first use of Not Arrayname

    Debug.Assert App.hInstance '<-- this avoids the IDE only bug and does not compile
    W o t . S i g

  23. #23
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: [RESOLVED] Dynamic array: has it been initialized?

    For an in depth dicussion on this subject just a little over a year ago on another site, see...

    http://www.tek-tips.com/viewthread.cfm?qid=1495917

    Hope it helps...




    Good Luck
    Option Explicit should not be an Option!

  24. #24
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: [RESOLVED] Dynamic array: has it been initialized?

    I found this thread while researching the same issue as krtxmrtz. Though this thread is resolved, I just wanted to say that all contributors made this a stimulating read.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  25. #25
    PowerPoster
    Join Date
    Jan 2020
    Posts
    5,541

    Re: [RESOLVED] Dynamic array: has it been initialized?

    True,UsedTime2=0.0349 Ms--GetLbound
    True,UsedTime1=0.0494 Ms--API--CopyMemory--IsArrayReDims
    True,UsedTime2=0.0099 Ms--API--SafeArrayGetDim
    Code:
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Dest As Any, Src As Any, ByVal cb&)
    Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long
    
    Function GetLbound(array1 As Variant) As Boolean
        On Error Resume Next
        Dim a As Long
        a = LBound(array1)
        GetLbound = Err.Number = 0
    End Function
    
    Public Function IsArrayReDims(wArray As Variant) As Boolean
    '?????????
        Dim a As Long
        If IsArray(wArray) Then
            CopyMemory a, ByVal VarPtr(wArray) + 8, 4
            CopyMemory a, ByVal a, 4
        End If
    IsArrayReDims = a <> 0
    End Function
    
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        timeEndPeriod 1
    End Sub
    Function CheckArray(MyArray As Variant) As Boolean
    If (Not MyArray()) = -1 Then
    Else
        CheckArray = True
    End If
    End Function

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