Solved

How to check if a list contains items? c#

Posted on 2009-05-08
6
2,055 Views
Last Modified: 2012-05-06
HI all,

I have a list wich I need to check if it contains any items or not. If it does not I need to show a warning that it has to have at least one item.

How to check this?
Probably pretty simple but can't seem to find it right now...

Grts
0
Comment
Question by:Pit76
  • 3
  • 2
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24335948

List<string> someList = new List<string>();
 

someList.Count > 0

Open in new window

0
 
LVL 12

Accepted Solution

by:
wht1986 earned 500 total points
ID: 24335991
You did say what kind of list you are referring to so here are a couple of examples

List<string> list1 = new List<string>();
if (list1.Count == 0) { /* Display Error */ }

string[] list2 = new string[] { "", "", "" };
if (list2.Length == 0) { /* Display Error */ }

ListBox lb = new ListBox();
if (lb.Items.Count == 0) { /* Display Error */ }
0
 

Author Comment

by:Pit76
ID: 24336039
Hi,

Indeed I forgot to tell that. It's the first one.
Thought it was going to be simple, about the same as checking if a DataSet has rows...

Anyway thx for putting my nose on it :)

Grts
0
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.

 

Author Closing Comment

by:Pit76
ID: 31579432
Thx :)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24336062
Wow...  I don't even know what to say.
0
 

Author Comment

by:Pit76
ID: 24339711
Yeah I know sry.
 I should have split the points but tbh I looked over your answer, as stupid as it may sound...

You've helped me before so I feel a little bad about it...

Greetings.

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

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

23 Experts available now in Live!

Get 1:1 Help Now