views:

46

answers:

3

hey all, i've built a ComboBox that gets manually items like this:

var newitem = new { fullname =c.Company+" "+ c.FirstName + " " + c.LastName,
                    custId = c.CustomerID };

c_dropCustomers.Items.Add(newitem);

later on on combo Selection event, i would want to get out the custId (The Value) only but i dont know how to reach it.

SOS :)

A: 

You need to actually define a class, so that you can cast to it later. You can't cast to an anonymous class (AFAIK).

Bob
A: 

If I understand your question correctly:

var item = c_dropCustomers.SelectedItem;
var custId = item.custId;

EDIT: (C# 3.5)

If it really is an anonymous type you'll need to do something like this:

        ComboBox cb = new ComboBox();
        cb.Items.Add(new { fullname = "Company" + " " + "First Name" + " " + "Last Name", custId = 44 });

        cb.SelectedIndex = 0;

        var item = cb.SelectedItem;
        var custId = item.GetType().GetProperty("custId").GetValue(item, System.Reflection.BindingFlags.GetProperty, null, null, null);
Chris Persichetti
+3  A: 

asuming c# 4.0:

dynamic item = c_dropCustomers.SelectedItem;
dynamic customerID = item.custId;
Excel20
+1 That's cool. Guess I need to get C# 4.0.
Chris Persichetti