function problems

  • Thread starter Thread starter Mehul Malhotra
  • Start date Start date
M

Mehul Malhotra

Hi I just wanted to know how to create a function that
passes more than one variable back if possible?

Thanks a lot
 
* "Mehul Malhotra said:
Hi I just wanted to know how to create a function that
passes more than one variable back if possible?

\\\
Public Function Foo(ByRef x As Integer, ByRef y As Integer)
x = 10
y = 120
End Function
..
..
..
Dim x, y As Integer
Foo(x, y)
MsgBox(x.ToString())
MsgBox(y.ToString())
///
 
Hi Mehul,

As is demonstrated in Herfried's example, any variable that is passed byref
will come back changed as however it may have been changed inside the
function. This is true in subs as well.

HTH,

Bernie Yaeger
 
* "Bernie Yaeger said:
As is demonstrated in Herfried's example, any variable that is passed byref
will come back changed as however it may have been changed inside the
function. This is true in subs as well.

For reference type, passing the variable 'ByVal' can be useful too when
manipulating the object referenced by the variable passed to the
parameter.
 
Alternatively, you might want to assign a new reference to an object. For
example.( Members declared public for brevity only ).

Class Person
Public Age as Integer
Public Name as String
End Class

Public Function TooOld( ByRef P as Person ) As Person

If P.Age < 21 Then
P.Name="Too Young"
Return P
Else
Return P
End If

End Function


Regards - OHM
 
Duh !!!. I just realised what you meant by that,maybe I should have written
the following.

If P.Age < 18 Then
P.Name="Too Young To Vote!"
. . .



Regards - OHM

---------------------------------------
 
* "One Handed Man said:
Alternatively, you might want to assign a new reference to an object. For
example.( Members declared public for brevity only ).

Class Person
Public Age as Integer
Public Name as String
End Class

Public Function TooOld( ByRef P as Person ) As Person

If P.Age < 21 Then
P.Name="Too Young"
Return P
Else
Return P
End If

End Function

What's the purpose of doing that?
 
* "One Handed Man said:
Duh !!!. I just realised what you meant by that,maybe I should have written
the following.

If P.Age < 18 Then
P.Name="Too Young To Vote!"

Too young to code in VB.NET.

SCNR
 
The OP's question was to find out how a function could return more than one
peice of data. Allowing the parameters passed to be changed does not
address that question.

Regards - OHM
 
Funnily enough, My son ( 13 and a half ) has started to learn VB.NET and
he's working through a step-by-step book, amazingly he is grasping the
concepts very quickly and is demonstrating this by augmenting the examples
and labs for himself.

Regards - OHM
 
* "One Handed Man said:
The OP's question was to find out how a function could return more than one
peice of data. Allowing the parameters passed to be changed does not
address that question.

There are different ways:

1. Return a structure.
2. Return the data in reference parameters.
 
* "One Handed Man said:
Funnily enough, My son ( 13 and a half ) has started to learn VB.NET and
he's working through a step-by-step book, amazingly he is grasping the
concepts very quickly and is demonstrating this by augmenting the examples
and labs for himself.

Why didn't you give him an assembler book?

SCNR again
 
Hi OHM,

I think that Herfried, means why do you do it like this
And not just

Public Function TooOld( ByVal P as Person ) As Person

I also do not like ByRef, (and read the thread this time good)

:-)))))

Cor
 
He will get the assembler book once he has his MVP status like you, after
all there are only a few years difference between your age and his !

SCNR . . .

Regards - OHM
 
Why bother using a structure, I dont see any benefit. After all, you can do
so much more with a class.
 
* "One Handed Man said:
Why bother using a structure, I dont see any benefit. After all, you can do
so much more with a class.

Sorry, I wanted to write class/structure.
 
* "Cor said:
I think that Herfried, means why do you do it like this

And not just

Public Function TooOld( ByVal P as Person ) As Person

I also do not like ByRef, (and read the thread this time good)

I remember the times when it was "best practice".

;-)
 
Back
Top