How to check the data type of a variable in FreeBasic ?

New to FreeBASIC? Post your questions here.
kcvinu
Posts: 134
Joined: Oct 07, 2015 16:44
Location: Keralam, India

How to check the data type of a variable in FreeBasic ?

Postby kcvinu » Apr 01, 2018 9:08

Hi, I am trying to receive an "Any Ptr" variable as my function argument. So in the function i need to check and know what is the variable's data type. How can i do that ?.
This was my failed try

Code: Select all

Dim tmp As String = "Test Me"
If Typeof(tmp) =  "String" Then ? "It is a String"          ' Failed
If Typeof(tmp) =  Typeof(String) Then ? "It is a String"       ' Failed
Tourist Trap
Posts: 2293
Joined: Jun 02, 2015 16:24

Re: How to check the data type of a variable in FreeBasic ?

Postby Tourist Trap » Apr 01, 2018 10:03

This works:

Code: Select all

  #if typeOf(THIS._ownerCar)=typeOf(CAR_FWD)
      carResolution = str(THIS._ownerCar)
   #endIf


Types are static in fb son they shouldn't change at runtime, that s why this is a job for a preprocessor if (#if). At least it's what I understood at the time of the code.

The snippet is taken from here : viewtopic.php?f=3&t=25652&p=231675&hilit=typeof%2A#p231675

You can also do typeOf(integer) and so on, but the preprocessor if seems mandatory.
fxm
Posts: 7901
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: How to check the data type of a variable in FreeBasic ?

Postby fxm » Apr 01, 2018 10:37

Yes, TypeOf() is a compiler intrinsic that can be used only at compilation time (replacing itself with the type of the variable passed to it).
It cannot be used as execution condition.

Example of passing non-typed pointer to a procedure in order to print different variable types:

Code: Select all

Enum VariableType
  IntegerVariable
  DoubleVariable
  StringVariable
End Enum

Sub AnyTypePrint (Byval p As Any Ptr, Byval t As VariableType)
  If t = IntegerVariable Then Print *Cptr(Integer Ptr, p)
  If t = DoubleVariable Then Print *Cptr(Double Ptr, p)
  If t = StringVariable Then Print *Cptr(String Ptr, p)
End Sub

Dim As Integer I = 1234
AnyTypePrint(@I, IntegerVariable)
Dim As Double D = 3.14
AnyTypePrint(@D, DoubleVariable)
Dim As String S = "text"
AnyTypePrint(@S, StringVariable)

Sleep
D.J.Peters
Posts: 7419
Joined: May 28, 2005 3:28

Re: How to check the data type of a variable in FreeBasic ?

Postby D.J.Peters » Apr 01, 2018 12:27

@kcvinu don't use type less pointer and overload your function.

Joshy

Code: Select all

#define YOUR_RETURN_TYPE boolean
#define RETURN_YOUR_RESULT_HERE true

function DoSomething overload (byval p as byte ptr) as YOUR_RETURN_TYPE
  ' bla bla
  return RETURN_YOUR_RESULT_HERE
end function 
function DoSomething (byval p as long ptr) as YOUR_RETURN_TYPE
  ' bla bla
  return RETURN_YOUR_RESULT_HERE
end function
function DoSomething (byval p as single ptr) as YOUR_RETURN_TYPE
  ' bla bla
  return RETURN_YOUR_RESULT_HERE
end function 
function DoSomething (byval p as zstring ptr) as YOUR_RETURN_TYPE
  ' bla bla
  return RETURN_YOUR_RESULT_HERE
end function
...
...
...
kcvinu
Posts: 134
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to check the data type of a variable in FreeBasic ?

Postby kcvinu » Apr 01, 2018 13:26

@fxm,
Thanks for the reply and sample code. Its a big help for me. Could you please tell me how can i retrieve an array from its pointer ?. like this ?

Code: Select all

Sub ArrayAdd (ArrayPtr As Any Ptr, Byval objItem As Any Ptr, Byval typ As VariableType)
   If typ = IntegerVariable Then
      'The array is an integer array.
      ' Retreive the array from pointer.
      ' Redim it and put the ObjItem in the array
   Elseif typ = StringVariable Then
      'The array is a string array.
      ' Retreive the array from pointer.
      ' Redim it and put the ObjItem in the array
   End If
End Sub
   
kcvinu
Posts: 134
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to check the data type of a variable in FreeBasic ?

Postby kcvinu » Apr 01, 2018 13:34

