Solved

C# Returning Multiple Strings?

Posted on 2009-04-02
4
508 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 125 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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