|
-
Feb 20th, 2005, 03:55 AM
#1
Thread Starter
Junior Member
[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.
-
Feb 20th, 2005, 05:24 AM
#2
Re: UBound giving subscript out of range
work around this
VB Code:
If (Not A) = -1 Then
'not been initialized
End If
casey.
-
Feb 20th, 2005, 09:37 AM
#3
Re: UBound giving subscript out of range
 Originally Posted by vbasicgirl
work around this
VB Code:
If (Not A) = -1 Then
'not been initialized
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
-
Feb 20th, 2005, 09:40 AM
#4
Re: UBound giving subscript out of range
i answered the question
How can I check if a array is initialized previously or not.
casey.
-
Feb 20th, 2005, 10:25 AM
#5
Re: UBound giving subscript out of range
 Originally Posted by vbasicgirl
work around this
VB Code:
If (Not A) = -1 Then
'not been initialized
End If
casey.
Sorry, casey, but it's a total absurd ...
-
Feb 20th, 2005, 10:26 AM
#6
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.
-
Feb 20th, 2005, 10:46 AM
#7
Re: UBound giving subscript out of range
are you saying this doesnt work for you ?
VB Code:
Dim A() As String
If (Not A) = -1 Then
Debug.Print "array as not been initialized"
Else
Debug.Print "array as been initialized"
End If
ReDim A(1)
If (Not A) = -1 Then
Debug.Print "array as not been initialized"
Else
Debug.Print "array as been initialized"
End If
casey.
-
Feb 20th, 2005, 10:51 AM
#8
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.
-
Feb 20th, 2005, 10:56 AM
#9
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.
-
Feb 20th, 2005, 11:24 AM
#10
Re: UBound giving subscript out of range
 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
-
Feb 20th, 2005, 11:24 AM
#11
Re: UBound giving subscript out of range
Best way for me is as follows:
VB Code:
Option Explicit
Dim arText() As String
Dim arNumbers As Integer
Private Sub Form_Load()
Redim arText(0)
arText(0) = "DUMMY"
Redim arNumbers(0)
srNumbers(0) = - 999
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 ...
-
Feb 20th, 2005, 11:27 AM
#12
Re: UBound giving subscript out of range
RhinoBull - Casey was right - I argued from a position that wasn't very valid!
-
Feb 20th, 2005, 11:30 AM
#13
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.
-
Feb 20th, 2005, 11:36 AM
#14
Re: UBound giving subscript out of range
 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.
-
Feb 20th, 2005, 12:04 PM
#15
Thread Starter
Junior Member
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.
-
Feb 20th, 2005, 07:45 PM
#16
Re: UBound giving subscript out of range
 Originally Posted by szlamany
RhinoBull - Casey was right - I argued from a position that wasn't very valid!
I will dissagree again - in the way it was presented it was a complete nonsense.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|