VB.NET和C#中的Ref参数 [英] By Ref parameters in VB.NET and C#

查看:378
本文介绍了VB.NET和C#中的Ref参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与问题有关的传递参数byRef,我有基于VB.NET的类库,其中某些函数是使用byref参数类型定义的。这些参数是父类对象,当我尝试调用此函数并在byref参数中传递子类对象时,它在VB.NET中有效,但是我无法在C#中做同样的事情

I have question related passing parameters byRef, I have VB.NET based class library in which some functions are defined with byref argument types. These parameters are parent class objects and when I tried to call this function and pass child class object in byref argument it works in VB.NET but I am unable to do same thing in C#

以下是我正在尝试的测试代码

following is test code i am trying

Public Class Father

        Private _Cast As String
        Public Property Cast() As String
            Get
                Return _Cast
            End Get
            Set(ByVal value As String)
                _Cast = value
            End Set
        End Property

    End Class


 Public Class Son
        Inherits Father

        Private _MyName As String
        Public Property Myname() As String
            Get
                Return _MyName
            End Get
            Set(ByVal value As String)
                _MyName = value
            End Set
        End Property


    End Class

VB中的实施类

Public Class Parent

        Public Function Show(ByRef value As Father) As Boolean
            Dim test As String = value.Cast
            Return True
        End Function

// Herer我可以调用Show方法并将子对象传递给ByRef类型参数并有效

// Herer I can call Show method and pass child object to ByRef type argument and it works

Public Function Show2() As Boolean

            Dim s As New Son
            Dim result As Boolean = Show(s)

            Return True
        End Function

    End Class

//但是当我在c#中尝试相同的事情

// But when i try same thing in c#

Parent p = new Parent();
            Son s = new Son();
            Father f = new Father();

            p.Show(ref s);

我收到儿子无法转换为父亲的错误,我已经在VB中测试了它的工作原理,但是我该如何使它在C#中工作?因为我有dll格式的类库

I get error that Son cannot convert to father , I already test it works in VB but how can I make it work in c#? as I have class library in dll format

预先感谢

推荐答案

C#对此非常严格,通过引用传递的变量必须与方法参数类型完全匹配。 VB.NET对此表示宽容,其编译器重写您的代码并创建所需类型的变量。大致这样,用C#表示:

C# is strict about this, a variable that's passed by reference must be an exact match with the method argument type. VB.NET is forgiving about that, its compiler rewrites your code and creates a variable of the required type. Roughly like this, expressed in C#:

    Son s = new Son();
    Father $temp = (Father)s;
    p.Show(ref $temp);
    s = (Son)$temp;

哪个很好,但并非没有问题。失败模式是Show()方法将 new 对象分配给错误类型的参数时。由于参数类型为父亲,因此可以创建父亲对象。但是,这将使上述摘要中的第4条语句失败,无法将父辈投降。并不是很漂亮,错误的语句会引发异常,真正的问题在Show()方法中。您可以花一会儿时间来解决这个问题,这至少是因为在您的VB.NET源代码中实际上看不到演员表。哎哟。 C#强制您明确编写以上代码段,从而解决了您的问题。

Which is nice, but not without problems. The failure mode is when the Show() method assigns a new object to its argument of the wrong type. It is allowed to create a Father object since the argument type is Father. That however will make the 4th statement in the above snippet fail, can't cast Father to Son. That's not so pretty, the exception will be raised at the wrong statement, the true problem is located in the Show() method. You can scratch your head over that for a while, not in the least because the cast is not actually visible in your VB.NET source code. Ouch. C# forces you to write the above snippet explicitly, which solves your problem.

这时您应该惊呼但是等等,Show()方法实际上并没有创建一个新对象!很好的洞察力,您将在代码中找到真正的问题,Show()方法应声明参数ByRef。仅在方法重新分配参数且需要将更改传播回调用方时才应使用它。最好完全避免,一个对象应通过其返回值由方法返回。在VB.NET中代替Sub。

At this point you should exclaim "But wait, the Show() method doesn't actually create a new object!" That's good insight and you'll have found the true problem in this code, the Show() method should not declare the argument ByRef. It should only be used when a method reassigns the argument and that change needs to be propagated back to the caller. Best avoided entirely, an object should be returned by a method by its return value. Function in VB.NET instead of Sub.

这篇关于VB.NET和C#中的Ref参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