Solved

C# Returning Multiple Strings?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

8 Experts available now in Live!

Get 1:1 Help Now