Solved

How to check if a list contains items? c#

Posted on 2009-05-08
6
2,064 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 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