1

I have 1 enum declaration of "car_brands" like this :

public enum Car_brands
{
    Audi = 1,
    ...
    ...
}

and many others enum declarations of each "car_brand" like this

public enum Audi
{
    model_a3 = 1,
    model_a4 = 2,
    ...
}

I have 2 combobox associated. One associated with the car_brands :

 comboBox1.DataSource = new BindingSource(Car_brands.Keys, null);

I want the other combobox fill with select brand's enum (exemple Audi's models of Audi Enum).

I try this but it seems not exact...

private void comboBox3_SelectedValueChanged(object sender, EventArgs e)
        {
string value = comboBox1.Text;   //car brand
Type type = Type.GetType(value);
var brand_models = Enum.GetNames(type.GetType());
                foreach (string enumValue in brand_models)
                    {
                        string brand_model = enumValue;
                        MessageBox.Show(brand_model);
                    }

        }
meilke
  • 3,280
  • 1
  • 15
  • 31
c_conl
  • 57
  • 1
  • 10
  • 2
    FYI - you don't need to explicitly set the numbers in your enum incrementally, it's done automatically. – James Sep 14 '13 at 12:32
  • I use also them as values in the following of my program... thanks – c_conl Sep 14 '13 at 12:37
  • My point is, if you take away the int assignment they are still going to represent 1, 2, 3... etc. so it's unnecessary. – James Sep 14 '13 at 12:39
  • What do you mean by "not exact"? Does it work, or does it not work? – Sergey Kalinichenko Sep 14 '13 at 12:39
  • @James, it is easier to look up a value when explicitly set when it represents a value in the database. – Silvermind Sep 14 '13 at 12:41
  • In fact , I get the error : Object reference not set to an instance of an object on "Type type = Type.GetType(value);" My problem is that I want declare an enum key as enum type but I don't know how I can do that – c_conl Sep 14 '13 at 12:42
  • @James Nope, If you remove values it will represent 0,1,2 – Sriram Sakthivel Sep 14 '13 at 12:45
  • @Silvermind could you elaborate on why its *easier* to look it up? The enum will still represent the same integer value. SriramSakthivel yeah you are correct, that was an oversight on my part as I generally would have a default item to represent 0 e.g. Car_Brands.None. – James Sep 14 '13 at 15:11
  • @James, Having only 1 car brand wouldn't be a problem, having over 100, would be easier to look up. – Silvermind Sep 14 '13 at 18:30
  • @Silverkind sorry still don't understand what difference it makes? Regardless if its set explicitly or not - the value is the same (at least in this circumstance). Obviously if you need *specific* values its different i.e. 100, 200 etc. – James Sep 15 '13 at 01:40
  • @James, I know that the value remains the same, but if you have a value like 100 in the database, it is easier to look at your code and search the value 100 than to manually count the enum-values. – Silvermind Sep 16 '13 at 16:04

2 Answers2

3
Type type = Type.GetType("full namespace where you declare enum" + "." + value);
var brand_models = Enum.GetNames(type);

in case of nested type you need to use "+" instead of "."

C# : having a "+" in the class name?

Community
  • 1
  • 1
Damith
  • 62,401
  • 13
  • 102
  • 153
  • @Damith I have tested with many options : Type type = Type.GetType("Cars" + "." + value); or Type type = Type.GetType("Cars."+value); but returns me NULL – c_conl Sep 14 '13 at 15:54
  • 1
    @c_conl you are missing the point, when you load the type you need to use the **full** namespace e.g. MyApplication.Cars – James Sep 15 '13 at 01:42
1

I can think of a lot of better ways on how to solve your task but this should work under the circumstances you chose:

private void comboBox3_SelectedValueChanged(object sender, EventArgs e)
{
  string value = comboBox1.Text;   //car brand
  Type type = Type.GetType("YOUR_NAMESPACE." + value);
  var brand_models = Enum.GetNames(type);
  foreach (string enumValue in brand_models)
  {
    string brand_model = enumValue;
    MessageBox.Show(brand_model);
  }
}

Please read up on the Type.GetType documentation (to be found here) to get the correct solution for your specific class hierarchy and assembly situation.

meilke
  • 3,280
  • 1
  • 15
  • 31
  • Did you provide the actual namespace or just "namespace."? You need to provide the actual namespace. – meilke Sep 14 '13 at 12:57
  • I use the actual namespace : Type type = Type.GetType("Cars." + value); It returns always NULL, I read that I must use AssemblyName, is it the solution, how can I do that? – c_conl Sep 14 '13 at 15:50
  • I assume that the GUI code is in a different assembly than the enumerations. Can you provide the full picture? – meilke Sep 14 '13 at 15:57