【问题标题】:Get value of variable named in a string获取以字符串命名的变量的值
【发布时间】:2016-05-04 19:16:42
【问题描述】:

有没有什么方法可以获取在字符串中命名的变量的值。 例如:

Dim number As Integer = 12

Dim numberCopy As Integer = Convert("number")
' numberCopy will now equal 12
' Convert is the function to convert the string to the variable

【问题讨论】:

    标签: realbasic xojo realstudio


    【解决方案1】:

    只有使用 Introspection 才能完成类似的操作,并且只有当保存该值的变量是类的属性时。您的示例,其中变量仅在函数内部暂时已知,无法按您喜欢的方式工作。

    要了解有关 Introspection 的更多信息,请参阅同名文档和示例。

    【讨论】:

    • 感谢您的回答。我不确定 Introspection 是否可以访问本机类型。发布问题后,我也意识到了我的计划中的缺陷。
    【解决方案2】:

    您也可以使用字典而不是直接的属性和变量。

    例如

       dim d as new dictionary
       d.value("number") = 12
    

    那么当你需要值的时候,你就可以做

       Dim numberCopy As Integer = d.value("number")
    

    Dictionary 的好处在于,键和值都是变体,你可以扔给它几乎任何东西,所有变量类型以及对象。

    Language Reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-20
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      相关资源
      最近更新 更多