Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to initiate a class stored in string variable

how to initiate a class stored in string variable

Scheduled Pinned Locked Moved C#
tutorial
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Asif Rehman
    wrote on last edited by
    #1

    I know its a silly ques. I've got treeview control which holds different class names. In normal ways what we do is.

    Ayclass cls= new AnyClass();

    now the AnyClass's name is stored in a variable. how to instantiate a class. here's the failed code.

    string varClass= treeview1.node.ToString();
    varclass d= new varClass();

    Hope I make myself clarify.

    G U 2 Replies Last reply
    0
    • A Asif Rehman

      I know its a silly ques. I've got treeview control which holds different class names. In normal ways what we do is.

      Ayclass cls= new AnyClass();

      now the AnyClass's name is stored in a variable. how to instantiate a class. here's the failed code.

      string varClass= treeview1.node.ToString();
      varclass d= new varClass();

      Hope I make myself clarify.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Normally you don't do this at all. It's possible using reflection, but having to use reflection is usually an indication that you are doing something backwards. What is it that you are trying to accomplish, really? You should ask for a solution for that, instead of asking about the way that you think that it's solved.

      --- single minded; short sighted; long gone;

      1 Reply Last reply
      0
      • A Asif Rehman

        I know its a silly ques. I've got treeview control which holds different class names. In normal ways what we do is.

        Ayclass cls= new AnyClass();

        now the AnyClass's name is stored in a variable. how to instantiate a class. here's the failed code.

        string varClass= treeview1.node.ToString();
        varclass d= new varClass();

        Hope I make myself clarify.

        U Offline
        U Offline
        Urs Enzler
        wrote on last edited by
        #3

        If you are sure that's the way to go (see post from Guffa) then this is how it's working: object d = Activator.CreateInstance(Type.GetType(typeName)); see help on CreateInstance for further information.

        -^-^-^-^-^- no risk no funk

        A 1 Reply Last reply
        0
        • U Urs Enzler

          If you are sure that's the way to go (see post from Guffa) then this is how it's working: object d = Activator.CreateInstance(Type.GetType(typeName)); see help on CreateInstance for further information.

          -^-^-^-^-^- no risk no funk

          A Offline
          A Offline
          Asif Rehman
          wrote on last edited by
          #4

          here's what I want. here's chart reports in treeview control for end user to display. Sales_Monthwise Sales_Citywise sales_PartyWise when a user clicks Sales_Monthwise I want open Charting Class with the same name. one possible way is:

          if (treeview.node.text == "Sales_MonthWise) {
          Sales_Monthwise sm= new Sales_Monthwise();
          sm.Show();
          }

          bcoz there are so many classes I don't want to insert logic for every class.

          C 1 Reply Last reply
          0
          • A Asif Rehman

            here's what I want. here's chart reports in treeview control for end user to display. Sales_Monthwise Sales_Citywise sales_PartyWise when a user clicks Sales_Monthwise I want open Charting Class with the same name. one possible way is:

            if (treeview.node.text == "Sales_MonthWise) {
            Sales_Monthwise sm= new Sales_Monthwise();
            sm.Show();
            }

            bcoz there are so many classes I don't want to insert logic for every class.

            C Offline
            C Offline
            CiNN
            wrote on last edited by
            #5

            Use an Interface

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups