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 input class name by user?

How to input class name by user?

Scheduled Pinned Locked Moved C#
databasetutorialquestion
6 Posts 2 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.
  • W Offline
    W Offline
    wjbjnr
    wrote on last edited by
    #1

    Hi! I am making Linqtosql application in which classes are used in mapping of database. I want to let the user input the class name from interface(windows form) for e.g if Class name = hello its object declaration is e.g hello object = new hello() now in this I want the user to enter the class name "hello" i tried storing user input in a string , but doesnt work , what to do?

    S 1 Reply Last reply
    0
    • W wjbjnr

      Hi! I am making Linqtosql application in which classes are used in mapping of database. I want to let the user input the class name from interface(windows form) for e.g if Class name = hello its object declaration is e.g hello object = new hello() now in this I want the user to enter the class name "hello" i tried storing user input in a string , but doesnt work , what to do?

      S Offline
      S Offline
      Steven Pinto2000
      wrote on last edited by
      #2

      Use reflections in C# with reflections u can store the class name in a string and then create the object of that class at run time Click Here:cool:

      modified on Wednesday, May 4, 2011 1:48 AM

      W 1 Reply Last reply
      0
      • S Steven Pinto2000

        Use reflections in C# with reflections u can store the class name in a string and then create the object of that class at run time Click Here:cool:

        modified on Wednesday, May 4, 2011 1:48 AM

        W Offline
        W Offline
        wjbjnr
        wrote on last edited by
        #3

        I couldn't understand the tutorial.. plz just in simple explanation , tell that how can i store a class name in string format? I have a class let say ABC let say it has fields. class abc { int a; int b; } now if i want to make an object of abc in an another class, through user entering the class name , how to do using reflections? I am sure it is just 2 lines of code :)

        S 1 Reply Last reply
        0
        • W wjbjnr

          I couldn't understand the tutorial.. plz just in simple explanation , tell that how can i store a class name in string format? I have a class let say ABC let say it has fields. class abc { int a; int b; } now if i want to make an object of abc in an another class, through user entering the class name , how to do using reflections? I am sure it is just 2 lines of code :)

          S Offline
          S Offline
          Steven Pinto2000
          wrote on last edited by
          #4

          Type type = Type.GetType("abc");
          object obj = Activator.CreateInstance(type);

          pass the class name in the GetType function if the class has a namespace pass it with the namespace.classname rate me if this post was helpful

          modified on Monday, May 9, 2011 6:51 AM

          W 1 Reply Last reply
          0
          • S Steven Pinto2000

            Type type = Type.GetType("abc");
            object obj = Activator.CreateInstance(type);

            pass the class name in the GetType function if the class has a namespace pass it with the namespace.classname rate me if this post was helpful

            modified on Monday, May 9, 2011 6:51 AM

            W Offline
            W Offline
            wjbjnr
            wrote on last edited by
            #5

            i tried it. I had the namespace FYP_RFID and class name RECORDS. Type type = Type.GetType("FYP_RFID.RECORDS"); object obj = Activator.CreateInstance(type); BUT when i try to acces any member uding obj. nothing happens but when i manually make the object like RECORDS OBJ = NEW RECORDS(); THEN through obj. function i can acess the members of class. what could be the problem? (I appreciate you helping me out:) )

            S 1 Reply Last reply
            0
            • W wjbjnr

              i tried it. I had the namespace FYP_RFID and class name RECORDS. Type type = Type.GetType("FYP_RFID.RECORDS"); object obj = Activator.CreateInstance(type); BUT when i try to acces any member uding obj. nothing happens but when i manually make the object like RECORDS OBJ = NEW RECORDS(); THEN through obj. function i can acess the members of class. what could be the problem? (I appreciate you helping me out:) )

              S Offline
              S Offline
              Steven Pinto2000
              wrote on last edited by
              #6

              There is a class called MethodInfo where in you will get all the info of the methods in the object and i would suggest you to learn this from any forums because it would help you more to learn about it like we could only guide you how to do a particular thing it would be good for you to learn new things

              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