How to return more than one value from an function

G

Guest

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
 
G

Guest

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().PassBoolean.ToString();
this.textBox2.Text=Returen_Many_Values_CLASS().PassDouble.ToString();
this.textBox3.Text=Returen_Many_Values_CLASS().PassInteger.ToString();
this.textBox4.Text=Returen_Many_Values_CLASS().PassString;

}

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

for Anything and Everything, Please Let Me Know

bye
Venkat_KL
 
G

Guest

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.ToString();
this.textBox2.Text=MyStruct_Returning_Method().y;
}

Test and for anything and everything, please let me know

bye
Venkat_KL
 
G

Guest

Hi Dear Venkat

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

bye
assankhan
 
G

Guest

Hi Dear Venkat

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

bye
assankhan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top