|
How do I check if a value in a list valid in C#? I have a list that dynamically grows and shrinks but when the list is Empty(no values/entries in the list), I want to run alternate code. So something like this: if(value == "ArgumentOutOfBounds") { //do This } How do I do code that actually works in this situation?
(comments are locked)
|
|
Use the list's Count property. is there a way to do it without referring to the actual list or is that the only way. like: if(value == "ArgumentOutOfBounds") { //do This }
May 03 '11 at 09:50 AM
Ari Levi
Well... sort of. It looks to me like you'd like to take some kind of action in the event that the list throws an index out of bounds exception upon access to it. The way to do that is to enclose the potentially offending code in a try-clause and catch the OutOfBounds exception. See http://msdn.microsoft.com/en-us/library/0yd65esw%28v=vs.80%29.aspx And catch IndexOutOfRangeException in the catch-clause.
May 03 '11 at 10:42 AM
CHPedersen
(comments are locked)
|
List.Count is a property, not a method. :) Otherwise, yes.
May 03 '11 at 09:14 AM
CHPedersen
Thanks, i've changed it!
May 03 '11 at 09:30 AM
Maarten
(comments are locked)
|
