# How to get instance variables of external class/struct?

**URL:** <https://forum.crystal-lang.org/t/how-to-get-instance-variables-of-external-class-struct/4175>\
**Category:** Help & Support\
**Created:** [December 22, 2021, 1:54pm UTC](https://forum.crystal-lang.org/t/how-to-get-instance-variables-of-external-class-struct/4175 "2021-12-22T13:54:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aravindavk](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/aravindavk/32/1020_2.png) [@aravindavk](https://forum.crystal-lang.org/u/aravindavk)\
**Post date:** [December 22, 2021, 1:54pm UTC](https://forum.crystal-lang.org/t/how-to-get-instance-variables-of-external-class-struct/4175/1 "2021-12-22T13:54:31Z")

</div>

I am trying to get properties of a struct/class and adding those to a new class. But looks like `instance_vars` macro only works to get property of self and not work for other classes. Any suggestion to achieve this?

```auto

struct Node
  property name = "", endpoint = ""
end

struct Pool
  property name = ""
end

# Expected
# struct PoolView
# property name = "", node_name = "", node_endpoint = ""
# end
struct PoolView
  property name = ""

  {% for v in Node.instance_vars %}
    property {{ ("node_" + v.name.stringify).id }} = {{ v.default_value }}
  {% end %}
end

pool_view = PoolView.new
puts pool_view
# Output:
# PoolView(@name="")

```

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [December 22, 2021, 1:58pm UTC](https://forum.crystal-lang.org/t/how-to-get-instance-variables-of-external-class-struct/4175/2 "2021-12-22T13:58:01Z")

</div>

The `instance_vars` method only works in the context of a method (either class or instance). Best bet would be to just copy the properties over manually, or define them in a module and include them into both contexts.

EDIT: Ofc you could get fancier, but IMO is a point where the extra complexity isn’t worth duplicating a few lines.

---

<div class="post-metadata">

**Author:** ![aravindavk](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/aravindavk/32/1020_2.png) [@aravindavk](https://forum.crystal-lang.org/u/aravindavk)\
**Post date:** [December 24, 2021, 7:39am UTC](https://forum.crystal-lang.org/t/how-to-get-instance-variables-of-external-class-struct/4175/3 "2021-12-24T07:39:52Z")

</div>

Thanks
