如何传递内联新对象的参数?(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)更多推荐
发布评论