ThePentiumGuy Posted August 2, 2003 Posted August 2, 2003 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? Quote My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!) vbprogramming.8k.com My Project (Need VB.NET Programmers) http://workspaces.gotdotnet.com/ResolutionRPG
*Experts* mutant Posted August 2, 2003 *Experts* Posted August 2, 2003 (edited) 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: 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: 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) Edited August 2, 2003 by mutant Quote
ThePentiumGuy Posted August 2, 2003 Author Posted August 2, 2003 ahh i see, thanks(the first method seems easier) but on the sencond method could u do this: 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 Quote My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!) vbprogramming.8k.com My Project (Need VB.NET Programmers) http://workspaces.gotdotnet.com/ResolutionRPG
*Experts* Volte Posted August 2, 2003 *Experts* Posted August 2, 2003 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.