动态引用 PowerBuilder 结构值
在 PowerBuilder 12 Classic 中,我尝试动态访问/设置结构的属性/元素。知道如何做到这一点吗?
我正在尝试构建一个开发人员实用程序,它将检查任意结构(在编译时未知的结构)以查找其元素。我可以通过访问结构的 ClassDefinition
的 VariableList
属性来获取元素的名称和类型。但是知道元素的名称和类型对我没有帮助,因为我不知道如何通过名称访问元素。
例如,假设我有一个包含两个字符串元素的结构 st_person
:first_name
和 last_name
。有没有办法表达设置值?任何像这样的遥远的事情:
st_person l_person
Any myStructure
myStructure = l_person
myStructure.setValue("first_name") = 'John'
myStructure.setValue("last_name") = 'Smith'
我真的希望我没有错过一些明显的事情。
In PowerBuilder 12 Classic, I am trying to dynamically access/set the properties/elements of a structure. Any idea how to do this?
I'm attempting to build a developer utility which will examine an arbitrary structure (one which is unknown at compile time) to find its elements. This I can get the names and types of the elements by accessing the VariableList
property of the structure's ClassDefinition
. But knowing the name and type of the element doesn't help me, as I know no way to access the elements by name.
For example, say I have a structure st_person
with two string elements: first_name
and last_name
. Is there a way to express setting the value? Anything remotely like this:
st_person l_person
Any myStructure
myStructure = l_person
myStructure.setValue("first_name") = 'John'
myStructure.setValue("last_name") = 'Smith'
I really hope I'm not missing something obvious.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,你不会错过任何东西,除非你在 PBNI 中编程。根据我对 PBNI 的了解,您应该能够做到这一点,但我不是有关 PBNI 的可靠信息来源。
您使用结构有原因吗?听起来您想要的东西接近哈希表或命名项集。我在 PFC 的链接列表之一的顶部构建了自己的命名项集,其中每个节点中的键是名称,数据是……好吧,数据。非常粗略地说,我有一个 of_get (string as_name) 返回 any 和 of_set (string as_name, any aa_value)。这样,我就可以
在接收此消息的另一端(这对于 OpenWithParm 和其他需要一个参数的东西来说非常有用)
更进一步,我有 of_Defined (string as_name) 来测试是否存在和 of_Get (string as_name, ref任何 aa_value) 返回整数,如果找到则返回成功,以启用可选参数和对象之间更宽松的契约等功能。
还有更多,但这应该可以帮助您继续。如果您不使用 PFC 并且不想从中提取链接列表,那么您可能没有理由不能在一对并行无界数组之上构建它:一个 String 用于键,一个 Any 用于数据。或者,将 DataStore 替换为 String 键列(DataStore 可以更快地搜索键)和 Number 列作为无界 Any 数组的索引。
祝你好运,
特里。
No, you're not missing anything, unless you program in PBNI. From what I've heard of PBNI, you should be able to do this, but I'm not a reliable source of information about PBNI.
Is there a reason why you're using a structure? It sounds like what you want is close to a hash table or a named item set. I've built my own named item set on top of one of PFC's linked lists, where in each node the key is the name, and the data is... well, the data. Very roughly, I have an of_get (string as_name) returns any, and of_set (string as_name, any aa_value). That way, I can
and on the other end that receives this (this is great for OpenWithParm and other things that expect one parameter)
Going a step further, I have of_Defined (string as_name) to test for existence and of_Get (string as_name, ref any aa_value) returns integer that returns success if found, to enable things like optional parameters and looser contracts between objects.
There's more, but that should get you going. If you're not using PFC and you don't feel like carving the linked lists out of it, there's probably no reason you can't build this on top of a pair of parallel unbounded arrays: one String for keys and one Any for data. Or, substitute a DataStore with a String key column (DataStore makes searching for keys quicker) and a Number column as an index into an unbounded Any array.
Good luck,
Terry.
我添加另一个答案是为了响应 Hugh 将字符串转换为枚举值的新目标(他于 2011 年 5 月 20 日发表的评论)。我有一些代码可以生成将字符串转换为枚举以及将枚举转换为字符串的代码,以 PFC 风格完成(如 n_cst_conversion)。要适应您的 PowerBuilder 版本,您唯一需要做的就是在构造函数中更新枚举分类列表。 (我通常从 PB 对象浏览器的报告中获取此信息,然后运行一些常规文本搜索和替换。)它并不完美,但它可以帮助您直到下次进行重大升级。
祝你好运,
特里。
n_cst_enumscripts
InstanceVariables
事件构造函数返回 long
*function of_enumprototypes () 返回字符串*
*function of_enumscripts () 返回字符串*
I'm adding another answer to respond to Hugh's new target (his comment on May 20/2011) of converting strings to enumerated values. I've got code that generates code that converts strings to enumerated and enumerated to strings, done in PFC style (like n_cst_conversion). The only thing you have to do to adapt to your version of PowerBuilder is, in the Constructor, update the list of enumerated classifications. (I usually get this from a report from PB's object browser, run through some regular text search and replace.) It's not perfect, but it gets you by until the next time you do a major upgrade.
Good luck,
Terry.
n_cst_enumscripts
InstanceVariables
event constructor returns long
*function of_enumprototypes () returns string*
*function of_enumscripts () returns string*