方法参数与签名匹配,但仍然出现错误
我正在将 VB 库转换为 C# 库。我的方法之一在 VB 中具有以下签名:
Private Shared Sub FillOrder(ByVal row As DataRowView, ByRef o As Order)
在 C# 中,我将其转换为:
private static void FillOrder(DataRowView row, ref Order o)
从 Order
类中的构造函数,我像这样调用 FillOrder()
方法:
DataView dv = //[get the data]
if (dv.Count > 0)
{
FillOrder(dv[0], this);
}
在 VB 中,这是有效的:
Dim dv As DataView = '[get data]'
If dv.Count > 0 Then
FillOrder(dv.Item(0), Me)
End If
但是,在 VS10 中的 C# 文件中,我在此调用下收到一条红色波形曲线,并出现以下错误:
与[该方法]匹配的最佳重载方法有一些无效参数
这是 VB 中的工作代码。我做错了什么?
I am in the midst of converting a VB library to C#. One of my methods has the following signature in VB:
Private Shared Sub FillOrder(ByVal row As DataRowView, ByRef o As Order)
In C# I converted it to:
private static void FillOrder(DataRowView row, ref Order o)
From my constructor inside my Order
class, I am calling the FillOrder()
method like so:
DataView dv = //[get the data]
if (dv.Count > 0)
{
FillOrder(dv[0], this);
}
In VB, this works:
Dim dv As DataView = '[get data]'
If dv.Count > 0 Then
FillOrder(dv.Item(0), Me)
End If
However, in VS10 in the C# file I am getting a red squiggle under this call with the following error:
The best overloaded method match for [the method] has some invalid arguments
This was working code in VB. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将第二个参数作为
ref
传递。但是,您不能将
this
作为ref
传递(除非它是一个结构体),所以你需要一个临时变量。请注意,该参数几乎绝对不应该是
ref
首先。You need to pass the second parameter as
ref
.However, you cannot pass
this
asref
(unless it's a struct), so you'll need a temporary variable.Note that the parameter almost definitely shouldn't be
ref
in the first place.根据文档,VB中的
ByRef
for 引用类型与 C# 中的ref
不同。它更意味着函数可能会改变变量。因此,只需从函数定义中删除
ref
即可:顺便说一句,您的
Order
是class
还是struct
?According to the documentation,
ByRef
in VB for reference types is not the same asref
in C#. It rather means that the function may change the variable.So, just drop
ref
from the function definition:BTW, is your
Order
aclass
or astruct
?从我记事起,
ByVal
和ByRef
/ref
参数就一直存在混淆。这是我可以解释的最好方法:您只需当且仅当您计划用不同的引用替换该引用时才需要通过引用传递对象em>参考。如果你想改变正在传递的对象的内容,你只需要按值传递它。示例:
是的,名为
me
的Person
实例按值传递到ModifyPerson
中;这只是意味着对实例的引用是按值传递的。函数仍然可以修改该引用的成员。现在,尝试一下:这些函数有所不同,因为它们尝试修改传入的实际引用。
AssignByValue
对名为me
的Person
没有影响因为参数是按值传递的。但是,AssignByReference
可以在调用它的方法中更改该参数的值,因此第二次调用Console.WriteLine(me.LastName)
会反映更新后的引用。As long as I can remember, there has always been confusion about
ByVal
andByRef
/ref
parameters. Here's the best way I can explain it:You only need to pass an object by reference if and only if you plan on replacing that reference with a different reference. If you want to change the contents of the object being passed, you only need to pass it by value. Example:
Yes, the instance of
Person
calledme
is passed intoModifyPerson
by value; that just means the reference to the instance is passed by value. A function can still modify the members of that reference. Now, try this:These functions differ because they try to modify the actual reference being passed in.
AssignByValue
has no effect on thePerson
namedme
because the parameter is passed by value. However,AssignByReference
can change the value of that parameter in the method that called it, hence why the second call toConsole.WriteLine(me.LastName)
reflects the updated reference.