Is there any way to detect that an array has not been yet dynamically sized. If I run into LBound() before a Redim, I get subscript out of range error and I want to eliminate this error (and I don't want to do error handling or any other "tricks"). Is there any legal keyword or command like if array = nothing then etc... ???

Thanks !

Re: Detecting non-dimensioned array by Zoury

Zoury
Thu Apr 29 13:40:34 CDT 2004

Hi! :O)

you could trap the error :
http://vbnet.mvps.org/code/helpers/isbounded.htm


or peek at the behind-the-scene SAFEARRAY structure :
http://vbnet.mvps.org/code/helpers/getarraydims.htm

--
Best Regards
Yanick Lefebvre
"dr. bandicoot" <anonymous@discussions.microsoft.com> wrote in message
news:58E34BBC-4C36-4958-82FD-56AA668A96BF@microsoft.com...
> Is there any way to detect that an array has not been yet dynamically
sized. If I run into LBound() before a Redim, I get subscript out of range
error and I want to eliminate this error (and I don't want to do error
handling or any other "tricks"). Is there any legal keyword or command like
if array = nothing then etc... ???
>
> Thanks !



Re: Detecting non-dimensioned array by Rick

Rick
Thu Apr 29 13:52:19 CDT 2004

You shouldn't think of "errors" as being something bad; rather think of
it as information in another format. There is nothing wrong with
trapping errors and reacting to them, nothing at all.

I've posted this in the past...

Function IsArrayBounded(ArrayIn As Variant) As Boolean
On Error Resume Next
IsArrayBounded = (UBound(ArrayIn) >= LBound(ArrayIn))
End Function

which should work under the vast majority of your situations. However,
someone once wrote in that the following construction could cause
unexpected behavior

>> Sub SomeSub()
>> Dim myArray() As String
>> On Error GoTo ErrorTrap
>> Err.Raise 5
>> Exit Sub
>> ErrorTrap:
>> If IsArrayBounded(myArray) = False Then
>> 'Will not display "Invalid procedure call or argument",
>> 'but will instead display "Subscript out of range":
>> MsgBox "Error info: " & Err.Description
>> End If
>> End Sub

Now, I don't use this type of construction myself; but, if you do, the
following modification of my code should solve that problem

Function IsArrayBounded(ArrayIn As Variant) As Boolean
Dim ErrObj As Variant
'Store Global Err Object
ErrObj = Array(Err.Number, Err.Source, Err.Description, _
Err.HelpFile, Err.HelpContext)
On Error Resume Next
IsArrayBounded = (UBound(ArrayIn) >= LBound(ArrayIn))
'Restore Global Err Object
Err.Raise ErrObj(0), ErrObj(1), ErrObj(2), ErrObj(3), ErrObj(4)
End Function

Rick - MVP



"dr. bandicoot" <anonymous@discussions.microsoft.com> wrote in message
news:58E34BBC-4C36-4958-82FD-56AA668A96BF@microsoft.com...
> Is there any way to detect that an array has not been yet dynamically
sized. If I run into LBound() before a Redim, I get subscript out of
range error and I want to eliminate this error (and I don't want to do
error handling or any other "tricks"). Is there any legal keyword or
command like if array = nothing then etc... ???
>
> Thanks !


Re: Detecting non-dimensioned array by Bob

Bob
Thu Apr 29 14:43:47 CDT 2004

