?
Solved

Return multiple strings

Posted on 2011-05-03
6
Medium Priority
?
317 Views
Last Modified: 2013-12-16
Hi,

I have a form that uses a seperate class. In the class I have this methode:

            public string GetDisplayText(DataRow row)
            {
                string text = "";

                switch (row.Table.TableName)
                {
                    case Tables.Product:
                        text = "ID: " + row[0];
                        text += row[ProductField.Name];
                        text += row[ProductField.Description];
                        break;
                }
                return text;

 This methode is being called from another methode on form1:

         private void lstvFoodDB_AfterSelect(object sender, TreeViewEventArgs e)
            {
                   txtFoodName.Text = DataClass.GetDisplayText((DataRow)e.Node.Tag);
            }

You see in the GetDisplayText methode a string has been returned, but I need
to return several strings seperatly, is that possible? Who can help me?

Peter
           
0
Comment
Question by:peterkiers
  • 3
  • 2
6 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 35512855
There are multiple ways to do this:
1. Return an list of strings

example:
List<string> returnValues = DataClass.GetDisplayText((DataRow)e.Node.Tag);
txtFoodName.Text = returnValues [0];
txtFoodDescription.Text = returnValues [1];

public List<string> GetDisplayText(DataRow row)
            {
}

2. Use the request-response pattern (basicly: passing a class back)
public OutPutClass GetDisplayText(DataRow row)
{
}

3. use the or use the "out" keyword on your parameters, like so
public void GetDisplayText(DataRow row, out string string1, out string string2, ...)
            {
}

0
 
LVL 10

Expert Comment

by:oxyoo
ID: 35512867
C# does not support multiple return values. There are different alternatives...

1. Use out parameters (not recommended)
2. Create a custom object that contains your two strings and return this custom object instead


Good Luck!
0
 
LVL 1

Author Comment

by:peterkiers
ID: 35512877
I go for option 1 can you give me an example?
p
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Dhaest
ID: 35512936
Below an example with the list<string>, but perhaps (i'll post it in a while) it's better to use a dictionary
public List<string> GetDisplayText(DataRow row)
            {
                List<string> text = new List<string>();

                switch (row.Table.TableName)
                {
                    case Tables.Product:
                        text.Add ("ID: " + row[0]);
                        text.Add (row[ProductField.Name]);
                        text.Add (row[ProductField.Description]);
                        break;
                }
                return text;
}

 This methode is being called from another methode on form1:

         private void lstvFoodDB_AfterSelect(object sender, TreeViewEventArgs e)
            {
                   List<string> returnValues = DataClass.GetDisplayText((DataRow)e.Node.Tag);
                   txtFoodName.Text = returnValues[1]; 
                   txtFoodName.Text = returnValues[2]; 

 }

Open in new window

0
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 35512959
public List<string> GetDisplayText(DataRow row)
            {
                Dictionary<string, string> text = new Dictionary<string, string>();

                switch (row.Table.TableName)
                {
                    case Tables.Product:
                        text.Add ("ID" + row[0]);
                        text.Add ("ProductName", row[ProductField.Name]);
                        text.Add ("ProductDescription", row[ProductField.Description]);
                        break;
                }
                return text;
}

 This methode is being called from another methode on form1:

         private void lstvFoodDB_AfterSelect(object sender, TreeViewEventArgs e)
            {
                   Dictionary<string, string> d = DataClass.GetDisplayText((DataRow)e.Node.Tag);
                   // See if Dictionary contains this string
            if (d.ContainsKey("ProductName"))
                          txtFoodName.Text = d["ProductName"];
                   if (d.ContainsKey("ProductDescription"))
                          txtFoodDesc.Text = d["ProductDescription"];

 }
0
 
LVL 1

Author Comment

by:peterkiers
ID: 35512985
Thanx.

P.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

839 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