0

I want to convert an item selected by a user in a combo box into executable assembly, so I can output directly from a class, without writing individual if statements. I did search this up and could not find an answer that worked for me. The error message that I got for lblOne.Text = cboComboBox.SelectedItem.aString is: 'object' does not contain a definition for 'aString' and no accessible extension method 'aString' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?). Sorry if I posted too much code, this is my second time posting here. Here is my attempt to code it:

// in public partial class Form1: Form    : A one = new A("One", "Cool");

public class A
    {

        // Instance Variables 
        string Name;
        string Info;

        // Constructor Declaration of Class 
        public A(string Name, string Info)
        {
            this.Name = Name;
            this.Info = Info;
        }

        // method 1 
        public string getName()
        {
            return Name;
        }

        // method 2 
        public string getInfo()
        {
            return Info;
        }

       //output to label

        public string aString()
        {
            return ("Name: " + this.getName() + "\n" +
                    "Info: " + this.getInfo() + "\n");
        }

    private void btnStats_Click(object sender, EventArgs e)
    {
        lblOne.Text = cboComboBox.SelectedItem.aString();
        //doesn't actually work
        //what I want it to be in code: lblOne.Text = one.aString();
    }
Cloud
  • 1
  • 3
  • When you say "into code"? You mean you want to compile the text in the combobox into an executable assembly? – ProgrammingLlama Dec 13 '19 at 02:45
  • @John_ReinstateMonica Yes, sorry. – Cloud Dec 13 '19 at 02:46
  • Can you give an example of the code in the combobox? C# doesn't have an equivalent of Javascript's `eval()`, so you need to actually compile a new assembly containing the code. See [this question](https://stackoverflow.com/questions/4629/how-can-i-evaluate-c-sharp-code-dynamically) for example. – ProgrammingLlama Dec 13 '19 at 02:47
  • @John_ReinstateMonica Not sure if I'm understanding what you're talking about correctly as I'm new to C#. I don't have any code in the combobox. I just have multiple items in it and I want to convert them into executable code when selected. – Cloud Dec 13 '19 at 02:51
  • Converting text to executable code means having a string in the combobox like `"public static int Add(int a, int b) { return a + b; }"` and converting that into something that you can actually call like any other method. Can you confirm that this is what you're asking for? – ProgrammingLlama Dec 13 '19 at 02:54
  • For example, this article? [Get selected Object from Combobox](https://stackoverflow.com/q/18577955/9014308) – kunif Dec 13 '19 at 02:54
  • @John_ReinstateMonica That sounds right. – Cloud Dec 13 '19 at 02:58
  • 1
    Or this article may be helpful.[How do you integrate the C# Scripting API (csi.exe) with a host program?](https://stackoverflow.com/q/39775568/9014308) – kunif Dec 13 '19 at 04:06

1 Answers1

0

SelectedItem property of combobox/listbox etc returns the selected item as an Object.
You need to cast it as your class type before using the property

((YourClassName)comboBox.SelectedItem).YourProperty

or

(comboBox.SelectedItem as YourClassName).YourProperty
Athul Raj
  • 189
  • 1
  • 9
  • Thanks for answering. However, I don't know where this goes in my code as I am fairly new to C#. – Cloud Dec 13 '19 at 02:55
  • lblOne.Text = cboComboBox.SelectedItem.aString; you can write it as lblOne.Text = ((YourClassName)cboComboBox.SelectedItem).aString. replace YourClassName with the your class which has the aString Property – Athul Raj Dec 13 '19 at 02:58
  • How do I add the aString Property to my class? – Cloud Dec 13 '19 at 03:03
  • What did you mean by cboComboBox.SelectedItem.aString; What are the items that are you displaying in the combobox ? Do you want to display the content of selected item in a label when you click a button ? – Athul Raj Dec 13 '19 at 03:07
  • Let's say that I am displaying numbers in the combobox. I have classes set up for all those numbers and what I want to do is lblOne.Text = (class).aString, so when an item is chosen it goes into (class) and all the information about it is displayed with the .aString property (which already works). – Cloud Dec 13 '19 at 03:11
  • I have posted the class at the top of my code: A one = new A("One", "Cool"); – Cloud Dec 13 '19 at 03:17
  • try lblOne.Text = ((A)cboComboBox.SelectedItem).aString() – Athul Raj Dec 13 '19 at 03:18
  • Sorry my previous comment was wrong, the error message is on .aString: Form1.A does not contain a definition for 'aString' and no accesible extension message 'aString' accepting a first argument of Type 'Form1.A' could be found (are you missing a using directive or an assembly reference?). – Cloud Dec 13 '19 at 03:24
  • Could you rephrase your question ? I'm not getting what you want here ? 1) Post the code where you populate the combobox 2) Why do you have a click event inside the class A ? it should be inside the form – Athul Raj Dec 13 '19 at 03:27
  • Here is all my actual code: https://github.com/xCloudzx/Gun/commit/e5a4b675259eb8228577377e60e6444f94e26972. I want the name of the item that the user selected to be converted into executable code so I can call a class just by using the name of item and output all its information with .aString. – Cloud Dec 13 '19 at 03:52
  • @Cloud It sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You're trying to create code from a value, rather than using that value as a lookup in a Dictionary, for example. – ProgrammingLlama Dec 13 '19 at 05:03
  • @John_ReinstateMonica You're right, but I didn't know of any other possible way. How would I change my code to just use the value as a lookup? – Cloud Dec 13 '19 at 05:52