?
Solved

Return multiple variables in c#

Posted on 2011-05-08
4
Medium Priority
?
595 Views
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.
0
Comment
Question by:Dave Fyfe
4 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35716046
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;
0
 
LVL 18

Expert Comment

by:dj_alik
ID: 35716114
Use Linq to XML
<root>
      <level1 name="A">
            <level2 name="A1" />
            <level2 name="A2" />
      </level1>
      <level1 name="B">
            <level2 name="B1" />
            <level2 name="B2" />
      </level1>
      <level1 name="C" />
</root>
XDocument xdoc = XDocument.Load("data.xml"));
var lv1s = from lv1 in xdoc.Descendants("level1")
		   select lv1.Attribute("name").Value;

foreach (var lv1 in lv1s)
{
	result.AppendLine(lv1);

	var lv2s = from lv2 in xdoc...???
}

Open in new window

0
 
LVL 7

Expert Comment

by:dimaj
ID: 35716932
Another approach would be to return a dictionary, where your xml tags are keys and xml values are values.
i.e.
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: http://blog.dimaj.net/2011/02/howto-update-xml-file-using-linq/
0
 
LVL 1

Accepted Solution

by:
amr-it earned 1000 total points
ID: 35718885
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.

Cheers
amr-it

    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.
            //
            //<ROOT>
            //  <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,
                         }).SingleOrDefault();

            return Persons;
        }
    }

Open in new window


0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

864 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