Eval(exp)
Posted
#1
(In Topic #1360)
Regular

The wiki says "most" expressions are available.
I was hoping that the introduction of multi-line expressions in GB version whatever would allow me to use such type of things as
Select, If Then, For X
This does not appear to be the case.
Has anyone have a better explanation of what can be done and what can't be done.
b
Posted
Guru

Dim sText As String = "If System.Version=3 Then Print 3 Else Print -1"
Eval(sText)
Error "Unexpected If"
This works though
Dim sText As String = "If(System.Version=3, 3, -1)"
Print Eval(sText)
Kinda limited ,
I usually use it to convert text into objects like if i want to get a control/property but all i have is it's name string.
a silly example..
Dim sTBName As String="TextBox1"
Dim sText As String = Eval(sTBName & ".Text")
Or an actual example from my code, where text is left,right,up or down but i want the key.constant , using Object.GetProperty(Key, sKey) gives a bad use of virtual Key class error but Eval works.
Case Like "{left,right,up,down}"
Terminal.MoveCursor(Eval("Key." & sKey), iVal)
But do i have a better explanation ?
probably not, just some observations.
Posted
Regular

here is a sample:
Code
'Class1.class
Public i As Integer
Public a As Integer
Public sub test()
Print Eval("Let Class1.i = 10\nLet Class1.a=100\nLet Class1.i = Class1.i*Class1.a", Null)
endPrint 1000
Regards
Posted
Regular

They are not multi-line expressions, they are just strings.
I cannot put a 40+ line expression into eval.
To better explain.
I have a dozen or so "templates" for an output to a text file. They are in text files that are read in (depending on a context) and consist of lines like:
Code (gambas)
- "obj="&thisobj.ObjectType
- "name="&thisobj.Name
- "blah="&thisobj.Blah
- "blahblah="&thisobj.BlahBlah
- etc etc etc
Posted
Regular

I think, the problem is not eval function it's an editor problem, multilines are considered like separate lines, il look in details and come back.
regards
Olivier
Posted
Regular

Form me, it's work nice:
Code
'Class1.class
Public i As Integer
Public sub test()
Eval("Let Class1.i=\"123\"" &
"Let Class1.i&=\"456\"" &
"Let Class1.i&=\"789\"")
Print i
endTry it
regards
Olivier
Posted
Guru

you can add a .Settings As Variant[] property to any class (that does not already have it)
then use Settings.Write() / Settings.Read() on the object
Say for example your objects are in a class called MYObject.class and a $hMyObject instance (just for example)
The MyObject.class like this..
Code (gambas)
- ' Gambas class file (MyObject.class)
- $sObjectType = Value[0]
- $sName = Value[1]
- $vBlah = Value[2]
- $vBlahBlah = Value[3]
- RefreshData() ' reload the data into the controls. In the above code use of something like Me.Name instead of $sName could automate this for you.
Then this to Save
Then this to Load
A more unique Key can be used
hSet.Write($hMyObject,"UniqueName") ' save the object with a name
A default can also be given
hSet.Read($hMyObject, ,"UniqueName", ["Type", "Name", "Blah", "BlahBlah"])
1 guest and 0 members have just viewed this.


