adding numbers with OOP

ThePentiumGuy

Well-known member
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:
Code:
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:
Code:
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 by a moderator:
ahh i see, thanks(the first method seems easier)
but on the sencond method could u do this:
Code:
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
 
Code:
Public Shared Function number(ByVal n as Integer) As Integer
  Return n + 1
End Function

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