=?Utf-8?B?ZHIuIGJhbmRpY29vdA==?= wrote:
>
> Is there any way to detect that an array has not been yet dynamically sized. If I
> run into LBound() before a Redim, I get subscript out of range error and I want to
> eliminate this error (and I don't want to do error handling or any other "tricks").


Then you've got a religious issue, not a programming problem.



Bob
--

Re: Detecting non-dimensioned array by U

U
Thu Apr 29 15:05:04 CDT 2004

Thanks a lot !! BTW, I have nothing against error trapping, I just did not
want to resort to it without checking if there was a function that does
this. Now I know there isn't and I will use error trapping.

"dr. bandicoot" <anonymous@discussions.microsoft.com> wrote in message
news:58E34BBC-4C36-4958-82FD-56AA668A96BF@microsoft.com...
> Is there any way to detect that an array has not been yet dynamically
sized. If I run into LBound() before a Redim, I get subscript out of range
error and I want to eliminate this error (and I don't want to do error
handling or any other "tricks"). Is there any legal keyword or command like
if array = nothing then etc... ???
>
> Thanks !



Re: Detecting non-dimensioned array by Joseph

Joseph
Thu Apr 29 15:27:48 CDT 2004

Suggestion: Why don't you wrap the error trap in a function which you pass
an array and return a boolean value true or false.

Been there, done that.

- Joe Geretz -

"U. Orhun" <uorhun@symagery.com> wrote in message
news:R9mdnc5aWp1twgzdRVn-ig@igs.net...
> Thanks a lot !! BTW, I have nothing against error trapping, I just did
not
> want to resort to it without checking if there was a function that does
> this. Now I know there isn't and I will use error trapping.
>
> "dr. bandicoot" <anonymous@discussions.microsoft.com> wrote in message
> news:58E34BBC-4C36-4958-82FD-56AA668A96BF@microsoft.com...
> > Is there any way to detect that an array has not been yet dynamically
> sized. If I run into LBound() before a Redim, I get subscript out of range
> error and I want to eliminate this error (and I don't want to do error
> handling or any other "tricks"). Is there any legal keyword or command
like
> if array = nothing then etc... ???
> >
> > Thanks !
>
>



Re: Detecting non-dimensioned array by YYZ

YYZ
Thu Apr 29 15:54:03 CDT 2004

"U. Orhun" <uorhun@symagery.com> wrote in message
news:R9mdnc5aWp1twgzdRVn-ig@igs.net...
> Thanks a lot !! BTW, I have nothing against error trapping, I just did not
> want to resort to it without checking if there was a function that does
> this. Now I know there isn't and I will use error trapping.

But there is, and Yanick (Zoury) pointed you direct to it.
"or peek at the behind-the-scene SAFEARRAY structure :
http://vbnet.mvps.org/code/helpers/getarraydims.htm"

That's what I use (well, a modified version anyway).

Error trapping has its place, but you can get around it in this case. I too
like to avoid trapping errors, preferring never to have them happen at all. If
both ways work fine, then that's just programming style. I just find that
trapping errors like that end up causing the code to pause more than is
necessary when running in the IDE since I run with "Break on All Errors" set.

Matt



Re: Detecting non-dimensioned array by Hapticz

Hapticz
Thu Apr 29 19:19:44 CDT 2004

"run into" a Lbound statement?

wha?

r u programming or just sitting at the keyboard sloshing lines of code into your program "on th fly" ?

evr used an eraser on a pencil before?

your approach to dealing with "unexpected" events is suspect and indicative of ill formed pseudo code and random strategic
methodology.

either get with the "program" and do it right or deal with the "error trappings" as is the case.

work arounds that attempt to "fix" your own poorly formed logic is itself self-defeating and eventually will "kick you in the butt"

but,if you are some poor soul trying to hack away at some 500,000 line mess of code from some handed off project, well, then you
have my sympathy......

.
--

Best regards,
Hapticz

STOP STOP STOP STOP STOP STOP STOP STOP STOP
<>><<>><<><>><<><>><<>><<><>><<>><<>><<>><<>><<>

"dr. bandicoot" <anonymous@discussions.microsoft.com> wrote in message news:58E34BBC-4C36-4958-82FD-56AA668A96BF@microsoft.com...
> Is there any way to detect that an array has not been yet dynamically sized. If I run into LBound() before a Redim, I get
subscript out of range error and I want to eliminate this error (and I don't want to do error handling or any other "tricks"). Is
there any legal keyword or command like if array = nothing then etc... ???
>
> Thanks !


Re: Detecting non-dimensioned array by Randy

Randy
Sat May 01 08:14:38 CDT 2004

: You shouldn't think of "errors" as being something bad; rather
: think of them as information in another format.


<g> You should work for government. Or MS .. "by design".

--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.


Re: Detecting non-dimensioned array by Rick

Rick
Sat May 01 08:38:22 CDT 2004

> : You shouldn't think of "errors" as being something bad; rather
> : think of them as information in another format.
>
>
> <g> You should work for government. Or MS .. "by design".

I did work for the government... our State government... 32+ years for
the New Jersey Department of Transportation. It took, maybe, the first
10 years or so to convince them the errors I made were a good thing,
them being just information in another format, that is.<g>

Rick


Re: Detecting non-dimensioned array by Don

Don
Sat May 01 08:40:58 CDT 2004

On Sat, 1 May 2004 09:14:38 -0400, "Randy Birch" <rgb_removethis@mvps.org>
wrote:

>: You shouldn't think of "errors" as being something bad; rather
>: think of them as information in another format.
>
>
><g> You should work for government. Or MS .. "by design".

My thoughts exactly....

Actually, IMOHO, manipulating Errors sometimes is the shortest, most convient,
path between two or more points...

Have a good day...

Don

Re: Detecting non-dimensioned array by Pásztor,

Pásztor,
Sun May 02 08:41:33 CDT 2004

Rick,

your workaround isn't quite correct, I'm afraid.

Calling the modified IsArrayBounded function will always raise an error, and
if the call is made from inside an error handler, then it is going upward
the error chain.

In general, I save the Err object at the begining of any handler if there is
any chance that the handler's code is going to change it (including
clearing), and use the saved info for whatever it's needed.

But I think the desired effect can be achieved if instead of raising the
error, you just restore the saved items (except of course the LastDllError),
as in:

Function IsArrayBounded(ArrayIn As Variant) As Boolean
Dim iErrN As Long
Dim ErrObj As Variant

iErrN = Err.Number
If iErrN Then
'Store Global Err Object
ErrObj = Array(Err.Source, Err.Description, _
Err.HelpFile, Err.HelpContext)
End If

On Error Resume Next
IsArrayBounded = (UBound(ArrayIn) >= LBound(ArrayIn))

If iErrN Then
'Restore Global Err Object
With Err
.Number = iErrN: .Source = ErrObj(0): .Description = ErrObj(1)
.HelpFile = ErrObj(2): .HelpContext = ErrObj(3)
End With
End If
End Function

--
PZ
Rick Rothstein wrote:
> You shouldn't think of "errors" as being something bad; rather think
> of it as information in another format. There is nothing wrong with
> trapping errors and reacting to them, nothing at all.
>
> I've posted this in the past...
>
> Function IsArrayBounded(ArrayIn As Variant) As Boolean
> On Error Resume Next
> IsArrayBounded = (UBound(ArrayIn) >= LBound(ArrayIn))
> End Function
>
> which should work under the vast majority of your situations. However,
> someone once wrote in that the following construction could cause
> unexpected behavior
>
>>> Sub SomeSub()
>>> Dim myArray() As String
>>> On Error GoTo ErrorTrap
>>> Err.Raise 5
>>> Exit Sub
>>> ErrorTrap:
>>> If IsArrayBounded(myArray) = False Then
>>> 'Will not display "Invalid procedure call or argument",
>>> 'but will instead display "Subscript out of range":
>>> MsgBox "Error info: " & Err.Description
>>> End If
>>> End Sub
>
> Now, I don't use this type of construction myself; but, if you do, the
> following modification of my code should solve that problem
>
> Function IsArrayBounded(ArrayIn As Variant) As Boolean
> Dim ErrObj As Variant
> 'Store Global Err Object
> ErrObj = Array(Err.Number, Err.Source, Err.Description, _
> Err.HelpFile, Err.HelpContext)
> On Error Resume Next
> IsArrayBounded = (UBound(ArrayIn) >= LBound(ArrayIn))
> 'Restore Global Err Object
> Err.Raise ErrObj(0), ErrObj(1), ErrObj(2), ErrObj(3), ErrObj(4)
> End Function
>
> Rick - MVP
>
>
>
> "dr. bandicoot" <anonymous@discussions.microsoft.com> wrote in message
> news:58E34BBC-4C36-4958-82FD-56AA668A96BF@microsoft.com...
>> Is there any way to detect that an array has not been yet dynamically
> sized. If I run into LBound() before a Redim, I get subscript out of
> range error and I want to eliminate this error (and I don't want to do
> error handling or any other "tricks"). Is there any legal keyword or
> command like if array = nothing then etc... ???
>>
>> Thanks !



Re: Detecting non-dimensioned array by Rick

Rick
Sun May 02 09:22:42 CDT 2004

I remember having trouble getting my head completely around this error
object propagation problem back when I first devised my err.raise
solution (some 3 or 4 years ago now) and I wasn't necessarily 100% sure
of it back then. In the mean time, you are the first to raise a concern
about how it will operate since then. Just so I can be sure I understand
the problem fully, can you give me an example piece of code that
demonstrates where my code doesn't work as you think it should, but
where your proposed change does... that would help me out greatly.
Thanks.

Rick - MVP


"Pásztor, Zoltán" <pasztor@ml-cons.hu> wrote in message
news:OoMPwsEMEHA.1340@TK2MSFTNGP12.phx.gbl...
> Rick,
>
> your workaround isn't quite correct, I'm afraid.
>
> Calling the modified IsArrayBounded function will always raise an
error, and
> if the call is made from inside an error handler, then it is going
upward
> the error chain.
>
> In general, I save the Err object at the begining of any handler if
there is
> any chance that the handler's code is going to change it (including
> clearing), and use the saved info for whatever it's needed.
>
> But I think the desired effect can be achieved if instead of raising
the
> error, you just restore the saved items (except of course the
LastDllError),
> as in:
>
> Function IsArrayBounded(ArrayIn As Variant) As Boolean
> Dim iErrN As Long
> Dim ErrObj As Variant
>
> iErrN = Err.Number
> If iErrN Then
> 'Store Global Err Object
> ErrObj = Array(Err.Source, Err.Description, _
> Err.HelpFile, Err.HelpContext)
> End If
>
> On Error Resume Next
> IsArrayBounded = (UBound(ArrayIn) >= LBound(ArrayIn))
>
> If iErrN Then
> 'Restore Global Err Object
> With Err
> .Number = iErrN: .Source = ErrObj(0): .Description = ErrObj(1)
> .HelpFile = ErrObj(2): .HelpContext = ErrObj(3)
> End With
> End If
> End Function
>
> --
> PZ
> Rick Rothstein wrote:
> > You shouldn't think of "errors" as being something bad; rather think
> > of it as information in another format. There is nothing wrong with
> > trapping errors and reacting to them, nothing at all.
> >
> > I've posted this in the past...
> >
> > Function IsArrayBounded(ArrayIn As Variant) As Boolean
> > On Error Resume Next
> > IsArrayBounded = (UBound(ArrayIn) >= LBound(ArrayIn))
> > End Function
> >
> > which should work under the vast majority of your situations.
However,
> > someone once wrote in that the following construction could cause
> > unexpected behavior
> >
> >>> Sub SomeSub()
> >>> Dim myArray() As String
> >>> On Error GoTo ErrorTrap
> >>> Err.Raise 5
> >>> Exit Sub
> >>> ErrorTrap:
> >>> If IsArrayBounded(myArray) = False Then
> >>> 'Will not display "Invalid procedure call or argument",
> >>> 'but will instead display "Subscript out of range":
> >>> MsgBox "Error info: " & Err.Description
> >>> End If
> >>> End Sub
> >
> > Now, I don't use this type of construction myself; but, if you do,
the
> > following modification of my code should solve that problem
> >
> > Function IsArrayBounded(ArrayIn As Variant) As Boolean
> > Dim ErrObj As Variant
> > 'Store Global Err Object
> > ErrObj = Array(Err.Number, Err.Source, Err.Description, _
> > Err.HelpFile, Err.HelpContext)
> > On Error Resume Next
> > IsArrayBounded = (UBound(ArrayIn) >= LBound(ArrayIn))
> > 'Restore Global Err Object
> > Err.Raise ErrObj(0), ErrObj(1), ErrObj(2), ErrObj(3), ErrObj(4)
> > End Function
> >
> > Rick - MVP
> >
> >
> >
> > "dr. bandicoot" <anonymous@discussions.microsoft.com> wrote in
message
> > news:58E34BBC-4C36-4958-82FD-56AA668A96BF@microsoft.com...
> >> Is there any way to detect that an array has not been yet
dynamically
> > sized. If I run into LBound() before a Redim, I get subscript out of
> > range error and I want to eliminate this error (and I don't want to
do
> > error handling or any other "tricks"). Is there any legal keyword or
> > command like if array = nothing then etc... ???
> >>
> >> Thanks !
>
>


