Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
im trying to return 2 output parameters from this c# method and it returning an error for the second ruturn parameter.

What I have tried:

public string getEMails(string staffid)
{
using (OracleConnection OracleConn = new OracleConnection(or))
{
OracleConn.Open();
try
{
query = "SELECT uetc.team_codes, LPAD (papf.employee_number, 5, 0) staff_id,papf.full_name full_name AND papf.employee_number = @employee_number";
OracleCommand OraC = new OracleCommand(query, OracleConn);
OraC.CommandType = CommandType.Text;
OraC.Parameters.Add(new OracleParameter("employee_number", OracleDbType.Varchar2)).Value = staffid;
//OraC.Parameters.Add(new OracleParameter("SESSION_ID", OracleDbType.Varchar2)).Value = SessionID;
//OraC.Parameters.Add(new OracleParameter("V_RESULT", OracleDbType.Varchar2, 32767)).Direction = ParameterDirection.Output;


//OracleDataReader dr = OraC.ExecuteNonQuery();
OracleDataReader dr = OraC.ExecuteReader();
while (dr.Read())
{
SupervisorEmail = dr["EMAIL_ADDRESS_1"].ToString();
StaffEmail = dr["EMAIL_ADDRESS"].ToString();
string id = dr["STAFF_ID"].ToString();

}

}
catch (Exception ex)
{

ex.Message.ToString();
}
finally
{
OracleConn.Close();
}
return SupervisorEmail;
return StaffEmail;

}
}
Posted
Updated 27-Jul-16 0:24am
Comments
F-ES Sitecore 27-Jul-16 6:10am    
And the error is...?

1 solution

A method can only return one value: that is all the definition allows.
To return multiple values, you either need to use an out or ref parameter as well:
C#
public string getEMails(string staffid, out staffEmail)
   {
   ...
   staffEmail = ... ;
   return SupervisorEmail;
   }

C#
public string getEMails(string staffid, ref staffEmail)
   {
   ...
   staffEmail = ... ;
   return SupervisorEmail;
   }
And call it passing the appropriate variable to the method:
C#
string staffEmail;
string supEmail = getEMails("00001", out staffEmail);

C#
string staffEmail = "none";
string supEmail = getEMails("00001", ref staffEmail);


Or, you could create a "container class" which holds both your parameters, and return that:
C#
public class EMails
   {
   string Staff {get; set}
   string Supervisor {get; set;}
   }
...
public EMails getEMails(string staffid)
   {
   ...
   Emails em = new Emails;
   em.Staff = StaffEmail;
   em.Supervisor = SupervisorEmail;
   return em;
   }
 
Share this answer
 
Comments
phirSOFT 27-Jul-16 8:22am    
As you said multiple returns are currently not allowed. But they will be in C# 7.0. Take a look at this
https://github.com/dotnet/roslyn/issues/347
OriginalGriff 27-Jul-16 8:25am    
That's a proposal, not a "concrete addition" - it may appear, it may not!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900