function returns
*Edit* how do i separate the code from the actual text?
Hi. i'm reading about classes and how to create fields, properties and methods so far i'm doing just far everything seems strait foward but one thing that i bumped into... a function must return a value in visual basic but in this example (http://msdn2.microsoft.com/en-us/library/f8f40dwh.aspx)
PrivateFunction CalcAge(ByVal yearAsInteger)AsInteger
CalcAge = My.Computer.Clock.LocalTime.Year - year
EndFunction
is CalcAge actually the equvalent of return CalcAge; ?
[906 byte] By [
.Adrian] at [2007-12-25]
a function that returns a value - the signature of your method/function is correct, now you need to return the value back:
Dim CalcAge as Integer = My.Computer.Clock.LocalTime.Year - year
return CalcAge
this returns the value back to the caller - the return type must be the same as the function return type declaration
There is nothing wrong with this function: the function will return the calculated value by assigning it to the function name (CalcAge):
Private Function CalcAge(ByVal year As Integer) As Integer
CalcAge = My.Computer.Clock.LocalTime.Year - year
End Function
This is functionally equivalent to:
Private Function CalcAge(ByVal year As Integer) As Integer
Return My.Computer.Clock.LocalTime.Year - year
End Function
You do not need to use 'return' to return a value (even though there are subtle differences).
i wasn't asking what was wrong with the function just wanted to make sure that return CalcAge was the same as CalcAage = .....
also how do you separate the code from the text here?
well I guess thats personal preference but I always, and many of my colleagues, always use the keyword return so we know its going to return something...I guess having a C# background enforces you to always use the return keyword when returning a value back to the caller (yes I know this is a VB.NET forum...)
to seperate code from text, enter:
[ code language = "vb" ]
[ /code ]
without the spaces, except between "code" and "language" tags
hehe yeah same. i never new that you could return something like that i always used return to return something. as i said thats not my code it's an example i'm following from msdn library and just wanted to make sure i understiid the concept :)
Thanks for all the replys, fast and helpful.