Eval(exp)

Post your Gambas programming questions here.
Post Reply
User avatar
thatbruce
Posts: 244
Joined: Saturday 4th September 2021 11:29pm

Eval(exp)

Post by thatbruce »

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
User avatar
BruceSteers
Posts: 1931
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Eval(exp)

Post by BruceSteers »

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.
If at first you don't succeed , try doing something differently.
BruceS
ocoquet
Posts: 43
Joined: Friday 18th December 2020 7:56am

Re: Eval(exp)

Post by ocoquet »

Multilines can work !

here is a sample:

Code: Select all

'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
User avatar
thatbruce
Posts: 244
Joined: Saturday 4th September 2021 11:29pm

Re: Eval(exp)

Post by thatbruce »

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:
"obj="&thisobj.ObjectType
"name="&thisobj.Name
"blah="&thisobj.Blah
"blahblah="&thisobj.BlahBlah
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.
ocoquet
Posts: 43
Joined: Friday 18th December 2020 7:56am

Re: Eval(exp)

Post by ocoquet »

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
ocoquet
Posts: 43
Joined: Friday 18th December 2020 7:56am

Re: Eval(exp)

Post by ocoquet »

OK

Form me, it's work nice:

Code: Select all

'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
User avatar
BruceSteers
Posts: 1931
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Eval(exp)

Post by BruceSteers »

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..
' Gambas class file  (MyObject.class)

Property ObjectType As String Use $sObjectType
Property Name As String Use $sName
Property Blah As Variant Use $vBlah
Property BlahBlah As Variant Use $vBlahBlah

Property Settings As Variant[]

Private Function Settings_Read() As Variant[]

  Return [$sObjectType, $sName, $vBlah, $vBlahBlah]

End

Private Sub Settings_Write(Value As Variant[])

  $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.

End


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


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


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"])
If at first you don't succeed , try doing something differently.
BruceS
Post Reply