Hi all,
I got to the problem how to call field names in a struct automatically with symbols or strings.
This can be solved manually with if-else-constructions but is quite long and error-prone.
Beside some related topics like Dictionaries and var"varname"
I finally found a way to do this with getproperty()
Previousely I had:
struct MyABCD
var_a
var_b
var_c
var_d
end
myabcd = MyABCD(1,2,3,4)
mysym = :c # Call variable 'c'
data = 0 # Copy the value of var_c to data
if mysym == :a
data = myabcd.var_a
elseif mysym == :b
data = myabcd.var_b
elseif mysym == :c
data = myabcd.var_c
elseif mysym == :d
data = myabcd.var_d
end
Instead of the if-else construction now I use:
data2 = 0
var_sym = Symbol("var_",string(mysym))
data2 = getproperty(myabcd, var_sym)
If you want to access a struct field you may use setproperty!()
For more information see: