Create object through classname

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bert

    Create object through classname

    Hi,

    I want to create an instance of a class by means of the classname. Can't figure out how.

    in pseudocode this would look like:

    Dim MyClassName as String = "MyClass"
    Dim MyObject as New MyClasses(MyCla ssName)

    Thanks for your help,

    Egbert
  • Armin Zingler

    #2
    Re: Create object through classname

    "Bert" <edoorduin@aequ or.nl> schrieb[color=blue]
    > Hi,
    >
    > I want to create an instance of a class by means of the classname.
    > Can't figure out how.
    >
    > in pseudocode this would look like:
    >
    > Dim MyClassName as String = "MyClass"
    > Dim MyObject as New MyClasses(MyCla ssName)[/color]


    I wonder why people need the class name at runtime, but if it's necessary,
    have a look at System.Activato r.CreateInstanc e


    Armin

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Create object through classname

      Egbert,

      Do you have a goal with this thousand times by newbies asked questions who
      want to convert VBA functions to VBNet?

      Be aware that VBNet does beside using it for giving debugging informations
      nothing with Object names, it is just for you to make it readable.

      Cor


      Comment

      • Carlos J. Quintero [VB MVP]

        #4
        Re: Create object through classname

        Hi Egbert,

        As Armin has said, you can use the System.Activato r.CreateInstanc e methods
        to create an instance, but be aware that this pseudo-code:

        Dim MyClassName as String = "MyClass"
        Dim MyObject as New MyClasses(MyCla ssName)

        is not very robust. If the name of the type "MyClass" changes and you don´t
        update that string, your code will fail at run-time. A more common approach
        is to create the instance from the type:
        System.Activato r.CreateInstanc e(type), and you can get the type through
        several ways. For example, you can get the types of an assembly with
        Assembly.GetTyp es, or only the public types with Assembly.GetExp ortedTypes.
        You can examine some properties (the bases or implemented interfaces) of the
        type to guess which ones you want to create instances from. This is quite
        common when creating plug-in frameworks.

        Bottom line: if you know what you are doing, OK, but if not you can think
        about the above.

        --

        Best regards,

        Carlos J. Quintero

        MZ-Tools: Productivity add-ins for Visual Studio
        You can code, design and document much faster:
        MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.



        "Bert" <edoorduin@aequ or.nl> escribió en el mensaje
        news:%238CT6TiN GHA.4052@TK2MSF TNGP15.phx.gbl. ..
        Hi,

        I want to create an instance of a class by means of the classname. Can't
        figure out how.

        in pseudocode this would look like:

        Dim MyClassName as String = "MyClass"
        Dim MyObject as New MyClasses(MyCla ssName)

        Thanks for your help,

        Egbert


        Comment

        • Bert

          #5
          Re: Create object through classname

          Thanks Armin, Carlos and Cor,

          I do have a goal with this question, I'm using a form that uses objects from different classes, depending on user's input. So in order to create the objects I need this code:

          Function CreateObject("C lassName" as String) as Object
          dim oType as Type = Type.GetType("C lassName")
          Return Activator.Creat eInstance(oType )
          End Function

          No worries about changing names, since everything is decribed and maintained in the databases.

          Egbert

          "Armin Zingler" <az.nospam@free net.de> wrote in message news:e$QtEciNGH A.3284@TK2MSFTN GP14.phx.gbl...[color=blue]
          > "Bert" <edoorduin@aequ or.nl> schrieb[color=green]
          >> Hi,
          >>
          >> I want to create an instance of a class by means of the classname.
          >> Can't figure out how.
          >>
          >> in pseudocode this would look like:
          >>
          >> Dim MyClassName as String = "MyClass"
          >> Dim MyObject as New MyClasses(MyCla ssName)[/color]
          >
          >
          > I wonder why people need the class name at runtime, but if it's necessary,
          > have a look at System.Activato r.CreateInstanc e
          >
          >
          > Armin
          >[/color]

          Comment

          • Armin Zingler

            #6
            Re: Create object through classname

            "Bert" <edoorduin@aequ or.nl> schrieb[color=blue]
            > Thanks Armin, Carlos and Cor,
            >
            > I do have a goal with this question, I'm using a form that uses
            > objects from different classes, depending on user's input. So in
            > order to create the objects I need this code:
            >
            > Function CreateObject("C lassName" as String) as Object
            > dim oType as Type = Type.GetType("C lassName")
            > Return Activator.Creat eInstance(oType )
            > End Function
            >
            > No worries about changing names, since everything is decribed and
            > maintained in the databases.[/color]


            After creating the object, do you intend to use the members inherited
            from System.Object only?


            Armin

            Comment

            • Bert

              #7
              Re: Create object through classname

              No, I use the members by means of Reflection.

              Egbert

              "Armin Zingler" <az.nospam@free net.de> wrote in message news:epYI%23YjN GHA.428@tk2msft ngp13.phx.gbl.. .[color=blue]
              > "Bert" <edoorduin@aequ or.nl> schrieb[color=green]
              >> Thanks Armin, Carlos and Cor,
              >>
              >> I do have a goal with this question, I'm using a form that uses
              >> objects from different classes, depending on user's input. So in
              >> order to create the objects I need this code:
              >>
              >> Function CreateObject("C lassName" as String) as Object
              >> dim oType as Type = Type.GetType("C lassName")
              >> Return Activator.Creat eInstance(oType )
              >> End Function
              >>
              >> No worries about changing names, since everything is decribed and
              >> maintained in the databases.[/color]
              >
              >
              > After creating the object, do you intend to use the members inherited
              > from System.Object only?
              >
              >
              > Armin
              >[/color]

              Comment

              • Armin Zingler

                #8
                Re: Create object through classname

                "Bert" <edoorduin@aequ or.nl> schrieb[color=blue][color=green][color=darkred]
                > > >
                > > > Function CreateObject("C lassName" as String) as Object
                > > > dim oType as Type = Type.GetType("C lassName")
                > > > Return Activator.Creat eInstance(oType )
                > > > End Function
                > > >
                > > > No worries about changing names, since everything is decribed
                > > > and maintained in the databases.[/color]
                > >
                > >
                > > After creating the object, do you intend to use the members
                > > inherited from System.Object only?[/color]
                >
                > No, I use the members by means of Reflection.[/color]


                I see. I asked only because otherwise I would have suggested a common
                interface or base class.


                Armin

                Comment

                • Herfried K. Wagner [MVP]

                  #9
                  Re: Create object through classname

                  "Bert" <edoorduin@aequ or.nl> schrieb:[color=blue]
                  >I want to create an instance of a class by means of the classname. Can't
                  >figure out how.
                  >
                  >in pseudocode this would look like:
                  >
                  >Dim MyClassName as String = "MyClass"
                  >Dim MyObject as New MyClasses(MyCla ssName)[/color]

                  Check out the snippets at
                  <URL:http://dotnet.mvps.org/dotnet/code/techniques/#ClassByName>.

                  --
                  M S Herfried K. Wagner
                  M V P <URL:http://dotnet.mvps.org/>
                  V B <URL:http://classicvb.org/petition/>

                  Comment

                  Working...