adding numbers with OOP

ThePentiumGuy

Senior Contributor
Joined
May 21, 2003
Messages
1,113
Location
Boston, Massachusetts
i have a class called add with a function called number
publc function number(byval mynumber as integr)
mynumber +=1
end function

in my main program(globals):
dim add as new add
dim x as integer = 3
(button1.click)
add.number(x)
Messagebox.sjow(x)

but its always 3? how come?
 
If you want to do it like this you have to pass the number to function as reference, which will keep oparating on the variable that you passed in itself instead of just getting the value of it and using it:
Visual Basic:
public Sub number(ByRef mynumber As Integer) 'ByRef instead of ByVal
Or if you want to pass it by value then you have to do this:
Visual Basic:
public function number(byval mynumber as integer) As Integer
mynumber +=1
Return mynumber
end function
'and then assign the result to the number
x = add.number(x)
Messagebox.Show(x)
 
Last edited:
ahh i see, thanks(the first method seems easier)
but on the sencond method could u do this:
Visual Basic:
Public Function number(ByVal mynumber As Integer) As Integer
mynumber+=1
return mynumber 'this doesnt work
End Function

x = add.number(x)
Messagebox.Show(x)
but anyway, thanks. im new to OOP and ive found out that its helpful :) Really helpful
 
Visual Basic:
Public Shared Function number(ByVal n as Integer) As Integer
  Return n + 1
End Function

' You don't need to declare an instance first since I made the function "Shared".
x = Add.Number(x)
MessageBox.Show(x)
I don't think changing a ByVal parameter has any effect.
 
Back
Top