views:

1717

answers:

5

I am binding an ArrayList() to a Listbox control and assigning out an Displaymember and Value on data in the Array. My problem is that I Bind on startup but the Array gets filled after a few function calls. I have code on selectedIndexChanged to check the selectedValue but if the ArrayList is empty it returns an object, once it has data it returns the string I expect. I am still a confused why it runs selectedIndexChanged when the list has no data. Think it may run after I bind the Displaymember but before the value gets assigned:

lbCAT_USER.DataSource = USERS;
// Running here maybe?
lbCAT_USER.DisplayMember = "DisplayString";
// Or Here?
lbCAT_USER.ValueMember = "ID";

Either way my current work around is a try/catch of comparing the SelectedValue to a string and trying to rerun the function.

Simple workaround is maybe a way to check the datatype prior to the if statement? Any ideas of suggestions could be very helpful. Thanks

+1  A: 

If your question is how to check the datatype of a value via if-condition (title!), then here you go (example: check if value is of type 'string'):

if(value.GetType().Equals(typeof(string)))
{
   ...
}

EDIT: This is not the cleanest way to do it. Check Guard's answer for more sophisticated ways to check types. Using 'GetType().Equals' as I do is more precise than 'is' or 'as', since 'value' must be exactly of the type 'string'. 'is' and 'as' will work even if 'value' is a subclass of the checked type. This is irrelevant though when comparing with type 'string', since you cannot inherit from string.

galaktor
kind of longwinded
spender
+8  A: 

You have two ways to check this:

string value = list.SelectedValue as string;

// The cast will return null if selectedvalue was not a string.
if( value == null ) return; 

//Logic goes here.

If you just want to do the comparison:

if( list.SelectedValue is string )
{
   // ...
}
Will Eddins
+1 it's better and more elegant than galaktor's solution
Clement Herreman
+1: of course this is much better readable than my version - the irony is, I usually do the trick with 'as' and a null-check, but didn't think of that while answering :-)
galaktor
just another note since i am not sure what type he wants to check: using 'as' does not work with not-nullable types
galaktor
below his code, he said he's currently trying to compare SelectedValue to a string, so that's what I assumed.
Will Eddins
A: 

To handle the case where value is null:

if (typeof(value) == typeof(string))
{
    ...
}
bcwood
typeof(...) requires a type name at compile time.
Jon Skeet
oops, you're right - what do I mean, of course you're right, you're Jon Skeet! :)
bcwood
A: 

I believe you can also use:

if (value is string)
{
}

to check the type of a variable in C#.

Lee D
A: 

Yes, they all call OnSelectedIndexChanged() event

you can check this by placing break points on the the OnSelectedIndexChanged handler and stepping through.

You could un-hook the OnSelectedIndexChanged event when you do the data binding

this.ListBox.SelectedIndexChanged -= OnSelectedIndexChangedHandler;

this.ListBox.DataSource = dataSource;
this.ListBox.ValueMember = "ID"
this.ListBox.DisplayMember = "Name"

this.ListBox.SelectedIndexChanged += OnSelectedIndexChangedHandler;

Or just check the value like others have suggested on the OnSelectedIndexChanged event.

Doing both can't hurt

Hath