[vb.net] Calling a Sub and returning a value

This might seem like an insanely easy question, but I can't seem to find an answer anywhere to it. I'd like to think that I am decent at VB but while I was learning javascript the other day I found something that seemed awesome and now I can't figure out how to do it in VB.

In javascript it looks like this:

var someValue = getThatValue()

It's both calling and setting the value from the getThatValue() sub. what is the VB equivalent?


Edit

I've tried doing this:

   private sub main()
       dim value = getValue()
       'do something with value
   end sub

   private sub getValue()
       return 3
   end sub

That doesn't seem to work, how can I get that to work?

This question is related to vb.net visual-studio

The answer is


You should be using a Property:

Private _myValue As String
Public Property MyValue As String
    Get
        Return _myValue
    End Get
    Set(value As String)
        _myValue = value
     End Set
End Property

Then use it like so:

MyValue = "Hello"
Console.write(MyValue)

Sub don't return values and functions don't have side effects.

Sometimes you want both side effect and return value.

This is easy to be done once you know that VBA passes arguments by default by reference so you can write your code in this way:

Sub getValue(retValue as Long)
    ...
    retValue = 42 
End SUb 

Sub Main()
    Dim retValue As Long
    getValue retValue 
    ... 
End SUb