@D.J.Peters,
Thanks for the reply. I am assuming that there will be any problems behind using Any Ptr. OK. I am not going to use it. But let me ask one question. You used define keyword instead of using "Boolean" in every overloaded function. Is that intentional ? Any reason behind that ? Do i need to follow that method in my ArrayAdd function too ?
MrSwiss
Posts: 2476
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How to check the data type of a variable in FreeBasic ?

Postby MrSwiss » Apr 01, 2018 13:55

kcvinu wrote:You used define keyword instead of using "Boolean" in every overloaded function. Is that intentional ? Any reason behind that ?
Two short answers: yes, yes
Reason: one place, to change it, for all 'overloaded' Functions (a one-shot method)

kcvinu wrote:Do i need to follow that method in my ArrayAdd function too ?
No, you don't have to, it's purely 'optional'. However, it's 'good programming practice'.
fxm
Posts: 7901
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: How to check the data type of a variable in FreeBasic ?

Postby fxm » Apr 01, 2018 13:56

There is not direct user link between an array and its descriptor (as for a string).
For an array, I think that only the Peters solution (overloading) can work.
kcvinu
Posts: 134
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to check the data type of a variable in FreeBasic ?

Postby kcvinu » Apr 01, 2018 14:01

@Mr Swiss,
Thanks. I am glad to follow that method since its a good practice.

@fxm,
Yeah, It seems that i need to bloat my *.bi file with so many overload functions since i need to use types like Cwstr, Integer, long, HWND, HTREEITEM, LVITEM etc..
kcvinu
Posts: 134
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to check the data type of a variable in FreeBasic ?

Postby kcvinu » Apr 01, 2018 14:21

Hi all,
Could you guys please help me or point me right direction on following subjects ?
1. I have a function which takes an array pointer as parameter. How can i get the original array from that pointer ?
2. I have another function which takes an array as parameter. How can i write it, because this code results in error.

Code: Select all

Sub ArrayAdd (Byref aArray() As Integer, Byval iItem As Integer)
   Dim ArUb As Integer = Ubound(aArray)
   If ArUb = -1 Then
      Redim aArray(0) As Integer
      aArray(0) = iItem
   Elseif ArUb >= 0 Then
      Redim Preserve aArray(ArUb + 1) As Integer
      aArray(ArUb + 1) = iItem
   End If
End Sub
Tourist Trap
Posts: 2293
Joined: Jun 02, 2015 16:24

Re: How to check the data type of a variable in FreeBasic ?

Postby Tourist Trap » Apr 01, 2018 14:49

kcvinu wrote:

Code: Select all

Sub ArrayAdd (Byref aArray() As Integer, Byval iItem As Integer)
...
Redim Preserve aArray(ArUb + 1) As Integer

Sub ArrayAdd (aArray() As Integer, Byval iItem As Integer)

Removing "byref" should help, no?

Ah yes this also:
Redim Preserve aArray(ArUb + 1) As Integer
(as far as I know you won't be able to change\redefine the array's items' types, only resizing will work)
kcvinu
Posts: 134
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to check the data type of a variable in FreeBasic ?

Postby kcvinu » Apr 01, 2018 15:13

@Tourist Trap,
Thank you very much. It worked. But i did a mistake. I didnt use paranthesis after arrayname in my sub call. Anyway, removing ByRef keyword solved the problem.
Tourist Trap
Posts: 2293
Joined: Jun 02, 2015 16:24

Re: How to check the data type of a variable in FreeBasic ?

Postby Tourist Trap » Apr 01, 2018 15:21

@kcvinu, you're welcome. Just to let you know, the arrays are not passed byval (copy of values) nor byref (copy of addresses of values) but rather by descriptor (the thing that describes the array structure and properties internally). So if a keyword had to be used it would be rather bydesc. I tell you that because it has been discussed time to time around here. I hope I'm not erring however, I may miss the things sometimes despite of my sincere efforts when it hits the hard stuff ;)
D.J.Peters
Posts: 7419
Joined: May 28, 2005 3:28

Re: How to check the data type of a variable in FreeBasic ?

Postby D.J.Peters » Apr 01, 2018 15:45

@kcvinu the #define's was only that you can compile the overloaded function's
of course you have to use your own stuff instead.

Joshy

Code: Select all

function blabla overload(p as AAA ptr) as YYY
  ...
end function
function blabla (p as BBB ptr) as YYY
  ...
end function
kcvinu
Posts: 134
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to check the data type of a variable in FreeBasic ?

Postby kcvinu » Apr 01, 2018 16:12

@Tourist Trap,
Thanks for the info. I am sorry for disturbing, but i have two more questions to ask.
1.) How can i get the original array from an array pointer ?.
2.) Is it considered a bad practice to get the array from its pointer ?.

@D.J.Peters,
Thanks for the info.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 1 guest