471,306 Members | 848 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

How to return more than one value from an function

Im using an C#'s user defined private function on which i want to return more
than one value (like int and string[] ) and from that function.

please let me know in advance..

assankhan Ismail
Nov 19 '05 #1
4 1862
Hi Dear AssanKhan Ismail,

You can return more than one (1) value or type by constructing a class and
returning that class in your function.

for example:
========

Construct a class like this with 4 Different Types like bool, int, double,
string

public class Different_Types
{
public Different_Types()
{
//
// TODO: Add constructor logic here
//
}

int m_int;
string m_str;
bool m_bool;
double m_double;

public int PassInteger
{
get {return m_int;}
set {m_int = value;}
}

public string PassString
{
get {return m_str;}
set {m_str = value;}
}
public bool PassBoolean
{
get {return m_bool;}
set {m_bool = value;}
}
public double PassDouble
{
get {return m_double;}
set {m_double = value;}
}
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Then Create you Function which return 4 Different Types of Values ( but all
the 4 different types of values pass through the object which is created the
class above

function name is = Returen_Many_Values_CLASS

it returns the type of object = Different_Types
for this object you have created a class above

public Different_Types Returen_Many_Values_CLASS()
{
Different_Types diff_types = new Different_Types();
diff_types.PassBoolean=false;
diff_types.PassDouble=3.142;
diff_types.PassInteger=67;
diff_types.PassString="Venkat";
return diff_types;
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Use the above function in a button click and populate 4 different textboxes

for this you have to place 4 different textboxes and one command button

private void button1_Click(object sender, System.EventArgs e)
{
this.textBox1.Text=Returen_Many_Values_CLASS().Pas sBoolean.ToString();
this.textBox2.Text=Returen_Many_Values_CLASS().Pas sDouble.ToString();
this.textBox3.Text=Returen_Many_Values_CLASS().Pas sInteger.ToString();
this.textBox4.Text=Returen_Many_Values_CLASS().Pas sString;

}

You can test this one in one dummy windows application in .net

for Anything and Everything, Please Let Me Know

bye
Venkat_KL

Nov 19 '05 #2
Hi Dear AssanKhan Ismail,

You can even do with struct

for that first create a struct like below

struct MyStruct
{
public int x;
public string y;
}
------------------------------------------------------------------------------------------------
then create you function which returns you struct

private MyStruct MyStruct_Returning_Method()
{
MyStruct ms = new MyStruct();
ms.x=121;
ms.y="venkat";
return ms;
}

Name of the Function=MyStruct_Returning_Method
Return type=MyStruct

Give value to both the type like int and string (I have given as 121 &
"venkat")

------------------------------------------------------------------------------------------------

Place two textboxex and one command button on the windows form to test

In the button click event write this code
private void button1_Click(object sender, System.EventArgs e)
{
this.textBox1.Text=MyStruct_Returning_Method().x.T oString();
this.textBox2.Text=MyStruct_Returning_Method().y;
}

Test and for anything and everything, please let me know

bye
Venkat_KL
Nov 19 '05 #3
Hi Dear Venkat

Your Code is very helpful to me.
Thanks a lot for your timely help.

bye
assankhan

"Venkat_KL" wrote:
Hi Dear AssanKhan Ismail,

You can return more than one (1) value or type by constructing a class and
returning that class in your function.

for example:
========

Construct a class like this with 4 Different Types like bool, int, double,
string

public class Different_Types
{
public Different_Types()
{
//
// TODO: Add constructor logic here
//
}

int m_int;
string m_str;
bool m_bool;
double m_double;

public int PassInteger
{
get {return m_int;}
set {m_int = value;}
}

public string PassString
{
get {return m_str;}
set {m_str = value;}
}
public bool PassBoolean
{
get {return m_bool;}
set {m_bool = value;}
}
public double PassDouble
{
get {return m_double;}
set {m_double = value;}
}
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Then Create you Function which return 4 Different Types of Values ( but all
the 4 different types of values pass through the object which is created the
class above

function name is = Returen_Many_Values_CLASS

it returns the type of object = Different_Types
for this object you have created a class above

public Different_Types Returen_Many_Values_CLASS()
{
Different_Types diff_types = new Different_Types();
diff_types.PassBoolean=false;
diff_types.PassDouble=3.142;
diff_types.PassInteger=67;
diff_types.PassString="Venkat";
return diff_types;
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Use the above function in a button click and populate 4 different textboxes

for this you have to place 4 different textboxes and one command button

private void button1_Click(object sender, System.EventArgs e)
{
this.textBox1.Text=Returen_Many_Values_CLASS().Pas sBoolean.ToString();
this.textBox2.Text=Returen_Many_Values_CLASS().Pas sDouble.ToString();
this.textBox3.Text=Returen_Many_Values_CLASS().Pas sInteger.ToString();
this.textBox4.Text=Returen_Many_Values_CLASS().Pas sString;

}

You can test this one in one dummy windows application in .net

for Anything and Everything, Please Let Me Know

bye
Venkat_KL

Nov 19 '05 #4
Hi Dear Venkat

Your alternate Coding sample adds more values to my doubts.
Thanks a lot for your timely help.

bye
assankhan

"Venkat_KL" wrote:
Hi Dear AssanKhan Ismail,

You can even do with struct

for that first create a struct like below

struct MyStruct
{
public int x;
public string y;
}
------------------------------------------------------------------------------------------------
then create you function which returns you struct

private MyStruct MyStruct_Returning_Method()
{
MyStruct ms = new MyStruct();
ms.x=121;
ms.y="venkat";
return ms;
}

Name of the Function=MyStruct_Returning_Method
Return type=MyStruct

Give value to both the type like int and string (I have given as 121 &
"venkat")

------------------------------------------------------------------------------------------------

Place two textboxex and one command button on the windows form to test

In the button click event write this code
private void button1_Click(object sender, System.EventArgs e)
{
this.textBox1.Text=MyStruct_Returning_Method().x.T oString();
this.textBox2.Text=MyStruct_Returning_Method().y;
}

Test and for anything and everything, please let me know

bye
Venkat_KL

Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Ravindranath Gummadidala | last post: by
8 posts views Thread by gregory_may | last post: by
7 posts views Thread by Terry Olsen | last post: by
49 posts views Thread by Davy | last post: by
18 posts views Thread by Carl Forsman | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.