1

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 can muck in the code base as little as possible.

Community
  • 1
  • 1
Connor Albright
  • 723
  • 4
  • 13
  • 29
  • The title and your question don't agree. I assume you want to pass the ParamArray by reference? By value is the default (and only) option in VB.NET. Please check your question title. – Cody Gray - on strike Nov 10 '10 at 16:46
  • It doesn't make any sense. You expect the callee to create a *new* param array and pass it back to the caller? – Hans Passant Nov 10 '10 at 16:55
  • The `%` type declaration character means `Integer` in VB6; I assume it's the same in TurboBasic? If so, it seems to me that the easiest thing to do is just pass the `Input` function an array of type `Integer`. Where are the variables that get filled by the `Input` method declared in the code—do they have a global scope or are they defined earlier in the same method that calls `Input`? Or is this one of those "auto-instantiate a variable wherever I pretend that one exists" languages? – Cody Gray - on strike Nov 10 '10 at 17:15
  • It appears that TurboBasic is quite similar to VB6. Input() should also accept strings. Most variables have global scope. – Connor Albright Nov 10 '10 at 18:14
  • I'm pretty sure this is impossible. – SLaks Nov 10 '10 at 18:38

3 Answers3

2

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • While I suspect you've correctly guessed what the OP wants to do (effectively pass *each* of a variable number of arguments `ByRef`), I think it's worth pointing out that this would *still* not be possible if you passed the array generated from a `ParamArray` parameter `ByRef`—only the array variable itself could be set to a new value (which, as SLaks pointed out, would be pointless); you still couldn't reach inside that array and find references to local variables from the calling method's scope. – Dan Tao Nov 10 '10 at 16:49
  • Im actually porting from TurboBasic(ever heard of it, I hadent). Unfortunately many of the built in functions made use of the ability to pass byref(there wasent any other way). I was hoping to get by without heavily rewriting the section to accept this behavior, but looks like I am out of luck. – Connor Albright Nov 10 '10 at 16:54
  • @Oak: Definitely heard of it, but that doesn't mean I know it. :-) We might be able to come up with some other less daunting workarounds if you post the section of code that currently only works with a `ParamArray` passed `ByRef`. I for one am curious what the callee is doing with the param array. – Cody Gray - on strike Nov 10 '10 at 16:58
  • I asked about ParamArray as that was the best equivalent I could find. I modified my question above to give an example of what I am trying to do. – Connor Albright Nov 10 '10 at 17:04
2

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.

MarkJ
  • 30,070
  • 5
  • 68
  • 111
  • Thanks! This is probably the simplest solution so far. – Connor Albright Nov 10 '10 at 19:48
  • 1
    @Oak Thanks for accepting my answer! I think the only answer to your original question was "this is impossible". I took advantage of your background information to give advice on the specific code problem that prompted your original question. – MarkJ Nov 11 '10 at 16:08
1

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964