473,224 Members | 1,513 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,224 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 1934
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) {...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.