EVAL function

Hello
I have a question dose xojo has something like EVAL function

I want to do something like this

name = myProperties(i).Name MyClass.name.value=3

thanks for the help

What is EVAL supposed to do?

Eval is a function which evaluates a string as though it were an expression and returns a result. Like in PHP or Matlab

>>> x = 1
>>> eval('x + 1')
2
>>> eval('x')
1

There is nothing native, but you can implement something similar using XojoScript.

the opensource mathfield available here :
may help you to write your own eval function.

Hi Marek,

There is nothing like (Matlab’s ?) eval in Xojo, but you can do what you are after.

Do you have your objects in an array? Is this class a control? Is it a subclass of something else? I am not very versed in subclassing, casting etc so I may only point you in the right direction.

Kem, Jean Yves, that was just an example, EVAL executes what is expressed by a string as if it were code. What he wants is to change some property of an instance of his class, of which he knows the name.

EDIT: To ellaborate a bit more on what (Matlab’s) Eval does:

 eval ("Dim myString as new string") 

would be equivalent to

Dim myString as new string 

It’s indeed similar to XojoScript in the sense that it can be constructed at runtime, but it is more flexible in the sense that it can execute any sentence, and it is not as limited as XojoScript. If I am not mistaken what he wants to do can be done using casting?

Julen

A simple type of Eval that uses XojoScript is included with the Xojo example projects:

Examples/Advanced/XojoScript/Evaluator

Okey so how could i do something like

  Dim Param as New ParamsClass
  Dim Found as Boolean
  Dim EVAL as new Evaluator
 Dim ReadValue, command as string  

  While Found = False
  Dim myProperties() as Introspection.PropertyInfo = Introspection.GetType(Param).GetProperties
    For i as Integer = 0 to Ubound(myProperties) 
      if myProperties(i).Name  =ParamName Then
        command = "ParamsSiteBasicClass." + myProperties(i).Name + ".value=" + ReadValue  
        TmpParamName=EVAL.Eval(run)
      Found = True
      elseif i = UBound(myProperties) then
        Found = True
      end
    Next
  Wend
  

As Kem said, you will need to use XojoScript.

Does ParamsSiteBasicClass inherit from any other class? Can you make it inherit from Control?

To everyone else, wouldn’t the following be a possibility? I am not sure you are understanding what he is trying to do, but maybe it’s me not understanding your reply :slight_smile: :

Have a property in a window of the class ParamsSiteBasicClass (make it inherit from control), the one you want to work with. In a method belonging to the window do:

for i = 0 to ControlCount-1 if Control(i) IsA ParamsSiteBasicClass then if ParamsSiteBasicClass(Control(i)).name= SoughtName then ParamsSiteBasicClass(Control(i)).value=ValueToBeSet Exit For end if end if next
Can anyone have a look at the code and see there is anything wrong?

Pixe

If a Control is an array of names of Properties of the ParamsSiteBasicClass then this is exactly what I wanted to do

Control is all controls on a layout(buttons, listboxes, etc)

IsA tests a control to see if it is an instance of the specified class

and if it is then you can cast to that type, safely, and just assign the value directly to the property

So, the answer is no. I didn’t understand from your previous posts the structure of your class, and I still don’t. I would like to keep trying to find an easy way of doing what you want but I would need to understand what your class is like.

Julen

So I will try to explain. I have a class “ParamsClass” This class has lots of Properties and a 2 Methods: READ, WRITE. Each Property has a type like ParamsIntClass and has a default value. So THE TASK is to read params from a file and replace the default values to those found in the file. there may be two or three or 30

This problem is exactly the topic I’ll be covering at this year’s XDC: Data serialization, i.e., converting an object to a string then back again. I have a class that will take an object and turn it into a JSONItem, and turn it back again later. Let me know if you want it.

I keep trying… :slight_smile:

In your ParamsClass, Param1, Param2,… etc are in an array? If so, once you read a flag/value pair you can do:

For i=0 to Ubound(ParamsClassArray)-1
   if ParamsClassArray(i).flag=SoughtFlag then 
      ParamsClassArray(i).Value=ValueToBeSet
      exit for
   end if
next

would like it yes if available !
thanks.

I certainly hope you’ll be able to share it also after the XDC with the rest of us who are too poor, too far away or whatever reason we have not being able to attent the XDC. Object serialization is a much needed feature in Xojo.

Sure. I’ll probably put it up on GitHub or something.

@Kem Tekinay Is this avaialble yet on GitHub? Really looking forward to see how you do this :slight_smile: