Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing an array as parameter by reference 1

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
571
US
Colleagues,
I need to pass an array as parameter, naturally - by reference.
All I could find in the MS documentation was ParamArray (In this article, the parameter array is not pre-populated (if that's the word) array, but is simply a list of values, literals.
Here's the code snippet:

Code:
'In the calling proc
If Not CheckFINFiles(lcSrcDir, gcLogFile, laFiles(3, lnRows)) Then
   lnRet = -1
   Write2Log(gcLogFile, gcLogStr, True)
   Environment.Exit(lnRet)
End If

The called function's signature (screenshot taken in VS 2012 IDE):

20200821_ParamArray_Passing_Problem_ofur70.jpg


Apparently (see the red-circled parts in the picture), this ParamArray takes only a list of literals. This is not what I need.
In other programming languages I know, there's a pointer to that array parameter that can be passed into a called subroutine.

So, my question is:
How, in VB .NET, do I pass into a subroutine an array as parameter by reference?
TIA!

Regards,

Ilya
 
It looks to me that you defined:[tt]
lcSrcDir As String
gcLogFile As String
laFiles As [red]???[/red][/tt]


---- Andy

There is a great need for a sarcasm font.
 
If you want to know how the array was declared and filled - here it is:

Code:
Dim lnRows As Int16 = 0, laFiles(3, lnRows) As String

lcLogStr = Now.ToString("yyyy-MM-dd HH:mm:ss") & ": Cursor reading results" & vbCrLf

Do While loDataRdr.Read()
   ReDim Preserve laFiles(3, lnRows)
   laFiles(0, lnRows) = loDataRdr.Item(2)
   laFiles(1, lnRows) = loDataRdr.Item(3)
   laFiles(2, lnRows) = loDataRdr.Item(4)
   laFiles(3, lnRows) = loDataRdr.Item(5)

   If glDevelop Then ' For development/testing purposes only
      lcLogStr = lcLogStr & laFiles(0, lnRows) & ", " & (Trim(laFiles(1, lnRows)) & ",").PadRight(48) & _
                 (Trim(laFiles(2, lnRows).PadRight(48)) & ",").PadRight(48) & laFiles(3, lnRows) & vbCrLf
   End If

   lnRows += 1
Loop

HTH.

On the footnote: I could have made this array global (Public), but my C/C3/VFP "upbringing" gets on the way... ;-)

Regards,

Ilya
 
A ParamArray* is somewhat different beast from what you want, which is simply an array as a parameter (they cannot be multidimensional, for example). Passing arrays is dead easy in vb.net, just as it was in VB6 ...

Code:
[blue]   Public Sub Example()
        Dim wombat(3, 2) As Integer
        wombat(1, 1) = 5
        MsgBox(spoon(wombat))
    End Sub

    Public Function spoon(ByRef fred(,) As Integer) As Boolean
        MsgBox(fred(1, 1))
    End Function[/BLUE]

* You've also seem to have misunderstood the compiler warning, this is nothing to do with literals. it is telling you that, since you have provided explicit types to other parameters you must also do so for the ParamArray - so 'As String' in this case
 
Thank you StrongM!
This works!
Issue's resolved, case being closed.

[thanks]

Regards,

Ilya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top