473,494 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Can we return more than one value through function/method in C#

14 New Member
The coding on which I am working is::

Expand|Select|Wrap|Line Numbers
  1.  private String infoPatient()
  2.         {
  3.             String initial;
  4.             String transplantDate;
  5.  
  6.             openConnection();
  7.             SqlDataAdapter adpGender = new SqlDataAdapter("select gender from tblRegistration where pID='"+patientID+"'", con);
  8.             DataTable dtGender = new DataTable();
  9.             adpGender.Fill(dtGender);
  10.             initial = dtGender.Rows[0][0].ToString()=="Male" ? "Mr" : "Mrs";
  11.  
  12.  
  13.             SqlDataAdapter adpTransplantDate = new SqlDataAdapter("select crossdate from txnpatientEvaluationFirst where patientID='"+patientID+"' and dateOfupdate=(select max(dateOfUpdate) from txnpatientEvaluationFirst where patientID='"+patientID+"')", con);
  14.             DataTable dtTransplantDate = new DataTable();
  15.             adpTransplantDate.Fill(dtTransplantDate);
  16.             transplantDate = (Convert.ToDateTime(dtTransplantDate.Rows[0][0].ToString())).Date.ToLongDateString();
  17.            }
  18.  
Now I want to return values transplantDate and iniatial
Aug 25 '08 #1
6 10028
cloud255
427 Recognized Expert Contributor
There are several approaches which you could take;

You could change the return type to a collection or array of strings

You could make the return type a collcetion of objects and then casting is not required.

You could keep the return type as a single string, then concatinate the two values with a seperator like a ";" and then split them again in another function.

Or you could define a new class which stores the two values in the correct type and then pass an instance of that class.

Good luck
Aug 25 '08 #2
coolsti
310 Contributor
You could also do this, for example:
Expand|Select|Wrap|Line Numbers
  1.  private String infoPatient(out String transplantDate)
  2.  {
  3.      // rest of your code here
  4.      return initial;
  5.  }
  6.  
where here I am returning one of the values, transplantDate, as an "out" parameter to the function, and returning the other one, initial, as a normal function return.
Aug 25 '08 #3
sandeepshetty
7 New Member
The Best way could be using "Out" Keyword... The code may be like:

Expand|Select|Wrap|Line Numbers
  1. private String infoPatient(out String initial,out String transplantDate )
  2. {............
  3. .............
  4. }
There is no need of return statement then...But Make sure using "out" kyword while Calling this Functio..Like :

Expand|Select|Wrap|Line Numbers
  1. infoPatient(out initial,out transplantDate );
All the Best.
Aug 26 '08 #4
amitjaura
14 New Member
The Best way could be using "Out" Keyword... The code may be like:

Expand|Select|Wrap|Line Numbers
  1. private String infoPatient(out String initial,out String transplantDate )
  2. {............
  3. .............
  4. }
There is no need of return statement then...But Make sure using "out" kyword while Calling this Functio..Like :

Expand|Select|Wrap|Line Numbers
  1. infoPatient(out initial,out transplantDate );
All the Best.



Expand|Select|Wrap|Line Numbers
  1. infoPatient(out initial,out transplantDate );

How will we get values returned by the
infoPatient(out initial,out transplantDate )
This function will return two values (initial,transplantDate).Should we get the values in collection?

Also you used the return type of function as string but we have two values to return....what if the two values are of different type?
Aug 26 '08 #5
Curtis Rutland
3,256 Recognized Expert Specialist
I think that you missed everyone's point.

There are several ways to do it.
The "out" method doesn't actually "return" the values...but it gets a reference to the parameters that are being passed to it. The objects you used to call that method will be updated. They don't have to be returned.

Or, (the way I would do it) is that you can write a simple class with the properties that you need. Then use objects of that class type.

Or like was suggested you can use a return type of object[] and cast back after you are done.

There are plenty of possibilities.
Aug 26 '08 #6
amitjaura
14 New Member
I think that you missed everyone's point.

There are several ways to do it.
The "out" method doesn't actually "return" the values...but it gets a reference to the parameters that are being passed to it. The objects you used to call that method will be updated. They don't have to be returned.

Or, (the way I would do it) is that you can write a simple class with the properties that you need. Then use objects of that class type.

Or like was suggested you can use a return type of object[] and cast back after you are done.

There are plenty of possibilities.


Thank you everybody

I have used a class returning the values and I am using object of that class
and it is doing good:
the coding is:::::
Expand|Select|Wrap|Line Numbers
  1. public class info
  2.     {
  3.         String initial,transplantDate;
  4.        public info(String initial, String transplantDate)
  5.        {
  6.            this.initial = initial;
  7.            this.transplantDate = transplantDate;
  8.        }
  9.  
  10.         public string initial1
  11.      {
  12.           get
  13.           {
  14.                return initial; 
  15.           }
  16.      }
  17.      // Gets our Second Result (string)
  18.      public string transplant1
  19.      {
  20.          get
  21.          {
  22.              return transplantDate;
  23.          }
  24.      }     
  25.  
  26.  
Thank you evrybody once again
Aug 27 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2908
by: lawrence | last post by:
I've this function, which is the method of a class. I'm posting the constructor of the class down below. For some reason, when I fill out a form and hit submit, I'm not getting any values. Can...
3
4502
by: Phil Powell | last post by:
My first time working with a PHP class, and after 6 hours of working out the kinks I am unable to return a value from the class, so now I appeal to the general audience what on earth did I do wrong...
47
3577
by: Martin DeMello | last post by:
It seems to be a fairly common pattern for an object-modifying method to return None - however, this is often quite inconvenient. For instance def f(lst1, lst2): g((lst1 + lst2).reverse()) #...
30
2132
by: John Bailo | last post by:
The c# *return* statement has been bothering me the past few months. I don't like the fact that you can have different code paths in a method and have multiple return statements. To me, it...
161
7677
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
6
1880
by: Tim Roberts | last post by:
I've been doing COM a long time, but I've just come across a behavior with late binding that surprises me. VB and VBS are not my normal milieux, so I'm hoping someone can point me to a document...
20
8941
by: Andrew Morton | last post by:
Is it possible to have two function declarations which take the same parameters but return different types depending on how the function is used? function f(x) as string ' return a string end...
8
11239
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
10284
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
2669
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()
0
7119
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
6989
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
7157
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
7195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6873
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
5453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
644
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.