Return multiple variables in c#

Posted on 2011-05-08
Last Modified: 2012-08-13
I have a class in a seperate file which extracts information from an XML file and places the required information into variables. I would like these variables to be returned to my form to populate fields.

Can anyone please supply simple C# code to do this.
Question by:davidfy
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Build yourself a custom class that has fields to hold the values you want to return.  Then just create an instance, fill in the values, and return it:

        public class Data {
            public string Name;
            public string ID;
            public bool processed;
            // etc...

    Then in your function:
        Data d = new Data();
        d.Name = "something";
        d.ID = "abc123";
        d.processed = true;
        return d;
    LVL 18

    Expert Comment

    Use Linq to XML
          <level1 name="A">
                <level2 name="A1" />
                <level2 name="A2" />
          <level1 name="B">
                <level2 name="B1" />
                <level2 name="B2" />
          <level1 name="C" />
    XDocument xdoc = XDocument.Load("data.xml"));
    var lv1s = from lv1 in xdoc.Descendants("level1")
    		   select lv1.Attribute("name").Value;
    foreach (var lv1 in lv1s)
    	var lv2s = from lv2 in xdoc...???

    Open in new window

    LVL 7

    Expert Comment

    Another approach would be to return a dictionary, where your xml tags are keys and xml values are values.
    myDict["rootNode"] = "some value";

    Of course, this might be a little complicated since dictionaries do not allow duplicate key names. So, if you have multiple instances of the same node name, append some number at the end of it.

    dj_alik has a good example on how to parse XML file using linq. You could also take a look at this blog post:
    LVL 1

    Accepted Solution

    I would suggest what the first poster suggested, create your own class and assign your variables to that. The code below is not tested. May contain error. But Im sure it will give you an idea.


        class Program
            //Your custom class
            class MyData {
               public string FirstName;
               public string LastName;
               public string Age;
            static void Main(string[] args)
                //Call your get "Persons" method.
                MyData myDataObject = getPersons();
                //Assign to your form
                txtFirstNameTextBox.Text = myDataObject.FirstName;
                txtLastNameTextBox.Text = myDataObject.LastName;
                txtAgeTextBox.Text = myDataObject.Age.ToString();
            public static MyData getPersons()
                //XMLFile location
                string file = "C:\\myfile.xml";
                //Load the XML to a XDocument
                XDocument xDoc = XDocument.Load(file);
                //Select all Persons from the file.
                //  <Person>
                //      <FirstName>
                // .....
                MyData Persons = (from p in xDoc.Descendants("Person")
                            select new MyData
                                       FirstName = p.Element("FirstName").Value,
                                       LastName = p.Element("LastName").Value,
                                       Age = p.Element("Age").Value,
                return Persons;

    Open in new window


    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now