locked
How to dynamically declare the class RRS feed

  • Question

  • Hi,

    I have a class like this:

    reports_controls.userControl_1001 report_1001 = new reports_controls.userControl_1001();

    I want to dynamically declare it based on user input because user might choose report ID 1001, 1002, etc so it should dynamically declare it.

    How can i do this please?

    Thanks,

    Jassim

    Sunday, May 22, 2016 10:38 AM

Answers

  • >>How can i do this please?

    Short answer: You can't.

    You can't can't create variables dynamically in C#. This is not supported.

    You may want to use a dictionary or ExpandoObject as suggested here: http://stackoverflow.com/questions/5033675/how-do-i-name-variables-dynamically-in-c

    You would then add dynamic keys/value pairs to a dictionary instead of creating variables.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by DotNet Wang Monday, May 23, 2016 5:12 AM
    • Marked as answer by DotNet Wang Monday, May 30, 2016 9:31 AM
    Sunday, May 22, 2016 5:06 PM
  • It seems a line of C# code.

    I encourage myself to make a reasonable speculation about what you want to do. I thinking you are invoking a coding way of Polymorphism, a little advanced programming technique. I try to answer it in C++ way. In terms of your question, the code in C++ may be like the following:

    class reports_control {
        public:
            // The "virtual" here is a must.
            void virtual do_something() {
                // Do common tasks or nothing here.
            }
    };

    class reports_control_1001 : public reports_control {
        public:
            // The "virtual" here is optional.
            void virtual do_something() {
                // Do tasks specific to report 1001 here.
            }
    };

    class reports_control_1002 : public reports_control {
        public:
            // The "virtual" here is optional.
            void virtual do_something() {
                // Do tasks specific to report 1002 here.
            }
    };

    reports_control* ReportsControlMaker(int report_id)
    {
        switch(report_id) {
        case 1001:
            return new reports_control_1001();

        case 1002:
            return new reports_control_1002();

        default:
            return nullptr;
        }
    }

    void program_somewhere()
    {
        ......

        // report_id_user_selected is an integer, report ID, coming from user choice, the value is 1001, 1002, etc.

        auto report = ReportsControlMaker(report_id_user_selected);

        report->do_something();

        ......
    }

    The code above was not tested. You probably need to convert it to C#.

    • Proposed as answer by DotNet Wang Monday, May 23, 2016 5:12 AM
    • Marked as answer by DotNet Wang Monday, May 30, 2016 9:31 AM
    Sunday, May 22, 2016 4:02 PM
  • Hi Jassim Rahma,

    You could use reflection to do it. .NET reflection allow us to create an object instance based on the type name. Code below is for your reference.

    string userInput = "1001";
    string typeName = "reports_controls.userControl_" + userInput;
    object report = Activator.CreateInstance(Type.GetType(typeName));

    Do userControl_1001 and userControl_1002 implement a same interface or a class? If yes, you could convert the report instance to a typed instance. After that, you could use its properties or methods.

    //suppose you have a class named reports_controls.userControl which is inherited by all these reports controls.
    reports_controls.userControl control = report as reports_controls.userControl;

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by DotNet Wang Monday, May 30, 2016 9:31 AM
    Tuesday, May 24, 2016 6:08 AM

All replies

  • It seems a line of C# code.

    I encourage myself to make a reasonable speculation about what you want to do. I thinking you are invoking a coding way of Polymorphism, a little advanced programming technique. I try to answer it in C++ way. In terms of your question, the code in C++ may be like the following:

    class reports_control {
        public:
            // The "virtual" here is a must.
            void virtual do_something() {
                // Do common tasks or nothing here.
            }
    };

    class reports_control_1001 : public reports_control {
        public:
            // The "virtual" here is optional.
            void virtual do_something() {
                // Do tasks specific to report 1001 here.
            }
    };

    class reports_control_1002 : public reports_control {
        public:
            // The "virtual" here is optional.
            void virtual do_something() {
                // Do tasks specific to report 1002 here.
            }
    };

    reports_control* ReportsControlMaker(int report_id)
    {
        switch(report_id) {
        case 1001:
            return new reports_control_1001();

        case 1002:
            return new reports_control_1002();

        default:
            return nullptr;
        }
    }

    void program_somewhere()
    {
        ......

        // report_id_user_selected is an integer, report ID, coming from user choice, the value is 1001, 1002, etc.

        auto report = ReportsControlMaker(report_id_user_selected);

        report->do_something();

        ......
    }

    The code above was not tested. You probably need to convert it to C#.

    • Proposed as answer by DotNet Wang Monday, May 23, 2016 5:12 AM
    • Marked as answer by DotNet Wang Monday, May 30, 2016 9:31 AM
    Sunday, May 22, 2016 4:02 PM
  • >>How can i do this please?

    Short answer: You can't.

    You can't can't create variables dynamically in C#. This is not supported.

    You may want to use a dictionary or ExpandoObject as suggested here: http://stackoverflow.com/questions/5033675/how-do-i-name-variables-dynamically-in-c

    You would then add dynamic keys/value pairs to a dictionary instead of creating variables.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by DotNet Wang Monday, May 23, 2016 5:12 AM
    • Marked as answer by DotNet Wang Monday, May 30, 2016 9:31 AM
    Sunday, May 22, 2016 5:06 PM
  • you mean something like this:

    dynamic d = new ExpandoObject();
    ((IDictionary<string, object>)d)["MyProperty"] =  5;
    int val = d.MyProperty; // 5
    

    but how can I use it with this:

    reports_controls.userControl_1001 report_1001 = new reports_controls.userControl_1001();
    reports_controls.userControl_1002 report_1002 = new reports_controls.userControl_1002();
    reports_controls.userControl_1003 report_1003 = new reports_controls.userControl_1003();



    Monday, May 23, 2016 2:07 PM
  • Hi Jassim Rahma,

    You could use reflection to do it. .NET reflection allow us to create an object instance based on the type name. Code below is for your reference.

    string userInput = "1001";
    string typeName = "reports_controls.userControl_" + userInput;
    object report = Activator.CreateInstance(Type.GetType(typeName));

    Do userControl_1001 and userControl_1002 implement a same interface or a class? If yes, you could convert the report instance to a typed instance. After that, you could use its properties or methods.

    //suppose you have a class named reports_controls.userControl which is inherited by all these reports controls.
    reports_controls.userControl control = report as reports_controls.userControl;

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by DotNet Wang Monday, May 30, 2016 9:31 AM
    Tuesday, May 24, 2016 6:08 AM
  • >>but how can I use it with this:

    As already mentioned you cannot create variables dynamically in C#.

    As an alternative, you could put values in a dictionary or ExpandoObject as I suggested or you could use reflection as suggested by DotNet Wang.

    Hope that helps.

    Please remember to close your threads by marking all helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread and please don't ask the same question more than once.

    Tuesday, May 24, 2016 7:38 PM