How to return three values from a function in c#?

Posted on 2014-07-17
Medium Priority
Last Modified: 2014-07-17
Hi Experts,

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();
                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");
                    foreach (DataRow dr in dataset.Tables[0].Rows)
                        attDefValue = dr["ATT_DEFVALUE"].ToString();
                        attDependsOnID = dr["DONID"].ToString();
            return attDa;

Open in new window

Is there any work around for this? Or Can you please share some ideas on how to resolve it?

Question by:o0JoeCool0o
LVL 22

Accepted Solution

p_davis earned 2000 total points
ID: 40202452
you can return a tuple
LVL 45

Expert Comment

ID: 40202586
A function can only return one value - but it is up to you what type of object that is, it could be a class.

An alternative is to use the parameter list (either as ref or as out keywords), see:

string dt1, dt2, dt3
GetAttDataType(dt1, dt2, dt3)

private void GetAttDataType(out string dt1, out string dt2, out string dt3)
//now fill in the values into dt1, dt2 and dt3
LVL 14

Expert Comment

ID: 40202678
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.

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question