473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 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 1939
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
8
by: Ravindranath Gummadidala | last post by:
Hi All: I am trying to understand the C function call mechanism. Please bear with me as I state what I know: "every invocation of a function causes a frame for that function to be pushed on...
6
by: lovecreatesbeauty | last post by:
I ever missed a `return' statement when write a function `int HighDigit(Num)' to get the highest digit of an integer. But even if the `return' statement is ignored the function still can obtain...
18
by: Ed Jay | last post by:
<disclaimer>js newbie</disclaimer> My page has a form comprised of several radio buttons. I want to poll the buttons to determine which button was selected and convert its value to a string. I...
8
by: gregory_may | last post by:
Is it possible to return "nothing" from an Integer function? This seems to give me "0" rather than "nothing". Private Function MyFunction() As Integer Return Nothing End Function
8
by: colmkav | last post by:
Can someone tell me how I can access the return value of a function called from Oracle as opposed to a store proc from oracle? my oracle function is get_num_dates_varposfile. I am only used to...
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
49
by: Davy | last post by:
Hi all, I am writing a function, which return the pointer of the int. But it seems to be wrong. Any suggestion? int * get_p_t(int t) { return &t; } int main()
18
by: Carl Forsman | last post by:
there are 2 ways to return a value from a function ==================== 1) passing a reference as parameter - the following will return time void Table::Get(char* FieldName, *SYSTEMTIME time) {...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.