Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

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*
Posted (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 by mutant
Posted

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

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*
Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...