How to variable, write the type of object and then use it? For example, there is a type VkNet.Enums.SafetyEnums.PhotoAlbumType.Wall it needs to be written. I just have a comboBox , it has 3 lines and, based on the selected line (by index), I need to return the desired type.

Type of such:

 switch (comboBox1.SelectedIndex) { case 0: fff = VkNet.Enums.SafetyEnums.PhotoAlbumType.Wall; break; case 1: fff = VkNet.Enums.SafetyEnums.PhotoAlbumType.Profile; break; case 2: fff = VkNet.Enums.SafetyEnums.PhotoAlbumType.Saved; break; } 

    1 answer 1

    If I'm not mistaken, then you need a ComboBox.SelectedItem .

    Bring it to the desired type and analyze.

    But this is on condition that you have the necessary object in the DataSource, and not just text, as for example:

     public class Product { public int id { get; set; } public string Name { get; set; } public int Cnt { get; set; } } listProd = new List<Product>(); listProd.Add(new Product() { id = 2, Name = "Прод 2", Cnt = 2 }); listProd.Add(new Product() { id = 3, Name = "Прод 3", Cnt = 4 }); comboBox1.DataSource = listProd; comboBox1.ValueMember = "id"; comboBox1.DisplayMember = "Name"; 

    Then, as I said, it will be possible to sell such a thing:

     var selVal = (Product) comboBox1.SelectedItem; 
    • ComboBox.SelectedItem returns the selected string in the comboBox , how is my problem related to this? Or I misunderstood you, please give an example if it does not make it difficult :) - Maxim
    • @ Maxim, what do you have in the ComboBox itself? Just a string? Not any dictionary? - iluxa1810
    • And, I understood the course of your thoughts, but no, I have my text in the ComboBox . - Max
    • @Max, can you make a dictionary <Text, Type> in your task and link it to the ComboBox? Then you could get by index the type that belongs to the selected type. - iluxa1810
    • But how to do it? Something I can not figure out. - Max