Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

returning multiple object types

Status
Not open for further replies.

moorman12

Programmer
Nov 3, 2007
1
0
0
US
Hi,
I am still learning c# and I had a question regarding returning multiple variable from a function. I pass a point object to a function and I want to return an integer and another point from that function. Is this possible?

function returnTwoObjects(point a)
{
return point and an integer
}

tks
 
Create a class that contains your 2 types and pass that back. This is known as a bean (encapsulation)

public class PointInfo
{
public Point Location;
public int Count;
}


private PointInfo DoSomeWork(point a)
{
//blah blah return a PointInfo object
}

 
Creating a class is common but you also can return as many objects you want using ref or out.
Example:
Code:
void  returnTwoObjects(point a, ref point b, ref int myInt)
{
    b=...;
myInt =...;
}
How to call the function ?
point a=...;
point onePoint=...;
int oneInt =0;
returnTwoObjects(a,ref onePoint, ref oneInt);
//Now the values of onePoint and oneInt were modified by function call.

or
Code:
void  returnTwoObjects(point a, out point b, out int myInt)
{
    b=...;
myInt =...;
}
The ref and out keywords are treated differently at run-time, but they are treated the same at compile time.
The variables passed as an out arguments need not be initialized prior to being passed.

-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top