Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Add an items from a list into like an array or collection

Posted on 2012-04-04
6
Medium Priority
?
261 Views
Last Modified: 2012-04-05
Hi all,

This is a relatively simple quesiton I would expect. But in C#, if I have a list of items (names of items). I want to load them into a collection or something. Waht is the best way to do that?

An array, a collection or something...

I want to do something like:

FieldList.Add(strValue).

Then I will end up seeing if a value doesn't exist in that list.

So, I the list contains some fruits:

Apple
Orange
Grape
Banana

I want those to go into a list.

then I will eventually want to use like:

"Nut" and see if it exists in that list.

What is the best way to do that?

Thanks
0
Comment
Question by:davism
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 37807656
List<String> items = new List<String>();

items.Add(yourStringValue);

if(items.Exists(yourStringValue)
{
  //do something

}
0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 37807663
In C#, you can use the generic List:

List<string> fruits = new List<string>();
fruits.Add( "Apple" );
fruits.Add( "Orange" );
etc.

if ( !fruits.Contains( "Apple" ) )
{
   // does not have "Apple" in the list.
}

You can also convert it to an array simply:


string[] fruitArray = fruits.ToArray();
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37808010
A HashSet would perform better with lookups.  The Contains implementation on a List relies on sequential comparisons, while the HashSet implementation is based on a hash lookup.

var fruits = new HashSet<string>() { "Apple", "Orange", "Grape", "Banana" };

if (fruits.Contains("Nuts"))
{
	Console.WriteLine("We have Nuts");
}
else
{
	Console.WriteLine("We don't have Nuts let's buy some");
	
	fruits.Add("Nuts");
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:davism
ID: 37808370
Ok, I had that a little wrong.

I need the table to have like

Fruit
    Apples
    Oranges
    Bananas

Meat
    Steak
    Hamburget
    Chicken

So, that's like a diffent type of collection is it not? Does anybody have an example on how to implement something like that?

Thanks
0
 
LVL 22

Accepted Solution

by:
p_davis earned 400 total points
ID: 37810678
Dictionary<string, List<string>> items = new Dictionary<string, List<string>>();

if(!items.ContainsKey("Fruit"))
             items.Add("Fruit", new List<String>());

items["Fruit"].Add("Apples");  //... etc.
0
 
LVL 1

Author Closing Comment

by:davism
ID: 37813737
Thanks for the information and greatly appreciated!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

664 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