Return values from a function
I want to return four values (more than one) from a function to its caller.
Four values are of four datatypes(int, long, double, string).
I just know how to return one value from a function. But how can I return more than one ?
Is it possible ?
plz, Give an example.
--Rajib
Re: Return values from a function
technically a function is only meant to return one value, so you should create a class or structure that will hold your four values.
You could use the out keyword, but it's not a good solution
Re: Return values from a function
Thanks for your help.
But I have found that it is easily solved by Reference passing.
---Rajib
Re: Return values from a function
Something to bear in mind an argument passed by ref must be first initialised. An out does not have to be initialised.
Re: Return values from a function
Can you tell me more about the out keyword?
Re: Return values from a function
Re: Return values from a function
Ah, so the out keyword is quite like those from SPs. Yet another feature I wish VB.NET had. :cry:
Re: Return values from a function
ref exists in VB.NET. When you pass an argument to a function, you can do it ByVal or ByRef.
Re: Return values from a function
An interesting thing with C# is that using out, ref or neither of them for a given function parameter defines different function signatures. This allows the overloading of a function in this way :
Code:
void testfunction(int iVal)
{
}
void testfunction(ref int iVal)
{
}
void testfunction(out int iVal)
{
}
All is well if used only in C#, but you cannot use this function in VB through a compiled assembly because it is ambiguious in VB because in VB you don't tell the compiler that you want to send your function parameter ByVal or ByRef.
Re: Return values from a function
Code:
//this is a valid overload
void testfunction(int iVal){}
void testfunction(ref int iVal){}
//so is this
void testfunction(int iVal){}
void testfunction(out int iVal){}
//this is not a valid overload
void testfunction(ref iVal){}
void testfunction(out int iVal){}
Re: Return values from a function
My bad, I assumed that it would be valid too, I never tried to mix out and ref before...