Solved

C# Returning Multiple Strings?

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now