Results 1 to 5 of 5

Thread: [RESOLVED] Uninitialized Arrays

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    223

    Resolved [RESOLVED] Uninitialized Arrays

    Awhile ago I came across a neat trick to detect uninitialized arrays, e.g.


    Code:
    Dim foo() as string
    If XXX = -1 Then Debug.Print "Array is uninitialized."
    I can't remember what XXX is, I apparently didn't make a note of it in my Google Notebook of VBA tricks, and I can't remember which of the countless Excel apps I used this trick in.

    It was really short, neat but unintuitive. I remember that whatever the test was, the value was supposed to be -1. I could always error trap a UBound call, but I'd like to know what the trick was.

    Does anyone know?

    Thanks!
    Pete

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Uninitialized Arrays

    This is one way to check... the API way...

    Code:
    Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" _
    (ByRef saArray() As Any) As Long
    
    Sub Sample()
        Dim foo() As String
        If SafeArrayGetDim(foo) = 0 Then MsgBox "Array is uninitialized."
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Uninitialized Arrays

    Or maybe with the help of a helper function like this:
    vb Code:
    1. Sub Sample()
    2.     Dim foo() As String
    3.     If GetUbound(foo) = -1 Then Debug.Print "Array is uninitialized."
    4. End Sub
    5.  
    6. Function GetUbound(arr) As Integer
    7.     On Error Resume Next
    8.     GetUbound = -1
    9.     GetUbound = UBound(arr)
    10. End Function
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    223

    Re: Uninitialized Arrays

    Awesome trick! That's going right into my Google notebook!

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

    Re: Uninitialized Arrays

    Quote Originally Posted by Pradeep1210 View Post
    Or maybe with the help of a helper function like this:
    vb Code:
    1. Sub Sample()
    2.     Dim foo() As String
    3.     If GetUbound(foo) = -1 Then Debug.Print "Array is uninitialized."
    4. End Sub
    5.  
    6. Function GetUbound(arr) As Integer
    7.     On Error Resume Next
    8.     GetUbound = -1
    9.     GetUbound = UBound(arr)
    10. End Function
    The above trick has a flaw: Although not many people use it but it is legal for an array to have both Lbound() and Ubound() negative.

    You can declare or redim like this:
    Code:
    Dim foo(-5 to -1) as String
    or
    Code:
    Dim foo() as String
    '...
    Redim foo(-5 to -1)
    In these cases, GetUbound(foo) will return -1 even foo has been initialized.

    Instead of GetUbound(), perhaps this is better:
    Code:
    Function IsArrayInit(arr) as Boolean
        Dim n as Integer
        On Error Resume Next
        n = Ubound(arr)
        IsArrayInit = (Err = 0)
    End Function
    The XXX in the original question is:
    Code:
    If Not Not foo = -1 Then
    However, people said this method may cause some problem in VB6-IDE and Debug.Assert need to be used to fix.
    • 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

Tags for this Thread

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