I have a code with a subroutine and a function. I use the subroutine to load the numbers and the function to add them. I pass the parameters to the function by ByRef but the result returns 0.
What am I doing wrong?
**Sub Main(args As String())
Dim num1, num2, num3 As UShort
CargarNumeros(num1, num2, num3)
Console.WriteLine("La suma de estos numeros es: " & Sumar(num1, num2, num3))
End Sub
Sub CargarNumeros(num1, num2, num3)
Console.WriteLine("Ingresar num1: ")
num1 = Console.ReadLine()
Console.WriteLine("Ingresar num2: ")
num2 = Console.ReadLine()
Console.WriteLine("Ingresar num3: ")
num3 = Console.ReadLine()
End Sub
Function Sumar(ByRef num1 As UShort, ByRef num2 As UShort, ByRef num3 As UShort) As UShort
Return num1 + num2 + num3
End Function**
By default, every variable in .net (be it c#, vb or whatever) is passed by value (
ByVal
in VB).Therefore, when you pass those variables to the function
Sub CargarNumeros(num1, num2, num3)
, you are not passing the same variables, but a copy of them.When you load values within that procedure, you are loading values to the copies, and when you return from that procedure, the variables have the same value they had originally....