方法参数与签名匹配,但仍然出现错误

发布于 2024-10-08 13:40:08 字数 721 浏览 6 评论 0原文

我正在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魂牵梦绕锁你心扉 2024-10-15 13:40:08

您需要将第二个参数作为 ref 传递。
但是,您不能将 this 作为 ref 传递(除非它是一个结构体),所以你需要一个临时变量。

请注意,该参数几乎绝对不应该是 ref 首先。

You need to pass the second parameter as ref.
However, you cannot pass this as ref (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.

橘和柠 2024-10-15 13:40:08

根据文档,VB中的ByRef for 引用类型与 C# 中的 ref 不同。它更意味着函数可能会改变变量。

因此,只需从函数定义中删除 ref 即可:

private static void FillOrder(DataRowView row, Order o)

顺便说一句,您的 Orderclass 还是 struct

According to the documentation, ByRef in VB for reference types is not the same as ref in C#. It rather means that the function may change the variable.

So, just drop ref from the function definition:

private static void FillOrder(DataRowView row, Order o)

BTW, is your Order a class or a struct?

め七分饶幸 2024-10-15 13:40:08

从我记事起,ByValByRef/ref 参数就一直存在混淆。这是我可以解释的最好方法:

您只需当且仅当您计划用不同的引用替换该引用时才需要通过引用传递对象em>参考。如果你想改变正在传递的对象的内容,你只需要按值传递它。示例:

Public Class Person
    Public Property FirstName As String
    Public Property LastName As String
End Class

Public Shared Sub ModifyPerson(ByVal someone As Person)
    ' Passed by value          ^^^^^

    someone.LastName = "Doe"
End Sub

Public Shared Sub Main(ByVal args() As String)
    Dim me As New Person
    me.FirstName = "Adam"
    me.LastName = "Maras"

    ModifyPerson(me)

    Console.WriteLine(me.LastName) ' Writes "Doe"
End Sub

是的,名为 mePerson 实例按值传递到 ModifyPerson 中;这只是意味着对实例的引用是按值传递的。函数仍然可以修改该引用的成员。现在,尝试一下:

Public Shared Sub Main(ByVal args() As String)
    Dim me As New Person
    me.FirstName = "Adam"
    me.LastName = "Maras"

    AssignByValue(me)
    Console.WriteLine(me.LastName) ' Writes "Maras"

    AssignByReference(me)
    Console.WriteLine(me.LastName) ' Writes "Doe"
End Sub

Public Shared Sub AssignByValue(ByVal someone As Person)
    Dim new As New Person
    new.FirstName = "John"
    new.LastName = "Doe"

    someone = new
End Sub

Public Shared Sub AssignByReference(ByRef someone As Person)
    Dim new As New Person
    new.FirstName = "John"
    new.LastName = "Doe"

    someone = new
End Sub

这些函数有所不同,因为它们尝试修改传入的实际引用。AssignByValue 对名为 mePerson 没有影响因为参数是按值传递的。但是,AssignByReference 可以在调用它的方法中更改该参数的值,因此第二次调用 Console.WriteLine(me.LastName) 会反映更新后的引用。

As long as I can remember, there has always been confusion about ByVal and ByRef/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:

Public Class Person
    Public Property FirstName As String
    Public Property LastName As String
End Class

Public Shared Sub ModifyPerson(ByVal someone As Person)
    ' Passed by value          ^^^^^

    someone.LastName = "Doe"
End Sub

Public Shared Sub Main(ByVal args() As String)
    Dim me As New Person
    me.FirstName = "Adam"
    me.LastName = "Maras"

    ModifyPerson(me)

    Console.WriteLine(me.LastName) ' Writes "Doe"
End Sub

Yes, the instance of Person called me is passed into ModifyPerson 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:

Public Shared Sub Main(ByVal args() As String)
    Dim me As New Person
    me.FirstName = "Adam"
    me.LastName = "Maras"

    AssignByValue(me)
    Console.WriteLine(me.LastName) ' Writes "Maras"

    AssignByReference(me)
    Console.WriteLine(me.LastName) ' Writes "Doe"
End Sub

Public Shared Sub AssignByValue(ByVal someone As Person)
    Dim new As New Person
    new.FirstName = "John"
    new.LastName = "Doe"

    someone = new
End Sub

Public Shared Sub AssignByReference(ByRef someone As Person)
    Dim new As New Person
    new.FirstName = "John"
    new.LastName = "Doe"

    someone = new
End Sub

These functions differ because they try to modify the actual reference being passed in. AssignByValue has no effect on the Person named me 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 to Console.WriteLine(me.LastName) reflects the updated reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文