Page 1 of 1
finding a variable's type
Posted: Thu Dec 10, 2009 10:56 am
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"
Re: finding a variable's type
Posted: Thu Dec 10, 2009 11:06 am
by UserOfPure
It never changes after being defined. Since you defined it, you already know.
Re: finding a variable's type
Posted: Thu Dec 10, 2009 11:17 am
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 ?
Re: finding a variable's type
Posted: Thu Dec 10, 2009 12:03 pm
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.
Re: finding a variable's type
Posted: Thu Dec 10, 2009 12:18 pm
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.
Re: finding a variable's type
Posted: Thu Dec 10, 2009 4:56 pm
by Kaeru Gaman
when you want to save different recordsets with the same routine,
just save the whole structure length as a recordset.