Is there something similar as EVALUATE(Field1)?

I have a string:-

Dim s As String s = "Field1, Field2, Field3, Field4, Field5"

After breaking up the string, I have the following variables:-

Dim f1, f2, f3, f4, f5 As String f1 = "Field1" f2 = "Field2" f3 = "Field3" f4 = "Field4" f5 = "Field5"

Is it possible to read the value of Field1 from f1?
For example

Field1 = "Apple" Field2 = "Banana"

I am expecting

and the output result is “Apple”

Not directly.

You can use XojoScript to build an expression evaluator.
or you can loop through control on a window to find the one with given name and read it’s value.

Would it not make more sense to use an array ?

Dim s() As String s = Array(Field1, Field2, Field3, Field4, Field5)

Then access each element simply by its index.

[quote=312835:@Michel Bujardet]Would it not make more sense to use an array ?

Dim s() As String s = Array(Field1, Field2, Field3, Field4, Field5)

Then access each element simply by its index.[/quote]
Hello Michel, as mentioned, the source is a string. I may not be able to change the data structure. Thanks for the suggestion anyway.

You could still store it in an array.

Dim s() As String
s = Split("Field1, Field2, Field3, Field4, Field5",",")

If the string always has , as separator, you can use NthField.

Also, I don’t know what you mean by “breaking up the string”, but with a string such as “Field1, Field2, Field3, Field4, Field5” you can do this to obtain an F array :

dim F() as String = split(s, ",")

If I did not understand what you are after, simply disregard.

I think the OP is asking to be able to access a field known as “Field1”, rather like the database call:

dim st as string = RECORDSET.Field("FIELD1").StringValue

or

RECORDSET.Field("FIELD1").StringValue = st

I suspect that introspection is what he needs but I am really unfamiliar with this aspect of Xojo so will not offer any advice - I’ll leave that to better experts than me!

[quote=312840:@Michel Bujardet]If the string always has , as separator, you can use NthField.

Also, I don’t know what you mean by “breaking up the string”, but with a string such as “Field1, Field2, Field3, Field4, Field5” you can do this to obtain an F array :

dim F() as String = split(s, ",")

If I did not understand what you are after, simply disregard.[/quote]
Hello Michel, by using your method, I still could not get the result of “Apple”.

[code] Dim s() As String = Split(“Field1, Field2, Field3, Field4, Field5”, “,”)

	MsgBox s(0)[/code]

I am expecting a value of “Apple” but it is returning “Field1”.

[quote=312844:@Simon Berridge]I think the OP is asking to be able to access a field known as “Field1”, rather like the database call:

dim st as string = RECORDSET.Field("FIELD1").StringValue

or

RECORDSET.Field("FIELD1").StringValue = st

I suspect that introspection is what he needs but I am really unfamiliar with this aspect of Xojo so will not offer any advice - I’ll leave that to better experts than me![/quote]
Hello Simon, Field1 is refering to a Variable. I did look into introspection, but seems like it can only return the data types but not the value of the passing parameters. I hope I am wrong.

The closest would seem to be Dictionary.

I had tried the xojoscript method but I am still getting an empty string :frowning:

[code] Dim Field1, Result1 As String
Field1 = “Apple”
Dim expression as String
expression = “Field1”

	Dim Script As New XojoScript
	Script.Source = "Dim expr As Variant = " + expression + EndOfLine + _
	"Result1 = expr"
	Script.Context = Self
	Script.Run
	
	msgbox Result1[/code]

Hello Michel, I did look into Dictionary but I do not know how to apply to it.

Dim f As String f = "field1" Dim d As New Dictionary d.Value(f) = ?

What am I suppose to code on the right side of the statement?

What exactly is field1 a variable, or a control? If its a variable then just use a dictionary like Michel said.

dim dict as new dictionary
dict.value("Field1")= "Apple"
dict.Value("Field2")= "Orange"
msgbox dict("Field1").value '< result is "Apple"

[quote=312853:@Neil Burkholder]What exactly is field1 as variable, or a control. If its a variable then just use a dictionary like Michel said.

[code]
dim dict as new dictionary
dict(“Field1”).value = “Apple”
dict(“Field2”).value = “Orange”

msgbox dict(“Field1”).value '< result is “Apple”
[/code][/quote]
Hello Neil, I am supposed to read the value from “Field1” and not providing value to “Field1”. That means I will not know the value of Field1 previously.

Again what is “Field1”? Is it a text field, database field, string variable…

There is no index of variables, so you can’t ask for a variable named Field1.

If it’s part of a RecordSet or a control, you can loop and look for the name.

Or you stuff the variables into a dictionary, which allows you find them by name.

[quote=312856:@Neil Burkholder]Dim s As String
s = “Field1, Field2, Field3, Field4, Field5”[/quote]

For simplicity issue, Field1 to Field5 is a WebPage String or Integer Property

Dim s() As String = Split("Field1, Field2, Field3, Field4, Field5", ",")

How do I convert string “Field1” to variable?

dim myField as TextField dim whichFieldToGet as string = window1.TextField1.text for i as integer = 0 to window1.ControlCount - 1 if window1.Control(i).Name=whichFieldToGet then myField=TextField(window1.Control(i)) MsgBox myField.text exit end if next

Using your xojo script example with a dictionary you would do this:

Dim Field1, Result1 As String
Field1 = "Apple"
Dim expression as String
expression = "Field1"

Dim Dict as new dictionary
Dict("Field1") = Field1		
Result1 = Dict.Value(expression)
msgbox Result1

[quote=312862:@Christian Schmitz]There is no index of variables, so you can’t ask for a variable named Field1.

If it’s part of a RecordSet or a control, you can loop and look for the name.

Or you stuff the variables into a dictionary, which allows you find them by name.[/quote]
Hello Christian, I will not be able to stuff the values into the dicionary as I do not know the value of Field1.

The string “Field1, Field2, Field3, Field4, Field5” is executed at runtime but not hardcoded in IDE. In IDE, I would be able to get the value easily by hardcoding

msgbox Field1  // return value Apple

[quote=312865:@Neil Burkholder]If Field1 is a textfield you could do something like this.

dim f as TextField for i as integer = 0 to self.ControlCount-1 if self.Control(i).Name = "Field1" then f = TextField(self.Control(i)) MsgBox f.Text Exit For end if next[/quote]
Hello Neil, it does not exist as control.