# Visual Basic Function Call - multiple variables

**URL:** <https://boards.straightdope.com/t/visual-basic-function-call-multiple-variables/510904>\
**Category:** Factual Questions\
**Created:** [September 21, 2009, 2:46am UTC](https://boards.straightdope.com/t/visual-basic-function-call-multiple-variables/510904 "2009-09-21T02:46:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mountainbiker](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@mountainbiker](https://boards.straightdope.com/u/mountainbiker)\
**Post date:** [September 21, 2009, 2:46am UTC](https://boards.straightdope.com/t/visual-basic-function-call-multiple-variables/510904/1 "2009-09-21T02:46:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [September 21, 2009, 2:51am UTC](https://boards.straightdope.com/t/visual-basic-function-call-multiple-variables/510904/2 "2009-09-21T02:51:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rysto](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Rysto](https://boards.straightdope.com/u/Rysto)\
**Post date:** [September 21, 2009, 3:01am UTC](https://boards.straightdope.com/t/visual-basic-function-call-multiple-variables/510904/3 "2009-09-21T03:01:41Z")

</div>

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

```auto

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.

---

<div class="post-metadata">

**Author:** ![OttoDaFe](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ottodafe/32/356_2.png) [@OttoDaFe](https://boards.straightdope.com/u/OttoDaFe)\
**Post date:** [September 21, 2009, 5:29am UTC](https://boards.straightdope.com/t/visual-basic-function-call-multiple-variables/510904/4 "2009-09-21T05:29:25Z")

</div>

> [@friedo](#):
>
> I’m not familiar with VB, but can a VB function return an array? (Or a pointer to an array?)

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).

---

<div class="post-metadata">

**Author:** ![LSLGuy](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lslguy/32/5813_2.png) [@LSLGuy](https://boards.straightdope.com/u/LSLGuy)\
**Post date:** [September 21, 2009, 1:07pm UTC](https://boards.straightdope.com/t/visual-basic-function-call-multiple-variables/510904/5 "2009-09-21T13:07:52Z")

</div>

Are you talking traditional VB, VBA, or [VB.Net](http://VB.Net)? Somewhat different answers apply for the three different cases.

---

<div class="post-metadata">

**Author:** ![mountainbiker](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@mountainbiker](https://boards.straightdope.com/u/mountainbiker)\
**Post date:** [September 22, 2009, 2:24am UTC](https://boards.straightdope.com/t/visual-basic-function-call-multiple-variables/510904/6 "2009-09-22T02:24:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![LSLGuy](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lslguy/32/5813_2.png) [@LSLGuy](https://boards.straightdope.com/u/LSLGuy)\
**Post date:** [September 22, 2009, 1:14pm UTC](https://boards.straightdope.com/t/visual-basic-function-call-multiple-variables/510904/7 "2009-09-22T13:14:20Z")

</div>

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](http://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);
