By Ref parameters in VB.NET and C #

I have a question related to passing parameters byRef, I have a library of classes based on VB.NET in which some functions are defined using argument types byref. These parameters are objects of the parent class, and when I tried to call this function and pass the object of the child class in the byref argument, it works in VB.NET, but I cannot do the same in C #

the next one is the test code I'm 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 implementation class

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

// Herer I can call the Show method and pass the child into an argument of type ByRef, 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 

// But when I try to do the same in C #

 Parent p = new Parent(); Son s = new Son(); Father f = new Father(); p.Show(ref s); 

I get an error that the Son cannot convert to a father, I am already testing it in VB, but how can I get it to work in C #? since I have a class library in dll format

Thank you in advance

+6
source share
2 answers

C # strictly talks about this, a variable that is passed by reference must be an exact match with the type of the argument of the method. VB.NET forgives this, its compiler overwrites your code and creates a variable of the required type. Something like that expressed in C #:

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

This is good, but not without problems. Failure mode is when the Show () method assigns a new object to its argument of the wrong type. It is allowed to create an object of the Father, since the argument type is "Father". This, however, will lead to the fact that the 4th statement in the above fragment does not succeed, cannot give the Father to the Son. This is not so beautiful, an exception will be raised if the statement is incorrect, the true problem is in the Show () method. You can scratch your head over this for a while, in no case, because the actor is not actually displayed in the VB.NET source code. Uch. C # forces you to explicitly write the above snippet, which solves your problem.

At this point, you should exclaim: "But wait, the Show () method does not actually create a new object!" This is a good understanding, and you will find the true problem in this code, the Show () method should not declare the ByRef argument. It should be used only when the method reassigns the argument, and this change should be passed back to the caller. It is best to avoid the target, the object should be returned by the method according to its return value. Function in VB.NET instead of Sub.

+10
source

ByRef allows a function to modify a managed pointer and point to something other than Son , so C # will not let you pass a managed pointer to Son directly. However, you can do this:

 Son s = new Son(); Father f = s; p.Show(ref f); s = (Son)f; //Success if f still points to a Son, InvalidCastException otherwise. 

However, if your Show method does not really change the managed pointer, then there is no reason to accept ByRef : just pass it to ByVal and you can still change the object itself.

+4
source

Source: https://habr.com/ru/post/946252/


All Articles