Announcement

Collapse
No announcement yet.

Check If Array Is Set

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Check If Array Is Set

    Hello.
    I would like to know if there is a straight forward way to check if dynamic array is set (initialized)?
    Is Nothing doesnt work with arrays and I dont want to use On Error GoTo becouse I dont like making jumps inside code.
    Sorry for this simple question but there is nothing about this in book i use and I couldnt find it in this forum.
    Last edited by Thor; April 29th, 2007, 16:17.
    Get custom programming done at GetAFreelancer.com!

  • #2
    Re: Check If Array Is Set.

    Originally posted by Thor
    Hello.
    I would like to know if there is a straight forward way to check if dynamic array is set (initialized)?
    Is Nothing doesnt work with arrays and I dont want to use On Error GoTo becouse I dont like making jumps inside code.
    Sorry for this simple question but there is nothing about this in book i use and I couldnt find it in this forum.
    How about

    IsArray function?

    Comment


    • #3
      Re: Check If Array Is Set.

      Is array checks if variable is array. It doesnt check if it was initialized.

      Code:
       
      Dim myArray() as String
      msgbox IsArray( my Array )
      This will return true.
      Get custom programming done at GetAFreelancer.com!

      Comment


      • #4
        Re: Check If Array Is Set.

        I just want to know why do you need this?
        Because, if you declared an array variable, it is already there as an array.

        Comment


        • #5
          Re: Check If Array Is Set.

          Well I need dynamic array.
          So I dont set it size at the beginning because well I don't know how big array I need.
          Then when I populate array I need position of last element. But if its not initialize UBound will throw error.

          Of course I can get around this in few ways but having some straight forword way to check if array is initialized would simplyfie code (a little).
          Plus I thought that this should be possible but I just couldn't find it so I just wanted to know.
          But if this is not possible in VBA then no problem, really.
          It is just bugging me.
          Get custom programming done at GetAFreelancer.com!

          Comment


          • #6
            Re: Check If Array Is Set.

            Hi

            Could you test to see if the Ubound of the array is 0? Eg:

            Code:
            Sub test()
            Dim myArray() As String, l As Long
            On Error Resume Next
            l = UBound(myArray)
            On Error GoTo 0
            Debug.Print l
            End Sub

            Comment


            • #7
              Re: Check If Array Is Set.

              Some googling returned this suggestion by C Pearson.
              Sub x()

              Dim strX() As String

              If (Not Not strX) <> 0 Then
              MsgBox "Allocated"
              Else
              MsgBox "Unallocated"
              End If

              ReDim strX(3) As String

              If (Not Not strX) <> 0 Then
              MsgBox "Allocated"
              Else
              MsgBox "Unallocated"
              End If

              End Sub

              Cheers
              Andy

              Comment


              • #8
                Re: Check If Array Is Set.

                Andy,

                great hint!!!

                filippo

                Comment


                • #9
                  Re: Check If Array Is Set

                  Hehe. Works. Great.
                  Thanks Andy Pope.
                  I tested many things but (Not Not) , strange.
                  Get custom programming done at GetAFreelancer.com!

                  Comment

                  Working...
                  X