Solved

How to check if a list contains items? c#

Posted on 2009-05-08
6
2,050 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility

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

someList.Count > 0

Open in new window

0
 
LVL 12

Accepted Solution

by:
wht1986 earned 500 total points
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Closing Comment

by:Pit76
Comment Utility
Thx :)
0
 
LVL 74

Expert Comment

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

Author Comment

by:Pit76
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
c# string 6 39
Object Oriented Best Practice 5 32
How do I get the id from URL? 19 46
getting id from database 5 23
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

10 Experts available now in Live!

Get 1:1 Help Now