Results 1 to 16 of 16

Thread: [RESOLVED]UBound giving subscript out of range

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    21

    Resolved [RESOLVED]UBound giving subscript out of range

    I have an array
    Dim A() As String

    this is initially not initialized so I do this in a function (this function is called multiple tiles)
    Max = Ubound(A)

    If Max = 0 Then
    Redim A(Max+1,Max+10) As String
    End IF

    This UBound gives me a error saying Subscript out of range. How can I check if a array is initialized previously or not. It works if the array had some value but fails first time.
    Last edited by mysticmusician; Feb 20th, 2005 at 12:05 PM.

  2. #2
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: UBound giving subscript out of range

    work around this
    VB Code:
    1. If (Not A) = -1 Then
    2.  'not been initialized
    3. End If

    casey.

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: UBound giving subscript out of range

    Quote Originally Posted by vbasicgirl
    work around this
    VB Code:
    1. If (Not A) = -1 Then
    2.  'not been initialized
    3. End If

    casey.
    That won't work at all - NOT A has nothing to do with the ARRAY of A.

    Simply change your logic a small bit:

    Code:
    Dim A() as String
    Redim A(0,0)
    
    ...then later on...
    
    Max = UBound(A) ' Note that UBound needs to specify which dimensional of the arry - if two dimensional

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: UBound giving subscript out of range

    i answered the question

    How can I check if a array is initialized previously or not.

    casey.

  5. #5

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: UBound giving subscript out of range

    Code:
    Private Sub Form_Load()
    
    Dim a() As String
    
    If Not a Then Debug.Print "A is not initialized"
    
    ReDim a(1, 1)
    
    If Not a Then Debug.Print "A is still not initialized - since NOT A does not check for that!"
    
    End Sub
    [edit] - please note that VBasicGirl's example does in fact work.
    Last edited by szlamany; Feb 20th, 2005 at 11:21 AM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: UBound giving subscript out of range

    are you saying this doesnt work for you ?
    VB Code:
    1. Dim A() As String
    2.  
    3. If (Not A) = -1 Then
    4.  Debug.Print "array as not been initialized"
    5. Else
    6.  Debug.Print "array as been initialized"
    7. End If
    8.  
    9. ReDim A(1)
    10.  
    11. If (Not A) = -1 Then
    12.  Debug.Print "array as not been initialized"
    13. Else
    14.  Debug.Print "array as been initialized"
    15. End If

    casey.

  8. #8
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: UBound giving subscript out of range

    i found the original explanation.


    If a dynamic array has not been redimmed, or has been erased, then it does not yet point to a SAFEARRAY structure. Unfortunately, you cannot test if the arrayname = 0. But, you can binary NOT it and make a comparison on the result.

    (Not 0) = -1 ' not redimmed or erased
    (Not SomePointer) <> -1 ' redimmed

    Why MS didn't include a simple function IsRedimmed, I'll never understand.


    casey.

  9. #9
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: UBound giving subscript out of range

    i dont know if this is allowed, a link to the forum thread i found it.

    delete if not allowed.

    http://www.xtremevbtalk.com/showthre...ght=impressive

    casey.

  10. #10
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: UBound giving subscript out of range

    Quote Originally Posted by vbasicgirl
    i found the original explanation.


    If a dynamic array has not been redimmed, or has been erased, then it does not yet point to a SAFEARRAY structure. Unfortunately, you cannot test if the arrayname = 0. But, you can binary NOT it and make a comparison on the result.

    (Not 0) = -1 ' not redimmed or erased
    (Not SomePointer) <> -1 ' redimmed

    Why MS didn't include a simple function IsRedimmed, I'll never understand.


    casey.
    Wow - I must admit that I was very, very wrong - your example does in fact work. What an odd technique to arrive at an array having been already dimensioned!

    I still stand by my method though - simply REDIM that array immediately and no need to check for whether it's been initialized yet. But that's just my personal preference.

    Also, I was wrong to state that A() and A where two different variables. That's my confusion from other BASIC/machine platforms I've coded on - that do allow for an A() array and an A variable - unrelated to each other.

    Casey - my apologies

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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

    Re: UBound giving subscript out of range

    Best way for me is as follows:
    VB Code:
    1. Option Explicit
    2.  
    3. Dim arText() As String
    4. Dim arNumbers As Integer
    5.  
    6. Private Sub Form_Load()
    7.     Redim arText(0)
    8.     arText(0) = "DUMMY"
    9.     Redim arNumbers(0)
    10.     srNumbers(0) = - 999
    11. End Sub
    So, all you really need to do is check if first element is a dummy entry and if YES = change it, if NOT Redim your array. Never failed this logic ...

  12. #12
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: UBound giving subscript out of range

    RhinoBull - Casey was right - I argued from a position that wasn't very valid!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  13. #13
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: UBound giving subscript out of range

    for the record, i do agree with you on redimming it firstly, i was just explaining on how you can check.

    szlamany - thank you.

  14. #14
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: UBound giving subscript out of range

    Quote Originally Posted by vbasicgirl
    for the record, i do agree with you on redimming it firstly, i was just explaining on how you can check.

    szlamany - thank you.
    Casey - you are very welcome.

    BTW - Whenever we use REDIM (which is infrequently), we like to do it in big jumps - like double the array size each time. When you use REDIM PRESERVE you are moving potentially large amounts of memory around in the process - and based on our old-school thoughts on memory manipulation, we avoid that.

    We always have a "long" variable that keeps track of the current position the array is built up to so far - I think that others might use a concept of checking UBOUND and up'ing the limit by 1 to fit another entry in. That's something we avoid.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    21

    Re: UBound giving subscript out of range

    What I ended up doing is this

    Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long

    Then
    If (SafeArrayGetDim(EmailArray) > 0) Then
    Array initialized
    Else
    Not Initialized !
    End IF

    This works perfect.

  16. #16

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