开发者

Using ParamArray ByRef

开发者 https://www.devze.com 2023-01-24 23:34 出处:网络
Is there any way to use ParamArray ByRef? Barring that, it there a workaround that accomplishes the same thing?

Is there any way to use ParamArray ByRef? Barring that, it there a workaround that accomplishes the same thing?

I could do some overloads, but I am trying to avoid the clunkiness.

Background: Porting over some old code, and trying to keep the same structure as much as possible.


EDIT

A specific example of what I want:

I have some code in turboBasic that I am porting to vb.net. The code has built in functions such as

Input#1,Data$(I%,1),Data$(I%,2),Data$(I%,3)

Where Input() gets file # 1 and reads three pieces of data from it and assigns it to the three variables shown. I would like to replicate this behavior with my own Input() function. To do that, how would I take in three(or more, or less) variables and assign values to them?

Ideally I would accomplish this by only modifying my own definition of Input(), so I c开发者_运维技巧an muck in the code base as little as possible.


I've never used TurboBasic but the syntax looks identical to the VB6 Input# statement, so I'm guessing the semantics are also the same.

This VB6 code

Input #1,Data$(I%,1),Data$(I%,2),Data$(I%,3)

Is equivalent to this VB.Net

Input(1,Data$(I%,1))
Input(1,Data$(I%,2))
Input(1,Data$(I%,3))

The VB.Net upgrade wizard converts the VB6 Input # statement like this. I would just port the code like this, rather than implementing your own function. Converting from VB6 to VB.net requires substantial edits in the code base, I would expect TurboBasic to be even more demanding.


There is currently no way to pass ParamArrays by reference in VB.NET. To pass a series of specified values to a method that accepts a ParamArray, the CLR fills an array of appropriate length with the values you specified, and then passes this array into the method you called. There is no way to copy all of the values out of the array that was passed and back into the original variables.

I assume that you are porting code from VB6 where the only way to pass ParamArrays is by reference, but you shouldn't have any need for this functionality in .NET.


Unless you are assigning the parameter to a new array instance and expecting the caller (which is passing a variable or field) to see the new instance, you don't need ByRef.

Even without ByRef, the caller will still see changes to the contents of the array. (Arrays are reference types).

If you don't see parameterName = something in the method, you don't need ByRef.

The point of a ParamArray parameter is to pass it an implicitly created array.
In such usages, ByRef cannot have any effect.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号