ANAT2403
asked on
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
Thankyou
anat
From MSDN about System.Collections.ArrayLi st:
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
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.Hashtab le()
list.Add("key1", "value1")
list.Add("key2", "value2")
Console.WriteLine(list.Con tainsKey(" key1"))
"System.Collections Namespace", http://msdn2.microsoft.com/en-us/library/system.collections.aspx
Dim list As New System.Collections.Hashtab
list.Add("key1", "value1")
list.Add("key2", "value2")
Console.WriteLine(list.Con
"System.Collections Namespace", http://msdn2.microsoft.com/en-us/library/system.collections.aspx
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
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
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)
Hashtable[Key] -- get/set value of specified key, returns null if not existing
ASKER
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
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
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.Confi gurationSe ttings.App Settings[" AvailableL ocales"];
string localeOnThisPage = "fr-ca";
if( localeList.ToLower().Index Of(localeO nThisPage .ToLower()) != -1) {
// here the locale on this page is defined in the list
}
<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.Confi
string localeOnThisPage = "fr-ca";
if( localeList.ToLower().Index
// here the locale on this page is defined in the list
}
ASKER
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
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
:)
Dim list as New System.Collections.ArrayLi
list.Add("string1")
Console.WriteLine(list.Con
OUTPUT: true