• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2215
  • Last Modified:

check if value exists in a list

in ASP.NET 2.0 with C# I want to declare a list, add to it some strings and then check if a string exists in this list. How do I do it?
Thankyou
anat
0
ANAT2403
Asked:
ANAT2403
  • 6
  • 4
1 Solution
 
gnoonCommented:
You can use System.Collections.ArrayList class for the purpose. It has a function to add object, which can be string, into itself and a function to check if an object exists in it.

Dim list as New System.Collections.ArrayList()
list.Add("string1")
Console.WriteLine(list.Contains("string1"))

OUTPUT: true
0
 
kaylanreilorCommented:
From MSDN about System.Collections.ArrayList:
using System.Collections;
 
ArrayList myAL = new ArrayList();
myAL.Add("The");
myAL.Add("quick");
 
bool b1 = myAL.Contains("The"); // b1 = true
bool b2 = myAL.Contains("It"); // b2 = false
bool b3 = myAL.Contains("quick"); // b3 = true

Open in new window

0
 
gnoonCommented:
Furthermore, there are many useful classes in System.Collections namespace which're more flexible. For example, Hashtable class is often used in case of you have a list of key/value pairs.

Dim list As New System.Collections.Hashtable()
list.Add("key1", "value1")
list.Add("key2", "value2")
Console.WriteLine(list.ContainsKey("key1"))

"System.Collections Namespace", http://msdn2.microsoft.com/en-us/library/system.collections.aspx
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ANAT2403Author Commented:
Hi,
First I would like to have examples in C#.
I would like to decalre my list in a const declaration cs file in App_Code and then to check in each page I enter.
Thankyou.
anat
0
 
gnoonCommented:
Well, it depends on the requirements and usage of the variable in question. Having a shared/const variable within App_Code is not a good idea (even though it's fast/ease) and I could not recommend
1. its life time is the same as IIS running
2. it can be visible across sessions/instances of class and can cause memory leak if you havent manage it
3. deadlock can be happen while accessing a shared variable from multi-threads (webapp is multi-threads application)

I would recommend using Session variable because it's managed by IIS runtime, and be cleaned if it's timeout.

The Session variable is global list of asp.net web application and ready for use anywhere in any aspx or cs file (but there're limitations in global.asa file).

// you got a string submitted from page1
string currentVal = Request["valueFromPage1"];

// you cant remember you have already initiated your own list
// and saved it in the session as named MyGlobalList,
// so you will check if it already exists in the session
System.Collections.ArrayList list = (System.Collections.ArrayList) Session["MyGlobalList"];

if(list == null) {
    // now you know it's not there, so you'll create a new one
    // then you add it to the session to use in another page
    list = new System.Collections.ArrayList();
    Session["MyGlobalList"] = list;
}

// now you can check if value of currentVal exists in your global list
bool exists = list.Contains(currentVal);

if(exists) {
    // process the next logic
0
 
ANAT2403Author Commented:
Hi,
I like what you wrote but the problem is that to my site people can enter from differnt pages so I have to set all this in each page. Is it possible to declare it in one place?
thankyou
Anat
0
 
gnoonCommented:
Yes, it's possible.

How many value per page?

You wont keep duplicated values in the list if user post back many times on one page.
You may need a logic to check if any duplicated values in the list on certain page.

The logic depends on question above.

For example, you may have 2 pages. Each page has 2 values

page1
  val1 = 1
  val2 = One

page2
  val1 = 2
  val2 = Two

Using ArrayList may be not convenience for this case because it's hard to check for duplicated values.
You can change to use Hashtable instead which is easier with key/value pairs.

// on page1
// the hashtable should look like this at the final
// +-----------------+------+
// |       KEY          | VAL |
// +-----------------+------+
// | page1_value1 |   1   |
// +-----------------+------+
// | page1_value2 | One |
// +-----------------+------+

// on page2
// the hashtable should look like this at the final
// +-----------------+------+
// |       KEY          | VAL |
// +-----------------+------+
// | page1_value1 |   1   |
// +-----------------+------+
// | page1_value2 | One |
// +-----------------+------+
// | page2_value1 |   2   |
// +-----------------+------+
// | page2_value2 | Two |
// +-----------------+------+

See in MSDN document how to use Hashtable (link above). For quick guide, there are methods

Hashtable.Add(Key, Value)    -- add pair of key/value to the table
Hashtable.ContainsKey(Key)  -- check if specified key exists in the table
Hashtable[Key]                    -- get/set value of specified key, returns null if not existing
0
 
ANAT2403Author Commented:
Hi qnoon,
The values I have in my list are constant for the whole application. They will include the cultures I deal with in my application. for example I have 2 values: "en-CA" and "fr-CA". I want to check in each page if the current culture is one of the values in my list. That's why I thought of declaring it once in a cs file. If I declare it in a session variable I will have to check in each page if it is already created and if not to create it and as I mentioned my site can be entered from differnt pages. What do you suggest me to do?
Thankyou
Anat
0
 
gnoonCommented:
If they're all constants, so you can define them in web.config file. The whole file will be loaded while the webapp starts, and accessible from all parts of your webapp. Only when you changed them in the config file, the webapp will be forced to restart and reload automatically.

<configuration>
      <appSettings>
            <add key="AvailableLocales" value="en-CA,fr-CA"/>
            <add key="AnotherKey" value="StringValue"/>
      </appSettings>
      ...
</configuration>


// somewhere within codebehind
// accessing to the global setting, AvailableLocales.
string localeList = (string) System.Configuration.ConfigurationSettings.AppSettings["AvailableLocales"];

string localeOnThisPage = "fr-ca";

if( localeList.ToLower().IndexOf(localeOnThisPage .ToLower()) != -1) {
    // here the locale on this page is defined in the list
}
0
 
ANAT2403Author Commented:
Hi qnoon
I enjoyed very much reading your answer. I learned a lot from them. I have difficulties in decision which answer to accept .  they are all good
Thankyou
0
 
gnoonCommented:
:)
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now