# Calling struct fields with symbol or string

**URL:** <https://discourse.julialang.org/t/calling-struct-fields-with-symbol-or-string/80633>\
**Category:** General Usage\
**Tags:** struct\
**Created:** [May 6, 2022, 6:35pm UTC](https://discourse.julialang.org/t/calling-struct-fields-with-symbol-or-string/80633 "2022-05-06T18:35:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephans3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephans3/32/34552_2.png) [@stephans3](https://discourse.julialang.org/u/stephans3)\
**Post date:** [May 6, 2022, 6:35pm UTC](https://discourse.julialang.org/t/calling-struct-fields-with-symbol-or-string/80633/1 "2022-05-06T18:35:08Z")

</div>

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:

```julia
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:

```julia
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:

- [Julia Docs: getproperty()](https://docs.julialang.org/en/v1/base/base/#Base.getproperty)
- [Discourse: Access a struct field from a string](https://discourse.julialang.org/t/access-a-struct-field-from-a-string/44337)

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [May 6, 2022, 7:06pm UTC](https://discourse.julialang.org/t/calling-struct-fields-with-symbol-or-string/80633/2 "2022-05-06T19:06:36Z")

</div>

If you want to use strings, you can even overload `getproperty` to work with `String`s.

```julia
               var_a
               var_b
               var_c
               var_d
       end

julia> myabcd = MyABCD(1,2,3,4)
MyABCD(1, 2, 3, 4)

julia> import Base.getproperty

julia> getproperty(m::MyABCD, s::String) = getproperty(m, Symbol(s));

julia> getproperty(myabcd, "var_a")
1

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 6, 2022, 8:08pm UTC](https://discourse.julialang.org/t/calling-struct-fields-with-symbol-or-string/80633/3 "2022-05-06T20:08:51Z")

</div>

Just note there is a good chance your field access becomes slower than usual. Are you sure this is necessary?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 7, 2022, 12:21am UTC](https://discourse.julialang.org/t/calling-struct-fields-with-symbol-or-string/80633/4 "2022-05-07T00:21:51Z")

</div>

Right, if you are looking to do this frequently, you probably want a Dict or similar, not a struct.
