creating a new instance of an object XXX type

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

    creating a new instance of an object XXX type

    Hi,
    Is it possible to do this in vb.net :

    1.)To make a function that would have a string parameter ClassName
    2.)This function would return an object of the same type as the ClassName
    parameter was.

    For example, in my project a have this classes : ClassPerson, ClassCar,
    ClassXXX....... ....

    Dim objectA as Object = GetNewObject(Cl assPerson)
    ' ObjectA would be of a type ClassPeron

    Public function GetNewObject(Cl assName as string)
    return new object instance of a type ClassName
    End function

    Thanks for your help!


  • One Handed Man [ OHM# ]

    #2
    Re: creating a new instance of an object XXX type




    Can see why you want to do this but !


    (Untested )

    Class CGetter

    public FunctionGetObje ct( cname as String ) return Object

    Select Case case cname

    case "ClassPerso n"
    return New ClassPerson()
    case "ClassCar"
    return New ClassCar()
    case Else
    return Nothing
    End Select

    End Function

    End Class





    jernej goricki wrote:[color=blue]
    > Hi,
    > Is it possible to do this in vb.net :
    >
    > 1.)To make a function that would have a string parameter ClassName
    > 2.)This function would return an object of the same type as the
    > ClassName parameter was.
    >
    > For example, in my project a have this classes : ClassPerson,
    > ClassCar, ClassXXX....... ....
    >
    > Dim objectA as Object = GetNewObject(Cl assPerson)
    > ' ObjectA would be of a type ClassPeron
    >
    > Public function GetNewObject(Cl assName as string)
    > return new object instance of a type ClassName
    > End function
    >
    > Thanks for your help![/color]

    Regards - OHM# OneHandedMan{at }BTInternet{dot }com


    Comment

    • Scott M.

      #3
      Re: creating a new instance of an object XXX type

      So you are saying that you want a function to return a dynamically
      determined class.

      You can't do what you have below unless you plan on returning a string
      because that's what the function says it returns. In your example below,
      you don't indicate what the return type from the function is.

      You need to know up front what type your function will return. You could do
      this I suppose:

      Public Function Test(x as object) as Object
      some code
      Dim x as new x.GetType
      return x
      End Function


      "jernej goricki" <ph-enom@obmocje51. net> wrote in message
      news:%23nnV1axx DHA.1740@TK2MSF TNGP09.phx.gbl. ..[color=blue]
      > Hi,
      > Is it possible to do this in vb.net :
      >
      > 1.)To make a function that would have a string parameter ClassName
      > 2.)This function would return an object of the same type as the ClassName
      > parameter was.
      >
      > For example, in my project a have this classes : ClassPerson, ClassCar,
      > ClassXXX....... ....
      >
      > Dim objectA as Object = GetNewObject(Cl assPerson)
      > ' ObjectA would be of a type ClassPeron
      >
      > Public function GetNewObject(Cl assName as string)
      > return new object instance of a type ClassName
      > End function
      >
      > Thanks for your help!
      >
      >[/color]


      Comment

      • One Handed Man [ OHM# ]

        #4
        TYPO


        Class CGetter

        public FunctionGetObje ct( cname as String ) As Object ( Not return object as
        f*&ked up in above post )

        Select Case case cname

        case "ClassPerso n"
        return New ClassPerson()
        case "ClassCar"
        return New ClassCar()
        case Else
        return Nothing
        End Select

        End Function

        End Class



        Comment

        • Blumidoo

          #5
          Re: creating a new instance of an object XXX type

          > Hi,[color=blue]
          > Is it possible to do this in vb.net :
          >
          > 1.)To make a function that would have a string parameter ClassName
          > 2.)This function would return an object of the same type as the ClassName
          > parameter was.[/color]

          What you are trying to do is dirty, very dirty. You should rather design and
          implement an AbstractFactory design pattern class. AbstractFactory design
          pattern allows you to returns an instance of one of several possible classes
          depending on the data provided to it.
          Read http://www.dotnetextreme.com/code/SimpleFactory.asp for more
          information about simple AbstractFactory design pattern implementations .

          sincerely,
          --
          Sebastian Zaklada
          Skilled Software

          *************** *************** ******
          SQL Source Control 2003 - for
          SQL Server Source Safe integration
          and custom databases documentation


          Comment

          • jernej goricki

            #6
            Re: creating a new instance of an object XXX type

            yes this with select case would probably work, but I want to do this without
            writing a new "select case" line for every ne class I will create.

            Is possible to do this dynamically something like this:

            return new object.Gettype( "Class Name")




            "One Handed Man [ OHM# ]" <OneHandedMan{a t}BTInternet{do t}com> wrote in
            message news:uCBxpixxDH A.3416@tk2msftn gp13.phx.gbl...[color=blue]
            >
            >
            >
            > Can see why you want to do this but !
            >
            >
            > (Untested )
            >
            > Class CGetter
            >
            > public FunctionGetObje ct( cname as String ) return Object
            >
            > Select Case case cname
            >
            > case "ClassPerso n"
            > return New ClassPerson()
            > case "ClassCar"
            > return New ClassCar()
            > case Else
            > return Nothing
            > End Select
            >
            > End Function
            >
            > End Class
            >
            >
            >
            >
            >
            > jernej goricki wrote:[color=green]
            > > Hi,
            > > Is it possible to do this in vb.net :
            > >
            > > 1.)To make a function that would have a string parameter ClassName
            > > 2.)This function would return an object of the same type as the
            > > ClassName parameter was.
            > >
            > > For example, in my project a have this classes : ClassPerson,
            > > ClassCar, ClassXXX....... ....
            > >
            > > Dim objectA as Object = GetNewObject(Cl assPerson)
            > > ' ObjectA would be of a type ClassPeron
            > >
            > > Public function GetNewObject(Cl assName as string)
            > > return new object instance of a type ClassName
            > > End function
            > >
            > > Thanks for your help![/color]
            >
            > Regards - OHM# OneHandedMan{at }BTInternet{dot }com
            >
            >[/color]


            Comment

            • One Handed Man [ OHM# ]

              #7
              Re: creating a new instance of an object XXX type

              There may well be some obscure way to achieve this, but It does not sound
              very structured because. What you imply is that your factory class does not
              need to validate if a class exists before it tries to create it.

              IMHO This is not good sense.

              Regards - OHM


              jernej goricki wrote:[color=blue]
              > yes this with select case would probably work, but I want to do this
              > without writing a new "select case" line for every ne class I will
              > create.
              >
              > Is possible to do this dynamically something like this:
              >
              > return new object.Gettype( "Class Name")
              >
              >
              >
              >
              > "One Handed Man [ OHM# ]" <OneHandedMan{a t}BTInternet{do t}com> wrote
              > in message news:uCBxpixxDH A.3416@tk2msftn gp13.phx.gbl...[color=green]
              >>
              >>
              >>
              >> Can see why you want to do this but !
              >>
              >>
              >> (Untested )
              >>
              >> Class CGetter
              >>
              >> public FunctionGetObje ct( cname as String ) return Object
              >>
              >> Select Case case cname
              >>
              >> case "ClassPerso n"
              >> return New ClassPerson()
              >> case "ClassCar"
              >> return New ClassCar()
              >> case Else
              >> return Nothing
              >> End Select
              >>
              >> End Function
              >>
              >> End Class
              >>
              >>
              >>
              >>
              >>
              >> jernej goricki wrote:[color=darkred]
              >>> Hi,
              >>> Is it possible to do this in vb.net :
              >>>
              >>> 1.)To make a function that would have a string parameter ClassName
              >>> 2.)This function would return an object of the same type as the
              >>> ClassName parameter was.
              >>>
              >>> For example, in my project a have this classes : ClassPerson,
              >>> ClassCar, ClassXXX....... ....
              >>>
              >>> Dim objectA as Object = GetNewObject(Cl assPerson)
              >>> ' ObjectA would be of a type ClassPeron
              >>>
              >>> Public function GetNewObject(Cl assName as string)
              >>> return new object instance of a type ClassName
              >>> End function
              >>>
              >>> Thanks for your help![/color]
              >>
              >> Regards - OHM# OneHandedMan{at }BTInternet{dot }com[/color][/color]

              Regards - OHM# OneHandedMan{at }BTInternet{dot }com


              Comment

              • Herfried K. Wagner [MVP]

                #8
                Re: creating a new instance of an object XXX type

                * "jernej goricki" <ph-enom@obmocje51. net> scripsit:[color=blue]
                > Is it possible to do this in vb.net :
                >
                > 1.)To make a function that would have a string parameter ClassName
                > 2.)This function would return an object of the same type as the ClassName
                > parameter was.[/color]

                <http://groups.google.c om/groups?selm=bqn s3k%2424ghqf%24 2%40ID-208219.news.uni-berlin.de>

                --
                Herfried K. Wagner [MVP]
                <http://www.mvps.org/dotnet>

                Comment

                • jernej goricki

                  #9
                  Re: creating a new instance of an object XXX type

                  that is just the thing im looking for, thanks
                  "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                  news:OP6tg9xxDH A.2156@TK2MSFTN GP09.phx.gbl...[color=blue]
                  > * "jernej goricki" <ph-enom@obmocje51. net> scripsit:[color=green]
                  > > Is it possible to do this in vb.net :
                  > >
                  > > 1.)To make a function that would have a string parameter ClassName
                  > > 2.)This function would return an object of the same type as the[/color][/color]
                  ClassName[color=blue][color=green]
                  > > parameter was.[/color]
                  >
                  >[/color]
                  <http://groups.google.com/groups?selm...-208219.news.u
                  ni-berlin.de>[color=blue]
                  >
                  > --
                  > Herfried K. Wagner [MVP]
                  > <http://www.mvps.org/dotnet>[/color]


                  Comment

                  • One Handed Man [ OHM# ]

                    #10
                    Re: creating a new instance of an object XXX type

                    I knew this would not be something simple, well done for finding it.

                    Regards - OHM


                    Herfried K. Wagner [MVP] wrote:[color=blue]
                    > * "jernej goricki" <ph-enom@obmocje51. net> scripsit:[color=green]
                    >> Is it possible to do this in vb.net :
                    >>
                    >> 1.)To make a function that would have a string parameter ClassName
                    >> 2.)This function would return an object of the same type as the
                    >> ClassName parameter was.[/color]
                    >
                    >[/color]
                    <http://groups.google.com/groups?selm...-208219.news.u
                    ni-berlin.de>

                    Regards - OHM# OneHandedMan{at }BTInternet{dot }com


                    Comment

                    • Blumidoo

                      #11
                      Re: creating a new instance of an object XXX type

                      >
                      <http://groups.google.com/groups?selm...-208219.news.u
                      ni-berlin.de>

                      What a beautiful way to make a total mess in the code... :-/
                      Not that I do not like the solution, but I don't like to promote bad
                      programming habits.

                      sincerely,
                      --
                      Sebastian Zaklada
                      Skilled Software

                      *************** *************** ******
                      SQL Source Control 2003 - for
                      SQL Server Source Safe integration
                      and custom databases documentation


                      Comment

                      • One Handed Man [ OHM# ]

                        #12
                        Re: creating a new instance of an object XXX type

                        I agree. The principal itself is poor.

                        Regards

                        Blumidoo wrote:[color=blue]
                        >[/color]
                        <http://groups.google.com/groups?selm...-208219.news.u[color=blue]
                        > ni-berlin.de>
                        >
                        > What a beautiful way to make a total mess in the code... :-/
                        > Not that I do not like the solution, but I don't like to promote bad
                        > programming habits.
                        >
                        > sincerely,[/color]

                        Regards - OHM# OneHandedMan{at }BTInternet{dot }com


                        Comment

                        • Herfried K. Wagner [MVP]

                          #13
                          Re: creating a new instance of an object XXX type

                          * "Blumidoo" <szaklada-dont-like-spam@skilledsof tware.com> scripsit:[color=blue]
                          > <http://groups.google.com/groups?selm...-208219.news.u
                          > ni-berlin.de>
                          >
                          > What a beautiful way to make a total mess in the code... :-/
                          > Not that I do not like the solution, but I don't like to promote bad
                          > programming habits.[/color]

                          Why should this code promote "bad programming habits"?

                          --
                          Herfried K. Wagner [MVP]
                          <http://www.mvps.org/dotnet>

                          Comment

                          • Herfried K. Wagner [MVP]

                            #14
                            Re: creating a new instance of an object XXX type

                            * "One Handed Man [ OHM# ]" <OneHandedMan{a t}BTInternet{do t}com> scripsit:[color=blue]
                            > I knew this would not be something simple, well done for finding it.[/color]

                            ;-)

                            It's easy for me to find messages I have written previously...

                            --
                            Herfried K. Wagner [MVP]
                            <http://www.mvps.org/dotnet>

                            Comment

                            • Blumidoo

                              #15
                              Re: creating a new instance of an object XXX type

                              > > What a beautiful way to make a total mess in the code... :-/[color=blue][color=green]
                              > > Not that I do not like the solution, but I don't like to promote bad
                              > > programming habits.[/color]
                              >
                              > Why should this code promote "bad programming habits"?[/color]

                              Not the code itself, I wrote I like it. But the idea is wrong - Jernej
                              should should rather design and implement an AbstractFactory design pattern
                              class, not create objects of any type whenever he wants and wherever in the
                              code. It is messy. Development is design, than design even more, and after
                              that you may code... And even than you will always end up refactoring what
                              you have written before... :-)

                              sincerely,
                              --
                              Sebastian Zaklada
                              Skilled Software

                              *************** *************** ******
                              SQL Source Control 2003 - for
                              SQL Server Source Safe integration
                              and custom databases documentation


                              Comment

                              Working...