[Gambas-user] How to test if result is an array

Caveat Gambas at ...1950...
Fri Mar 16 13:52:04 CET 2012


Hi,

It's probably worth putting in a check for TypeOf(my_var) = gb.Object
(16) before doing the Is Array check...

(From the documentation):

          Expression type
            Return value
NULL
gb.Null=15
Boolean
gb.Boolean=1
Byte
gb.Byte=2
Short
gb.Short=3
Integer
gb.Integer=4
Long
gb.Long=5
Single
gb.Single=6
Float
gb.Float=7
Date
gb.Date=8
String
gb.String=9
Variant
gb.Variant=12
Object
gb.Object=16
Pointer
gb.Pointer=11
Function
gb.Function=13
Class
gb.Class=14

Also from the documentation:
IS: Returns TRUE if an object is an instance of a class, or one of its
descendants.

So, obviously ;-) the thing you're checking with 'IS' has to be some
kind of object and can't be a type like Integer or Boolean etc...hence
you first need to ask "is this thing an object?" and then if so, ask "is
this object an array?"... otherwise Gambas will complain and say "I
expected an object here and you gave me a Boolean" (to take a random
example) :-D

Regards,
Caveat

On Fri, 2012-03-16 at 13:09 +0100, Emil Lenngren wrote:
> That kind of code should work, I think... On what line is the error?
> 
> 2012/3/16 Randall Morgan <rmorgan62 at ...626...>
> 
> > The code is for a test framework. I am currently on the wrong machine to
> > send the code. But it's something like this:
> >
> > Dim result As Variant
> > Dim expected As Variant
> > Dim i As Integer
> > Dim isEqual As Boolean
> >
> > result = myFunction() As Variant
> >
> >
> > if Typeof(result) = Typeof(expected) Then
> >   isEqual = True
> >
> >   'test values
> >   if expected Is Array Then
> >      'test each array element against the expected result
> >      if result Is Array Then
> >         For i=0 To result.Max()
> >            if expected[i] <> result[i] then
> >              isEqual = False
> >            endif
> >         Next
> >      endif
> >   else
> >    .....
> >   endif
> > endif
> >
> >
> >
> >
> >
> > On Fri, Mar 16, 2012 at 4:08 AM, Emil Lenngren <emil.lenngren at ...626...
> > >wrote:
> >
> > > How does your code look like?
> > > A boolean is certainly not an array... :)
> > >
> > > /Emil
> > >
> > > 2012/3/16 Randall Morgan <rmorgan62 at ...626...>
> > >
> > > > That works as long as a is an array. But it a is an a boolean then I
> > get
> > > an
> > > > error "Object expected but got boolean" or float, ect...
> > > >
> > > > I too thought it should work that way. Mybe there is a bug in the
> > Gambas
> > > or
> > > > my code?
> > > >
> > > >
> > > >
> > > > On Fri, Mar 16, 2012 at 3:34 AM, Emil Lenngren <
> > emil.lenngren at ...626...
> > > > >wrote:
> > > >
> > > > > Dim a As New String[]
> > > > > Print a Is Array
> > > > >
> > > > > should work I think...
> > > > >
> > > > > 2012/3/16 Randall Morgan <rmorgan62 at ...626...>
> > > > >
> > > > > > Hi,
> > > > > >
> > > > > > I am writing a small test suit and need to tell is the result I got
> > > > back
> > > > > > from a function is an array of some type.
> > > > > > Does anyone have a solution for this?
> > > > > >
> > > > > > Thanks
> > > > > >
> > > > > > --
> > > > > > If you ask me if it can be done. The answer is YES, it can always
> > be
> > > > > done.
> > > > > > The correct questions however are... What will it cost, and how
> > long
> > > > will
> > > > > > it take?
> > > > > >
> > > > > >
> > > > >
> > > >
> > >
> > ------------------------------------------------------------------------------
> > > > > > This SF email is sponsosred by:
> > > > > > Try Windows Azure free for 90 days Click Here
> > > > > > http://p.sf.net/sfu/sfd2d-msazure
> > > > > > _______________________________________________
> > > > > > Gambas-user mailing list
> > > > > > Gambas-user at lists.sourceforge.net
> > > > > > https://lists.sourceforge.net/lists/listinfo/gambas-user
> > > > > >
> > > > >
> > > > >
> > > >
> > >
> > ------------------------------------------------------------------------------
> > > > > This SF email is sponsosred by:
> > > > > Try Windows Azure free for 90 days Click Here
> > > > > http://p.sf.net/sfu/sfd2d-msazure
> > > > > _______________________________________________
> > > > > Gambas-user mailing list
> > > > > Gambas-user at lists.sourceforge.net
> > > > > https://lists.sourceforge.net/lists/listinfo/gambas-user
> > > > >
> > > >
> > > >
> > > >
> > > > --
> > > > If you ask me if it can be done. The answer is YES, it can always be
> > > done.
> > > > The correct questions however are... What will it cost, and how long
> > will
> > > > it take?
> > > >
> > > >
> > >
> > ------------------------------------------------------------------------------
> > > > This SF email is sponsosred by:
> > > > Try Windows Azure free for 90 days Click Here
> > > > http://p.sf.net/sfu/sfd2d-msazure
> > > > _______________________________________________
> > > > Gambas-user mailing list
> > > > Gambas-user at lists.sourceforge.net
> > > > https://lists.sourceforge.net/lists/listinfo/gambas-user
> > > >
> > >
> > >
> > ------------------------------------------------------------------------------
> > > This SF email is sponsosred by:
> > > Try Windows Azure free for 90 days Click Here
> > > http://p.sf.net/sfu/sfd2d-msazure
> > > _______________________________________________
> > > Gambas-user mailing list
> > > Gambas-user at lists.sourceforge.net
> > > https://lists.sourceforge.net/lists/listinfo/gambas-user
> > >
> >
> >
> >
> > --
> > If you ask me if it can be done. The answer is YES, it can always be done.
> > The correct questions however are... What will it cost, and how long will
> > it take?
> >
> > ------------------------------------------------------------------------------
> > This SF email is sponsosred by:
> > Try Windows Azure free for 90 days Click Here
> > http://p.sf.net/sfu/sfd2d-msazure
> > _______________________________________________
> > Gambas-user mailing list
> > Gambas-user at lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/gambas-user
> >
> ------------------------------------------------------------------------------
> This SF email is sponsosred by:
> Try Windows Azure free for 90 days Click Here 
> http://p.sf.net/sfu/sfd2d-msazure
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user






More information about the User mailing list