VB.Net、EventArgs、ByRef 和 ByVal
在 VB.Net 中,我有一个名为 WorkflowButtonEventArgs
的对象,它继承自 System.EventArgs
。
WorkflowButtonEventArgs
类包含两个 ByRef
属性。这些是内存中的对象,我不希望它们以任何方式重复或复制。
我可以在 VB.Net 中传递 WorkflowButtonEventArgs
对象 ByVal
并让它仍然保留 WorkflowButtonEventArgs
中的两个 ByRef
定义吗?
具体来说,如果我传递 ByVal:
Dim e As New WorkflowButtonEventArgs(...) ' e has some ByRef properties
RaiseEvent SomeEventName(e) ' e is passed ByVal
e
(WorkflowButtonEventArgs
类)中的 ByRef
Properties/Members 是否不会在内存中复制或重复?
长话短说:我可以传递 e
ByVal
,还是需要传递它 ByRef
因为它包含 ByRef属性?
In VB.Net, I have an object named WorkflowButtonEventArgs
that inherits from System.EventArgs
.
The WorkflowButtonEventArgs
class contains two ByRef
Properties. These are objects that are in memory, and I do not want them duplicated or copied in any way.
Can I pass the WorkflowButtonEventArgs
object ByVal
in VB.Net and have it still preserve the two ByRef
definitions in WorkflowButtonEventArgs
?
Specifically, if I pass it ByVal:
Dim e As New WorkflowButtonEventArgs(...) ' e has some ByRef properties
RaiseEvent SomeEventName(e) ' e is passed ByVal
Will the ByRef
Properties/Members in e
(WorkflowButtonEventArgs
class) not be copied or duplicated in memory?
Long-story-short: Can I pass e
ByVal
, or do I need to pass it ByRef
since it contains ByRef
Properties?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的。即使您的
EventArgs
通过 ByVal 传递,引用指向的对象也不会被复制。Yes. The objects pointed to by the reference will not get copied, even if your
EventArgs
is passed ByVal.引用对象不会在内存中重复。参数上的
ByRef
关键字仅意味着您可以更改调用代码中参数下的变量的值。Reference objects wont be duplicated in memory. The
ByRef
keyword on a parameter only means that you can change the value of a variable underlying the argument in the calling code.实现目标的另一种方法是创建一个存储这两个属性的单例。
Another way to achieve your goal would be to create a singleton that would store the two properties.