[Gambas-user] Answer to: "Way to tell if an object is a struct"

Benoit Minisini benoit.minisini at gambas-basic.org
Thu Apr 6 11:47:21 CEST 2023


Le 05/04/2023 à 18:04, vuott--- via User a écrit :
> On this thread
> https://lists.gambas-basic.org/pipermail/user/2023-March/078862.html 
> <https://lists.gambas-basic.org/pipermail/user/2023-March/078862.html>
> Brian G asked, if it were possible to know if an Object is a Structure.
> In Gambas source file
>     /home/[USER]/gambasdevel/main/share/gambas.h
> the numbers of datatypes identifiers are listed.
> Therefore, by knowing which number a declared variable belongs to, it is 
> possible to know its data type.
> I found a way to know, from the memory address of the variable it is 
> necessary to go back 8 units. Deferencing the Pointer will give the 
> identifier number of the data type of the variable, which consequently 
> will have to be compared with the list above.
> 
> This is my simply code:
> 
> [code]
> Public Struct Struttura
>    b As Byte
>    c As Short
> End Struct
> 
> Private id As Struct Struttura
> 
> 
> Public Sub Main()
> 
> Dim p As Pointer
> 
> p = VarPtr(id)
> 
> Print "Gambas datatype identifier: "; Byte@(p - 8)
> 
> End
> [/code]
> 
> It should be noted that in the list of data-type identifier numbers, in 
> particular, the data type Structure is NOT present.
> It will be located and reported by my code as an "Object" (number: 16)
> 

Of course, don't do that! It might bite you at any time. TypeOf() gives 
you the datatype of a value.

Why do you need to know if an object reference points at a structure?

-- 
Benoît Minisini.



More information about the User mailing list