Eval(exp)

Post

Posted
Rating:
#1 (In Topic #1360)
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
 The limitations of Eval(exp as expression) are unexplained.
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

Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
 it doesn't seem to like multiline or conditional statements.

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.
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
ocoquet is in the usergroup ‘Regular’
Multilines can work !

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)

end

Print 1000

Regards

Olivier Coquet
Gambas Dev
Le Forum développeur Gambas
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
Yes but.
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)

  1. "obj="&thisobj.ObjectType
  2. "name="&thisobj.Name
  3. "blah="&thisobj.Blah
  4. "blahblah="&thisobj.BlahBlah
  5. etc etc etc
Obviously with a dozen or so templates with up to 60ish lines I don't want to compress the templates to one single line, for coding, execution and debugging purposes.

Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
ocoquet is in the usergroup ‘Regular’
 Ok !

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

Olivier Coquet
Gambas Dev
Le Forum développeur Gambas
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
ocoquet is in the usergroup ‘Regular’
OK

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

end

Try it

regards
Olivier

Olivier Coquet
Gambas Dev
Le Forum développeur Gambas
Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘Guru’
sounds like a job for Settings.class

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)

  1. ' Gambas class file  (MyObject.class)
  2.  
  3. Property ObjectType As String Use $sObjectType
  4. Property Name As String Use $sName
  5. Property Blah As Variant Use $vBlah
  6. Property BlahBlah As Variant Use $vBlahBlah
  7.  
  8.  
  9. Private Function Settings_Read() As Variant[]
  10.  
  11.   Return [$sObjectType, $sName, $vBlah, $vBlahBlah]
  12.  
  13.  
  14. Private Sub Settings_Write(Value As Variant[])
  15.  
  16.   $sObjectType = Value[0]
  17.   $sName = Value[1]
  18.   $vBlah = Value[2]
  19.   $vBlahBlah = Value[3]
  20.  
  21.   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.
  22.  
  23.  

Then this to Save

Code (gambas)

  1. Dim hSet as Settings = New Settings(sConfigFile) ' open/set the config file for saving
  2. hSet.Write($hMyObject)   ' save all the .$hMyObject.Settings data you added.
  3. hSet.Save
  4.  

Then this to Load

Code (gambas)

  1. Dim hSet as Settings = New Settings(sConfigFile) ' load the config
  2. hSet.Read($hMyObject)   ' set all the object properties according to file data
  3.  

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"])
Online now: No Back to the top
1 guest and 0 members have just viewed this.