Re: Detecting non-dimensioned array by Larry

Larry
Sun May 02 09:53:19 CDT 2004


"U. Orhun" <uorhun@symagery.com> wrote
> Thanks a lot !! BTW, I have nothing against error trapping, I just did not
> want to resort to it without checking if there was a function that does
> this. Now I know there isn't and I will use error trapping.

It will be better to use a more commonly used method than some trick function,
but I see no one has added the Not logic response, so I'll inlcude that here.

I am adding this to the thread just so you know there is a way to do it, but
that doesn't mean you should use it when common practise is otherwise....

LFS


Private Sub Form_Load()
AddNext 5
AddNext 15
AddNext 20
End Sub


Private Sub AddNext(ByVal Value As Long)
Static list() As Long, index As Long
Dim i As Long

' Dim test
If (Not list) = -1 Then
ReDim list(0 To 10)
End If
list(index) = Value

' Show list
Debug.Print "New List:"
For i = 0 To index
Debug.Print list(i)
Next
index = index + 1

End Sub




Re: Detecting non-dimensioned array by Pásztor,

Pásztor,
Sun May 02 11:40:24 CDT 2004

Sorry, I had jumped to the conclusion w/o trying first. I missed the effect
of Resume Next on Err.Raise ...

--
PZ


Rick Rothstein wrote:
> I remember having trouble getting my head completely around this error
> object propagation problem back when I first devised my err.raise
> solution (some 3 or 4 years ago now) and I wasn't necessarily 100%
> sure of it back then. In the mean time, you are the first to raise a
> concern about how it will operate since then. Just so I can be sure I
> understand the problem fully, can you give me an example piece of
> code that demonstrates where my code doesn't work as you think it
> should, but where your proposed change does... that would help me out
> greatly. Thanks.
>
> Rick - MVP
>



