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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amitjaura
    New Member
    • Aug 2008
    • 14

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

    The coding on which I am working is::

    Code:
     private String infoPatient()
            {
                String initial;
                String transplantDate;
    
                openConnection();
                SqlDataAdapter adpGender = new SqlDataAdapter("select gender from tblRegistration where pID='"+patientID+"'", con);
                DataTable dtGender = new DataTable();
                adpGender.Fill(dtGender);
                initial = dtGender.Rows[0][0].ToString()=="Male" ? "Mr" : "Mrs";
                
    
                SqlDataAdapter adpTransplantDate = new SqlDataAdapter("select crossdate from txnpatientEvaluationFirst where patientID='"+patientID+"' and dateOfupdate=(select max(dateOfUpdate) from txnpatientEvaluationFirst where patientID='"+patientID+"')", con);
                DataTable dtTransplantDate = new DataTable();
                adpTransplantDate.Fill(dtTransplantDate);
                transplantDate = (Convert.ToDateTime(dtTransplantDate.Rows[0][0].ToString())).Date.ToLongDateString();
               }
    Now I want to return values transplantDate and iniatial
    Last edited by Curtis Rutland; Aug 25 '08, 01:07 PM. Reason: Added Code Tags - Please use the # button
  • cloud255
    Recognized Expert Contributor
    • Jun 2008
    • 427

    #2
    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

    Comment

    • coolsti
      Contributor
      • Mar 2008
      • 310

      #3
      You could also do this, for example:
      Code:
       private String infoPatient(out String transplantDate)
       {
           // rest of your code here
           return initial;
       }
      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.

      Comment

      • sandeepshetty
        New Member
        • Aug 2008
        • 7

        #4
        The Best way could be using "Out" Keyword... The code may be like:

        Code:
        private String infoPatient(out String initial,out String transplantDate )
        {............
        .............
        }
        There is no need of return statement then...But Make sure using "out" kyword while Calling this Functio..Like :

        Code:
        infoPatient(out initial,out transplantDate );
        All the Best.

        Comment

        • amitjaura
          New Member
          • Aug 2008
          • 14

          #5
          Originally posted by sandeepshetty
          The Best way could be using "Out" Keyword... The code may be like:

          Code:
          private String infoPatient(out String initial,out String transplantDate )
          {............
          .............
          }
          There is no need of return statement then...But Make sure using "out" kyword while Calling this Functio..Like :

          Code:
          infoPatient(out initial,out transplantDate );
          All the Best.



          Code:
          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,transp lantDate).Shoul d 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?

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            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.

            Comment

            • amitjaura
              New Member
              • Aug 2008
              • 14

              #7
              Originally posted by insertAlias
              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:::::
              Code:
              public class info
                  {
                      String initial,transplantDate;
                     public info(String initial, String transplantDate)
                     {
                         this.initial = initial;
                         this.transplantDate = transplantDate;
                     }
              
                      public string initial1
                   {
                        get
                        {
                             return initial; 
                        }
                   }
                   // Gets our Second Result (string)
                   public string transplant1
                   {
                       get
                       {
                           return transplantDate;
                       }
                   }
              Thank you evrybody once again

              Comment

              Working...