如何传递内联新对象的参数?(How do I pass parameter of inline new object?)

编程入门 行业动态 更新时间:2024-10-17 15:26:25
如何传递内联新对象的参数?(How do I pass parameter of inline new object?)

我有一个方法接受一个字符串作为参数。 我需要传递的字符串是实例化对象的属性。 一旦获得该属性的值,我就不需要该对象。

我知道我可以这样做:

Dim x As New myClass1 foo.thing1 = MyMethod(x.Name) x = New MyClass2 foo.thing2 = MyMethod(x.Name) 'etc...

但如果可能的话,我宁愿这样做内联,因为我必须用不同的MyClass类型连续多次执行此操作。

编辑:

弄清楚了:

foo.thing = MyMethod(new MyClass().Name)

I have a method that accepts a string as a parameter. The string I need to pass is the property of an instantiated object. I don't need the object to stick around once I get the value of that property.

I know I can do this like so:

Dim x As New myClass1 foo.thing1 = MyMethod(x.Name) x = New MyClass2 foo.thing2 = MyMethod(x.Name) 'etc...

But I would prefer to do this inline if possible, since I have to do this several times in a row with different MyClass types.

EDIT:

Figured it out:

foo.thing = MyMethod(new MyClass().Name)

最满意答案

尝试

foo.thing1 = MyMethod((New myClass1).Name) foo.thing2 = MyMethod((New MyClass2).Name)

你需要围绕New myClass1使用大括号,否则VB认为你正在尝试创建一个myClass1.Name类型的对象,当然这个对象不存在。

foo.thing = MyMethod(new MyClass().Name)

更多推荐

本文发布于:2023-08-04 23:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423375.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内联   对象   参数   pass   inline

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!