Re: Detecting non-dimensioned array by Tony

Tony
Mon May 03 04:32:07 CDT 2004

Unfortunately, this IsArrayBounded function won't work for UDT arrays
(unless the Type is Public in a Public class of an ActiveX component).

There have been previous threads on this subject before. Didn't someone
publish some code that actually looked at the SafeArray descriptor to
determine the dimensionality. OK, it's stepping "outside VB" slightly, but
it works for all array types, and doesn't rely on raising/trapping errors.

Tony Proctor

"Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
news:uTHiYshLEHA.1556@TK2MSFTNGP10.phx.gbl...
> You shouldn't think of "errors" as being something bad; rather think of
> it as information in another format. There is nothing wrong with
> trapping errors and reacting to them, nothing at all.
>
> I've posted this in the past...
>
> Function IsArrayBounded(ArrayIn As Variant) As Boolean
> On Error Resume Next
> IsArrayBounded = (UBound(ArrayIn) >= LBound(ArrayIn))
> End Function
>
> which should work under the vast majority of your situations. However,
> someone once wrote in that the following construction could cause
> unexpected behavior
>
> >> Sub SomeSub()
> >> Dim myArray() As String
> >> On Error GoTo ErrorTrap
> >> Err.Raise 5
> >> Exit Sub
> >> ErrorTrap:
> >> If IsArrayBounded(myArray) = False Then
> >> 'Will not display "Invalid procedure call or argument",
> >> 'but will instead display "Subscript out of range":
> >> MsgBox "Error info: " & Err.Description
> >> End If
> >> End Sub
>
> Now, I don't use this type of construction myself; but, if you do, the
> following modification of my code should solve that problem
>
> Function IsArrayBounded(ArrayIn As Variant) As Boolean
> Dim ErrObj As Variant
> 'Store Global Err Object
> ErrObj = Array(Err.Number, Err.Source, Err.Description, _
> Err.HelpFile, Err.HelpContext)
> On Error Resume Next
> IsArrayBounded = (UBound(ArrayIn) >= LBound(ArrayIn))
> 'Restore Global Err Object
> Err.Raise ErrObj(0), ErrObj(1), ErrObj(2), ErrObj(3), ErrObj(4)
> End Function
>
> Rick - MVP
>
>
>
> "dr. bandicoot" <anonymous@discussions.microsoft.com> wrote in message
> news:58E34BBC-4C36-4958-82FD-56AA668A96BF@microsoft.com...
> > Is there any way to detect that an array has not been yet dynamically
> sized. If I run into LBound() before a Redim, I get subscript out of
> range error and I want to eliminate this error (and I don't want to do
> error handling or any other "tricks"). Is there any legal keyword or
> command like if array = nothing then etc... ???
> >
> > Thanks !
>