Return Values From A Function

Hello Experts -

Got a question. I've got a simple function that conceptually looks something like this:

Public Function DoSomething ( Value1, Value2 )
      blah, blah...
      DoSomething = 123
End Function

Now let's say that in the process of arriving at the result ("123"), Value1 changes from what was the intially passed value. Let's say Value1 was 1. And, in the course of calculating, Value1 becomes the value 5.

It seems in VB.NET that to the calling program, Value1 always remains a 1. That's both before and after the function is called.

This seems to be very desirable because it prevents the calling program from becoming contaminated from bugs in the Function DoThing.

But, how about on occasions when you would like more than one value being returned. You wanted to know if Value1 had changed and you want to know its new value.

Is it possible to tell VB.NET that you want Value1 to change when control is returned to the original program?

Sorry, but Iif the answer seems obvious , you should realize that I never took a programming course.


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The parameter should be passed as ByRef instead of ByVal if you want to keep the changes.

Public Function DoSomething ( ByRef Value1, ByRef Value2 )
      blah, blah...
      DoSomething = 123
End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
This could be accomplished in a couple of ways, but the easiest way would be to Dim the variables you want in the calling function, then pass them ByRef to DoSomething.  This way, any changes to any of the variables is automatically updated in the calling function.  Example:
Public Sub Main()
    Dim intVariable1 As Integer
    Dim intVariable2 As Integer
    Dim intResult As Integer
    intVariable1 = 1
    intVariable2 = 5
    intResult = 0

    intResult = DoSomething(intVariable1, intVariable2)
End Sub

Private Function DoSomething(ByRef Value1 As Integer, ByRef Value2 As Integer) As Integer
    Dim intCalculation As Integer
    Value1 = Value2 + 2
    intCalculation = Value1 * Value2
    Return intCalculation
End Function

Open in new window

Here's a decent explanation:
gbmcneilAuthor Commented:
Gee, I appreciate your input here G Hosa Phat. But, I think that wellhole answered the question first.

And, his solution works.

Many thanks to both of you. I obviously didn't know the difference between ByVal and ByRef. Now, I do.
All good.  I can get a bit carried away sometimes.  Glad you got what you needed.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.