C# Returning Multiple Strings?

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

chriswightAsked:
Who is Participating?
 
anyoneisConnect With a Mentor Commented:
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
 
anyoneisCommented:
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
 
chriswightAuthor Commented:
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
 
chriswightAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.