finding a variable's type

Just starting out? Need help? Post your questions and find answers here.
freezer
New User
New User
Posts: 5
Joined: Thu Dec 10, 2009 10:52 am

finding a variable's type

Post by freezer »

hi,

is there a function to find the variable's type after it has been defined ?

thanks

Code: Select all

a.b = 42
b.i = 42
c.q = 42
d.f = 42.245
e.d = 42
f.s = "forty two"
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: finding a variable's type

Post by UserOfPure »

It never changes after being defined. Since you defined it, you already know.
freezer
New User
New User
Posts: 5
Joined: Thu Dec 10, 2009 10:52 am

Re: finding a variable's type

Post by freezer »

i have an app with hundreds of variables in, and I want to save/load these from a preference file.

rather than having to write every variable preference out individually (WritePreferenceInteger(blablabla) etc..) i wanted to read in a text file containing the
names of all the variables, and then generate the required PB code to output the variables with their correct types.

.. something like this ..

Code: Select all

Enumeration
  #prefListHandle
  #writePrefListCoded
  #readPrefListCoded
EndEnumeration

Procedure HandleError(result.l, Text.s = "")
  If result = 0
    MessageRequester("Error", Text, #PB_MessageRequester_Ok)
    End
  EndIf
  ProcedureReturn result
EndProcedure

a = 6
b = 10
c$ = "holiday"
List$ = ""

directory$ = "d:\temp\"
filename$ = directory$ + "prefsListSource.txt" : HandleError(ReadFile(#prefListHandle,filename$),"Cannot open " + filename$)
filename$ = directory$ + "writePrefListCoded.txt" : HandleError(CreateFile(#writePrefListCoded,filename$),"Cannot create " + filename$)
filename$ = directory$ + "readPrefListCoded.txt" : HandleError(CreateFile(#readPrefListCoded,filename$),"Cannot create " + filename$)

Repeat
  temp$ = ReadString(#prefListHandle)
  If temp$ <> ""
    
    If Right(temp$,1) = "$"
      writeList$ + "writepreferencestring("
    Else
      writeList$ + "writepreferenceinteger("
    EndIf
    writeList$ + Chr(34) + temp$ + Chr(34) + "," + temp$ + ")" + Chr(13) + Chr(10)
    WriteString(#writePrefListCoded,writeList$)
    
    If Right(temp$,1) = "$"
      readList$ + "readpreferencestring("
    Else
      readList$ + "readpreferenceinteger("
    EndIf
    readList$ + Chr(34) + temp$ + Chr(34) + "," + temp$ + ")" + Chr(13) + Chr(10)
    WriteString(#readPrefListCoded,readList$)
    
  EndIf
Until temp$ = ""

CloseFile(#prefListHandle)
CloseFile(#writePrefListCoded)
CloseFile(#readPrefListCoded)
in php there is a command "is_numeric()" and "is_string", is there anything similar in pure basic please ?
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: finding a variable's type

Post by Kaeru Gaman »

you cannot generate PB Code on-the-fly.
PureBasic is no scripting language, php is no compiler language.
and there a tons of differences more.

yes, there are really few cases when you want a variant type, but in 99.999% of the cases the type of a value is clearly defined, has to be clearly defined to match the very nature of programming.


in my opinion you are thinking the wrong way.

when you want to go the way your question leads, you don't want a compiler language.
oh... and have a nice day.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: finding a variable's type

Post by blueznl »

Well, there is a way...

Before you compile, use some tool to run through your code and detect each variable type, then create an include file containing the procedures for saving / loading variables.

Shouldn't be too hard.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: finding a variable's type

Post by Kaeru Gaman »

when you want to save different recordsets with the same routine,
just save the whole structure length as a recordset.
oh... and have a nice day.
Post Reply