o0JoeCool0o
asked on
How to return three values from a function in c#?
Hi Experts,
I am writing a function which should return three string values and my code looks like this.
Is there any work around for this? Or Can you please share some ideas on how to resolve it?
Thanks,
I am writing a function which should return three string values and my code looks like this.
private String GetAttDataType(string dt)
{
string attDa = null;
string attDe = null;
string attDep = null;
string ConString = "User Id=sys;Password=sys;Data Source=T";
using (OracleConnection con = new OracleConnection(ConString))
{
OracleDataAdapter adapter = new OracleDataAdapter();
con.Open();
OracleCommand command = new OracleCommand("SELECT ATT_DATATYPE,ATT_DEFVALUE, DONID FROM T.ATTRIBUTES WHERE ATTRIBUTE_NUMBER=" + dt, con);
adapter.SelectCommand = command;
DataSet dataset = new DataSet("ATTDATATYPE1");
adapter.Fill(dataset);
foreach (DataRow dr in dataset.Tables[0].Rows)
{
attDataType=dr["ATT_DATATYPE"].ToString();
attDefValue = dr["ATT_DEFVALUE"].ToString();
attDependsOnID = dr["DONID"].ToString();
}
con.Close();
}
return attDa;
}
Is there any work around for this? Or Can you please share some ideas on how to resolve it?
Thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
As p_davis suggested, using a tuple would work (http://msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx)
If for some reason a tuple doesn't work, you could also
1. Return an array
2. Return a string separated by some delimiter such as |and use String.Split (http://msdn.microsoft.com/en-us/library/tabh47cf(v=vs.110).aspx)
3. Change your function to use pass by reference variables (http://msdn.microsoft.com/en-us/library/0f66670z.aspx) You would then pass in three additional strings to your function and set those. The code that calls this function would have the values.
Tuple is best, but providing a few other options just in case. Order preference would probably be tuple, array, delimited string, and, only if you have to, reference variables.
If for some reason a tuple doesn't work, you could also
1. Return an array
2. Return a string separated by some delimiter such as |and use String.Split (http://msdn.microsoft.com/en-us/library/tabh47cf(v=vs.110).aspx)
3. Change your function to use pass by reference variables (http://msdn.microsoft.com/en-us/library/0f66670z.aspx) You would then pass in three additional strings to your function and set those. The code that calls this function would have the values.
Tuple is best, but providing a few other options just in case. Order preference would probably be tuple, array, delimited string, and, only if you have to, reference variables.
An alternative is to use the parameter list (either as ref or as out keywords), see:
http://msdn.microsoft.com/en-us/library/ee332485.aspx
roughly
string dt1, dt2, dt3
GetAttDataType(dt1, dt2, dt3)
and
private void GetAttDataType(out string dt1, out string dt2, out string dt3)
{
//now fill in the values into dt1, dt2 and dt3
}