Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# Returning Multiple Strings?

Posted on 2009-04-02
4
Medium Priority
?
520 Views
Last Modified: 2012-05-06
Hello. I have an ASP.NET 2.0 app with a class file that runs code used by other pages in the site, and I'm trying to figure out how to return multiple distinct strings (ie, I'm not sure this can be done with an array, because each string returned will be placed in a separate Label on other pages). See below for a sample of how I'm retrieving a single string. Many thanks in advance for your help. Chris
// class file code
    public string testString
    {
        get
        {
            // create dataSet and connect to db
            DataRow[] dr = dataSet.Tables[0].Select("", "string1");
            connection.Close();
            foreach (DataRow dataRow in dataSet.Tables[0].Rows)
            {
                if (Convert.ToString(dataRow["string1"]) == "1")
                {
                    testStringValue += Convert.ToString(dataRow["string1"]);
                }
            }
            return testStringValue;
        }
        set
        {
            testStringValue = value;
        }
    }
 
// code on another page in the site
public partial class nmsalert : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        whoDaDaDA();
    }
    private void whoString1()
    {
        testString sc = new testString();
        Label1.Text = sc.testString;
    }
}

Open in new window

0
Comment
Question by:chriswight
  • 2
  • 2
4 Comments
 
LVL 11

Expert Comment

by:anyoneis
ID: 24056950
Hi Chris;

I'm not 100% sure what you want. How would the other results be generate? From "String2..Stringn"?

Does the page know about "String1"? If so, you could have testtring return a Dictionary<string, string> with all of the constructed strings in it.

If I'm not close, please provide more information!

David
0
 

Author Comment

by:chriswight
ID: 24060767
Hi David. Thanks for the reply. Yes, I think you might be onto something.

To put more simply, I have code that checks a database table for a name that meets certain criteria (the foreach loop), and returns a single string. I'm looking for a way to use the same connection to return multiple strings (a total of 3 names and 3 phone numbers), which will then be used by other pages to populate 6 separate Labels.

The C# dictionary is a new concept to me. Right now I'm checking MSDN to see how I might apply this. Meanwhile if you have any tips or suggestions they would be greatly appreciated.

Chris
0
 
LVL 11

Accepted Solution

by:
anyoneis earned 500 total points
ID: 24063425
A Dictionary<KeyType, ValueType> is just a lookup table. You put in the key and get back the value. You would probably create a Dictionary<string, string> and then populate it as you did for the single string. Here is some pseudo code:

Dictionary<string, string> retDictionary= new Dictionary<string, string>();

for string curstring in all string names we are gathering ("string1" - "string n")
       reset and build up mystringresults contents
       retDictionary.Add(curstring, mystringresults);
end for

return retDictionary;

----

Then, page A gets its value using Label1.Text = retDictionary["string1"];
page B: Label1.Text = retDictionary["string2"];

...
0
 

Author Comment

by:chriswight
ID: 24069015
Got it! Thanks David. Here's a basic version of the final code in C#, for anyone who's interested in passing a .NET dictionary from ASP.NET class file to other pages in the site.



// class file
    public Dictionary<string, string> hmm
    {
        get
        {
        Dictionary<string, string> webster = new Dictionary<string,string>();
        webster.Add("bbmDa", "Kelsey Pileggi");
        return webster;
        }
        set
        {
            webster = value;
        }
    }
 
 
// other pages
 
    private void getDa()
    {
        daDictionary blahblah = new daDictionary();
        LabelDAEscalation.Text = blahblah.hmm["bbmDa"];
    }

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

877 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