Visual Basic Function Call - multiple variables

I have a Matlab .m file that has a sub-routine (function) that I need to convert to a visual basic compatible version. The Matlab function definition is:

function [X,Y,Z] = convert(A,B, C)

and the call to it is:

[Xr,Yr,Zr] = convert(refA,refB,refC)

Is there a way to call a function in Visual Basic that will return three values (without having to pass them as arguments to the function). Perhaps as a structure or class? I would like to preserve the existing call method that is used in the .m file.

So far no amount of Googling has turned up an answer. Thanks in advance to anyone who has a possible solution.

I’m not familiar with VB, but can a VB function return an array? (Or a pointer to an array?)

Pass your parameters by reference. The syntax is something like this:


Sub foo(ByRef number As Integer)
    number = 2
End Sub
Dim value As Integer = 1
foo(value)

After the call to foo, value contains 2, not 1.

Yes, VB can return either an array or a pointer. But I would either use a structure, or declare the variables at the module level (making them available to both the function and the caller).

Are you talking traditional VB, VBA, or VB.Net? Somewhat different answers apply for the three different cases.

It is Visual Basic 2008 with .Net 3.5 Framework. But I think I have figured out how to make it work. I am just going to pass those values in when I call the function. It’s not as clean as I would have preferred. I would have preferred to have a method for returning multiple values, but that does not appear to be possible with VB. Besides, this was just a short piece of code to validate a GPS algorithm before I insert it into an existing C++ project.

Thanks to all who responed for the input.

Given that we’re talking .Net …

The clean way is to define a struct & have the method populate and return an instance of it.

Another method is to return a Generic<T, T, T> (whether this is newer & better or just more obscure than a declared struct is a religious issue in the .Net world. I lean towards “better” at least for things of lmiited scope & lifetime.)

The less-clean way is to return an array. If all three return items are the same type, (e.g. float), then a float-typed array works. Otherwise you get an array of type object which is very unclean.

Last and most evil you can pass the results as parameters e.g. MyMethod(inputA, inputB, inputC, ref outputQ, ref outputW, ref outputE)
I’m curious why you want to use VB.Net versus C#.Net when you’re a C++ programmer. The one small advantage C# has over VB is you can use the out qualifier on the returning parameters to avoid the need to initialize them in the caller. e.g.

MyMethod(inputA, inputB, inputC, out outputQ, out outputW, out outputE);