Instantiating new objects with Generics

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

    #1

    Instantiating new objects with Generics

    Hi

    I have a factory class which I want to use to create new objects which
    inherit a certain base type so I have a declaration

    Public Class MyOwnFactory(Of T As MyOwnBase)

    As I understand it, a new MyOwnFactory will get instantiated for every
    different type of class implementing MyOwnBase. (This is what I want
    anyway as the factory is holding a cache of previously created objects
    of this type)


    In the class, I have some methods :


    Public Function GetMyObject(ByV al p_key As String, ByVal p_reload As
    Boolean) As T

    ....

    Dim l_def As T = CreateMyObject( l_dto)

    ...

    Return l_def
    End Function

    Private Function CreateMyObject( ) As T

    Dim l_def As T = New T()

    If LoadCommonData( l_def, l_data) then

    endif

    Return l_def

    End Function

    I'm getting the error

    "'New' cannot be used on a type parameter that does not have a 'New'
    constraint." against the line

    Dim l_def As T = New T()

    Because it is a factory, I want to keep the New-ing local to the factory
    (so I don't want public constructors on the types inheriting MyOwnBase)
    and I wondering if someone could explain a way to do this or perhaps an
    alternative way of looking at this.

    Thx in advance

    Simon




  • Bill McCarthy

    #2
    Re: Instantiating new objects with Generics

    Public Class MyOwnFactory(Of T As {New, MyOwnBase})


    "Simon Woods" <simonjwoods@ho tmail.comwrote in message
    news:esbziBawIH A.3680@TK2MSFTN GP05.phx.gbl...
    Hi
    >
    I have a factory class which I want to use to create new objects which
    inherit a certain base type so I have a declaration
    >
    Public Class MyOwnFactory(Of T As MyOwnBase)
    >
    As I understand it, a new MyOwnFactory will get instantiated for every
    different type of class implementing MyOwnBase. (This is what I want
    anyway as the factory is holding a cache of previously created objects of
    this type)
    >
    >
    In the class, I have some methods :
    >
    >
    Public Function GetMyObject(ByV al p_key As String, ByVal p_reload As
    Boolean) As T
    >
    ....
    >
    Dim l_def As T = CreateMyObject( l_dto)
    >
    ...
    >
    Return l_def
    End Function
    >
    Private Function CreateMyObject( ) As T
    >
    Dim l_def As T = New T()
    >
    If LoadCommonData( l_def, l_data) then
    >
    endif
    >
    Return l_def
    >
    End Function
    >
    I'm getting the error
    >
    "'New' cannot be used on a type parameter that does not have a 'New'
    constraint." against the line
    >
    Dim l_def As T = New T()
    >
    Because it is a factory, I want to keep the New-ing local to the factory
    (so I don't want public constructors on the types inheriting MyOwnBase)
    and I wondering if someone could explain a way to do this or perhaps an
    alternative way of looking at this.
    >
    Thx in advance
    >
    Simon
    >
    >
    >
    >

    Comment

    • Simon Woods

      #3
      Re: Instantiating new objects with Generics

      Thx Bill

      When I introduce the New constraint, an error is generated

      "'MyOwnClasses. MyOwnClass' must have a public parameterless instance
      constructor to satisfy the 'New' constraint for type parameter 'T'."

      where the factory is instantiated with

      Dim l_factory As MyOwnClasses.Fa ctory(Of MyOwnClasses.My OwnClass) _
      = MyOwnClasses.Fa ctory(Of MyOwnClasses.My OwnClass).Insta nce

      (It's a singleton btw)

      I'm not too sure how to get round this, as it seems that it is looking
      for a public New constructor which I don't want to supply because I want
      the factory to be doing all the newing

      Thanks again

      S


      Bill McCarthy wrote:
      Public Class MyOwnFactory(Of T As {New, MyOwnBase})
      >
      >
      "Simon Woods" <simonjwoods@ho tmail.comwrote in message
      news:esbziBawIH A.3680@TK2MSFTN GP05.phx.gbl...
      >Hi
      >>
      >I have a factory class which I want to use to create new objects which
      >inherit a certain base type so I have a declaration
      >>
      > Public Class MyOwnFactory(Of T As MyOwnBase)
      >>
      >As I understand it, a new MyOwnFactory will get instantiated for every
      >different type of class implementing MyOwnBase. (This is what I want
      >anyway as the factory is holding a cache of previously created objects
      >of this type)
      >>
      >>
      >In the class, I have some methods :
      >>
      >>
      > Public Function GetMyObject(ByV al p_key As String, ByVal p_reload As
      >Boolean) As T
      >>
      > ....
      >>
      > Dim l_def As T = CreateMyObject( l_dto)
      >>
      > ...
      >>
      > Return l_def
      > End Function
      >>
      > Private Function CreateMyObject( ) As T
      >>
      > Dim l_def As T = New T()
      >>
      > If LoadCommonData( l_def, l_data) then
      >>
      > endif
      >>
      > Return l_def
      >>
      > End Function
      >>
      >I'm getting the error
      >>
      >"'New' cannot be used on a type parameter that does not have a 'New'
      >constraint." against the line
      >>
      > Dim l_def As T = New T()
      >>
      >Because it is a factory, I want to keep the New-ing local to the
      >factory (so I don't want public constructors on the types inheriting
      >MyOwnBase) and I wondering if someone could explain a way to do this
      >or perhaps an alternative way of looking at this.
      >>
      >Thx in advance
      >>
      >Simon
      >>
      >>
      >>
      >>
      >

      Comment

      • Bill McCarthy

        #4
        Re: Instantiating new objects with Generics

        From generic code the only constraint available for creating instances is
        New


        "Simon Woods" <simonjwoods@ho tmail.comwrote in message
        news:u1lPCgawIH A.2188@TK2MSFTN GP04.phx.gbl...
        Thx Bill
        >
        When I introduce the New constraint, an error is generated
        >
        "'MyOwnClasses. MyOwnClass' must have a public parameterless instance
        constructor to satisfy the 'New' constraint for type parameter 'T'."
        where the factory is instantiated with
        >
        Dim l_factory As MyOwnClasses.Fa ctory(Of MyOwnClasses.My OwnClass) _
        = MyOwnClasses.Fa ctory(Of MyOwnClasses.My OwnClass).Insta nce
        >
        (It's a singleton btw)
        >
        I'm not too sure how to get round this, as it seems that it is looking for
        a public New constructor which I don't want to supply because I want the
        factory to be doing all the newing
        >
        Thanks again
        >
        S
        >
        >
        Bill McCarthy wrote:
        >Public Class MyOwnFactory(Of T As {New, MyOwnBase})
        >>
        >>
        >"Simon Woods" <simonjwoods@ho tmail.comwrote in message
        >news:esbziBawI HA.3680@TK2MSFT NGP05.phx.gbl.. .
        >>Hi
        >>>
        >>I have a factory class which I want to use to create new objects which
        >>inherit a certain base type so I have a declaration
        >>>
        >> Public Class MyOwnFactory(Of T As MyOwnBase)
        >>>
        >>As I understand it, a new MyOwnFactory will get instantiated for every
        >>different type of class implementing MyOwnBase. (This is what I want
        >>anyway as the factory is holding a cache of previously created objects
        >>of this type)
        >>>
        >>>
        >>In the class, I have some methods :
        >>>
        >>>
        >> Public Function GetMyObject(ByV al p_key As String, ByVal p_reload As
        >>Boolean) As T
        >>>
        >> ....
        >>>
        >> Dim l_def As T = CreateMyObject( l_dto)
        >>>
        >> ...
        >>>
        >> Return l_def
        >> End Function
        >>>
        >> Private Function CreateMyObject( ) As T
        >>>
        >> Dim l_def As T = New T()
        >>>
        >> If LoadCommonData( l_def, l_data) then
        >>>
        >> endif
        >>>
        >> Return l_def
        >>>
        >> End Function
        >>>
        >>I'm getting the error
        >>>
        >>"'New' cannot be used on a type parameter that does not have a 'New'
        >>constraint. " against the line
        >>>
        >> Dim l_def As T = New T()
        >>>
        >>Because it is a factory, I want to keep the New-ing local to the factory
        >>(so I don't want public constructors on the types inheriting MyOwnBase)
        >>and I wondering if someone could explain a way to do this or perhaps an
        >>alternative way of looking at this.
        >>>
        >>Thx in advance
        >>>
        >>Simon
        >>>
        >>>
        >>>
        >>>
        >>

        Comment

        • Simon Woods

          #5
          Re: Instantiating new objects with Generics

          So is it a case of using Reflection to lookup the type of T and create
          an object of that type and return it as a T ... or will I suffer from
          the same problem that Reflection also needs a public constructor.

          I have been looking at this approach, but have encountered a similar
          problem (but this time at runtime presumably because of its latebound
          nature) when trying to use the following - ...

          Dim l_myOwnClass As T = CType(Activator .CreateInstance (GetType(T), _
          '
          System.Reflecti on.BindingFlags .NonPublic Or _
          '
          System.Reflecti on.BindingFlags .Instance), T)



          Bill McCarthy wrote:
          From generic code the only constraint available for creating instances
          is New
          >
          >
          "Simon Woods" <simonjwoods@ho tmail.comwrote in message
          news:u1lPCgawIH A.2188@TK2MSFTN GP04.phx.gbl...
          >Thx Bill
          >>
          >When I introduce the New constraint, an error is generated
          >>
          >"'MyOwnClasses .MyOwnClass' must have a public parameterless instance
          >constructor to satisfy the 'New' constraint for type parameter 'T'."
          >where the factory is instantiated with
          >>
          >Dim l_factory As MyOwnClasses.Fa ctory(Of MyOwnClasses.My OwnClass) _
          > = MyOwnClasses.Fa ctory(Of MyOwnClasses.My OwnClass).Insta nce
          >>
          >(It's a singleton btw)
          >>
          >I'm not too sure how to get round this, as it seems that it is looking
          >for a public New constructor which I don't want to supply because I
          >want the factory to be doing all the newing
          >>
          >Thanks again
          >>
          >S
          >>
          >>
          >Bill McCarthy wrote:
          >>Public Class MyOwnFactory(Of T As {New, MyOwnBase})
          >>>
          >>>
          >>"Simon Woods" <simonjwoods@ho tmail.comwrote in message
          >>news:esbziBaw IHA.3680@TK2MSF TNGP05.phx.gbl. ..
          >>>Hi
          >>>>
          >>>I have a factory class which I want to use to create new objects
          >>>which inherit a certain base type so I have a declaration
          >>>>
          >>> Public Class MyOwnFactory(Of T As MyOwnBase)
          >>>>
          >>>As I understand it, a new MyOwnFactory will get instantiated for
          >>>every different type of class implementing MyOwnBase. (This is what
          >>>I want anyway as the factory is holding a cache of previously
          >>>created objects of this type)
          >>>>
          >>>>
          >>>In the class, I have some methods :
          >>>>
          >>>>
          >>> Public Function GetMyObject(ByV al p_key As String, ByVal p_reload
          >>>As Boolean) As T
          >>>>
          >>> ....
          >>>>
          >>> Dim l_def As T = CreateMyObject( l_dto)
          >>>>
          >>> ...
          >>>>
          >>> Return l_def
          >>> End Function
          >>>>
          >>> Private Function CreateMyObject( ) As T
          >>>>
          >>> Dim l_def As T = New T()
          >>>>
          >>> If LoadCommonData( l_def, l_data) then
          >>>>
          >>> endif
          >>>>
          >>> Return l_def
          >>>>
          >>> End Function
          >>>>
          >>>I'm getting the error
          >>>>
          >>>"'New' cannot be used on a type parameter that does not have a 'New'
          >>>constraint ." against the line
          >>>>
          >>> Dim l_def As T = New T()
          >>>>
          >>>Because it is a factory, I want to keep the New-ing local to the
          >>>factory (so I don't want public constructors on the types inheriting
          >>>MyOwnBase) and I wondering if someone could explain a way to do this
          >>>or perhaps an alternative way of looking at this.
          >>>>
          >>>Thx in advance
          >>>>
          >>>Simon
          >>>>
          >>>>
          >>>>
          >>>>
          >>>
          >

          Comment

          • Bill McCarthy

            #6
            Re: Instantiating new objects with Generics

            The reflection call should work. You want to include the
            System.Reflecti on.BindingFlags .CreateInstance flag


            "Simon Woods" <simonjwoods@ho tmail.comwrote in message
            news:uao76xawIH A.4488@TK2MSFTN GP04.phx.gbl...
            So is it a case of using Reflection to lookup the type of T and create an
            object of that type and return it as a T ... or will I suffer from the
            same problem that Reflection also needs a public constructor.
            >
            I have been looking at this approach, but have encountered a similar
            problem (but this time at runtime presumably because of its latebound
            nature) when trying to use the following - ...
            >
            Dim l_myOwnClass As T = CType(Activator .CreateInstance (GetType(T), _
            ' System.Reflecti on.BindingFlags .NonPublic Or _
            ' System.Reflecti on.BindingFlags .Instance), T)
            >
            >
            >
            Bill McCarthy wrote:
            > From generic code the only constraint available for creating instances
            >is New
            >>
            >>
            >"Simon Woods" <simonjwoods@ho tmail.comwrote in message
            >news:u1lPCgawI HA.2188@TK2MSFT NGP04.phx.gbl.. .
            >>Thx Bill
            >>>
            >>When I introduce the New constraint, an error is generated
            >>>
            >>"'MyOwnClasse s.MyOwnClass' must have a public parameterless instance
            >>constructor to satisfy the 'New' constraint for type parameter 'T'."
            >>where the factory is instantiated with
            >>>
            >>Dim l_factory As MyOwnClasses.Fa ctory(Of MyOwnClasses.My OwnClass) _
            >> = MyOwnClasses.Fa ctory(Of MyOwnClasses.My OwnClass).Insta nce
            >>>
            >>(It's a singleton btw)
            >>>
            >>I'm not too sure how to get round this, as it seems that it is looking
            >>for a public New constructor which I don't want to supply because I want
            >>the factory to be doing all the newing
            >>>
            >>Thanks again
            >>>
            >>S
            >>>
            >>>
            >>Bill McCarthy wrote:
            >>>Public Class MyOwnFactory(Of T As {New, MyOwnBase})
            >>>>
            >>>>
            >>>"Simon Woods" <simonjwoods@ho tmail.comwrote in message
            >>>news:esbziBa wIHA.3680@TK2MS FTNGP05.phx.gbl ...
            >>>>Hi
            >>>>>
            >>>>I have a factory class which I want to use to create new objects which
            >>>>inherit a certain base type so I have a declaration
            >>>>>
            >>>> Public Class MyOwnFactory(Of T As MyOwnBase)
            >>>>>
            >>>>As I understand it, a new MyOwnFactory will get instantiated for every
            >>>>different type of class implementing MyOwnBase. (This is what I want
            >>>>anyway as the factory is holding a cache of previously created objects
            >>>>of this type)
            >>>>>
            >>>>>
            >>>>In the class, I have some methods :
            >>>>>
            >>>>>
            >>>> Public Function GetMyObject(ByV al p_key As String, ByVal p_reload As
            >>>>Boolean) As T
            >>>>>
            >>>> ....
            >>>>>
            >>>> Dim l_def As T = CreateMyObject( l_dto)
            >>>>>
            >>>> ...
            >>>>>
            >>>> Return l_def
            >>>> End Function
            >>>>>
            >>>> Private Function CreateMyObject( ) As T
            >>>>>
            >>>> Dim l_def As T = New T()
            >>>>>
            >>>> If LoadCommonData( l_def, l_data) then
            >>>>>
            >>>> endif
            >>>>>
            >>>> Return l_def
            >>>>>
            >>>> End Function
            >>>>>
            >>>>I'm getting the error
            >>>>>
            >>>>"'New' cannot be used on a type parameter that does not have a 'New'
            >>>>constraint. " against the line
            >>>>>
            >>>> Dim l_def As T = New T()
            >>>>>
            >>>>Because it is a factory, I want to keep the New-ing local to the
            >>>>factory (so I don't want public constructors on the types inheriting
            >>>>MyOwnBase ) and I wondering if someone could explain a way to do this
            >>>>or perhaps an alternative way of looking at this.
            >>>>>
            >>>>Thx in advance
            >>>>>
            >>>>Simon
            >>>>>
            >>>>>
            >>>>>
            >>>>>
            >>>>
            >>

            Comment

            • Simon Woods

              #7
              Re: Instantiating new objects with Generics

              Ok ... thx very much Bill

              So I've tried this cmd with a protected, private and friend constructor
              on MyOwnClass

              Return CType(Activator .CreateInstance (GetType(T), _
              System.Reflecti on.BindingFlags .NonPublic Or
              _
              System.Reflecti on.BindingFlags .CreateInstance ), T)

              but it's erroring with

              System.MissingM ethodException: Constructor on type
              'MyOwnClasses.M yOwnClass' not found..

              Any other suggestions ? I also tried putting an explicit constructor on
              MyOwnBase Because MyOwnClass is inheriting it but got the same error

              thx again



              Bill McCarthy wrote:
              The reflection call should work. You want to include the
              System.Reflecti on.BindingFlags .CreateInstance flag
              >
              >
              "Simon Woods" <simonjwoods@ho tmail.comwrote in message
              news:uao76xawIH A.4488@TK2MSFTN GP04.phx.gbl...
              >So is it a case of using Reflection to lookup the type of T and create
              >an object of that type and return it as a T ... or will I suffer from
              >the same problem that Reflection also needs a public constructor.
              >>
              >I have been looking at this approach, but have encountered a similar
              >problem (but this time at runtime presumably because of its latebound
              >nature) when trying to use the following - ...
              >>
              >Dim l_myOwnClass As T = CType(Activator .CreateInstance (GetType(T), _
              > ' System.Reflecti on.BindingFlags .NonPublic Or _
              > ' System.Reflecti on.BindingFlags .Instance), T)
              >>
              >>
              >>
              >Bill McCarthy wrote:
              >> From generic code the only constraint available for creating
              >>instances is New
              >>>
              >>>
              >>"Simon Woods" <simonjwoods@ho tmail.comwrote in message
              >>news:u1lPCgaw IHA.2188@TK2MSF TNGP04.phx.gbl. ..
              >>>Thx Bill
              >>>>
              >>>When I introduce the New constraint, an error is generated
              >>>>
              >>>"'MyOwnClass es.MyOwnClass' must have a public parameterless instance
              >>>constructo r to satisfy the 'New' constraint for type parameter 'T'."
              >>>where the factory is instantiated with
              >>>>
              >>>Dim l_factory As MyOwnClasses.Fa ctory(Of MyOwnClasses.My OwnClass) _
              >>> = MyOwnClasses.Fa ctory(Of MyOwnClasses.My OwnClass).Insta nce
              >>>>
              >>>(It's a singleton btw)
              >>>>
              >>>I'm not too sure how to get round this, as it seems that it is
              >>>looking for a public New constructor which I don't want to supply
              >>>because I want the factory to be doing all the newing
              >>>>
              >>>Thanks again
              >>>>
              >>>S
              >>>>
              >>>>
              >>>Bill McCarthy wrote:
              >>>>Public Class MyOwnFactory(Of T As {New, MyOwnBase})
              >>>>>
              >>>>>
              >>>>"Simon Woods" <simonjwoods@ho tmail.comwrote in message
              >>>>news:esbziB awIHA.3680@TK2M SFTNGP05.phx.gb l...
              >>>>>Hi
              >>>>>>
              >>>>>I have a factory class which I want to use to create new objects
              >>>>>which inherit a certain base type so I have a declaration
              >>>>>>
              >>>>> Public Class MyOwnFactory(Of T As MyOwnBase)
              >>>>>>
              >>>>>As I understand it, a new MyOwnFactory will get instantiated for
              >>>>>every different type of class implementing MyOwnBase. (This is
              >>>>>what I want anyway as the factory is holding a cache of previously
              >>>>>created objects of this type)
              >>>>>>
              >>>>>>
              >>>>>In the class, I have some methods :
              >>>>>>
              >>>>>>
              >>>>> Public Function GetMyObject(ByV al p_key As String, ByVal
              >>>>>p_reload As Boolean) As T
              >>>>>>
              >>>>> ....
              >>>>>>
              >>>>> Dim l_def As T = CreateMyObject( l_dto)
              >>>>>>
              >>>>> ...
              >>>>>>
              >>>>> Return l_def
              >>>>> End Function
              >>>>>>
              >>>>> Private Function CreateMyObject( ) As T
              >>>>>>
              >>>>> Dim l_def As T = New T()
              >>>>>>
              >>>>> If LoadCommonData( l_def, l_data) then
              >>>>>>
              >>>>> endif
              >>>>>>
              >>>>> Return l_def
              >>>>>>
              >>>>> End Function
              >>>>>>
              >>>>>I'm getting the error
              >>>>>>
              >>>>>"'New' cannot be used on a type parameter that does not have a
              >>>>>'New' constraint." against the line
              >>>>>>
              >>>>> Dim l_def As T = New T()
              >>>>>>
              >>>>>Because it is a factory, I want to keep the New-ing local to the
              >>>>>factory (so I don't want public constructors on the types
              >>>>>inheriti ng MyOwnBase) and I wondering if someone could explain a
              >>>>>way to do this or perhaps an alternative way of looking at this.
              >>>>>>
              >>>>>Thx in advance
              >>>>>>
              >>>>>Simon
              >>>>>>
              >>>>>>
              >>>>>>
              >>>>>>
              >>>>>
              >>>
              >

              Comment

              • Simon Woods

                #8
                Re: Instantiating new objects with Generics

                Simon Woods wrote:
                Ok ... thx very much Bill
                Actually Bill, ... I'm not going with the Reflection approach because it
                seems noticably slower even to report an error compare to the New T()
                approach ... so I'll have a rethink about how to re-engineer this so I
                get all early binding.

                thx very much again...

                Simon

                Comment

                • Bill McCarthy

                  #9
                  Re: Instantiating new objects with Generics

                  Right as it should. There is no overload that matches that .

                  CType(Activator .CreateInstance (GetType(T), CreateInstance Or [Public] Or
                  Instance, Nothing, Nothing, Nothing), T)


                  "Simon Woods" <simonjwoods@ho tmail.comwrote in message
                  news:e%23vgyEbw IHA.4376@TK2MSF TNGP06.phx.gbl. ..
                  Ok ... thx very much Bill
                  >
                  So I've tried this cmd with a protected, private and friend constructor on
                  MyOwnClass
                  >
                  Return CType(Activator .CreateInstance (GetType(T), _
                  System.Reflecti on.BindingFlags .NonPublic Or _
                  System.Reflecti on.BindingFlags .CreateInstance ), T)
                  >
                  but it's erroring with
                  >
                  System.MissingM ethodException: Constructor on type
                  'MyOwnClasses.M yOwnClass' not found..
                  Any other suggestions ? I also tried putting an explicit constructor on
                  MyOwnBase Because MyOwnClass is inheriting it but got the same error
                  >
                  thx again
                  >
                  >
                  >
                  Bill McCarthy wrote:
                  >The reflection call should work. You want to include the
                  >System.Reflect ion.BindingFlag s.CreateInstanc e flag
                  >>
                  >>
                  >"Simon Woods" <simonjwoods@ho tmail.comwrote in message
                  >news:uao76xawI HA.4488@TK2MSFT NGP04.phx.gbl.. .
                  >>So is it a case of using Reflection to lookup the type of T and create
                  >>an object of that type and return it as a T ... or will I suffer from
                  >>the same problem that Reflection also needs a public constructor.
                  >>>
                  >>I have been looking at this approach, but have encountered a similar
                  >>problem (but this time at runtime presumably because of its latebound
                  >>nature) when trying to use the following - ...
                  >>>
                  >>Dim l_myOwnClass As T = CType(Activator .CreateInstance (GetType(T), _
                  >> ' System.Reflecti on.BindingFlags .NonPublic Or _
                  >> ' System.Reflecti on.BindingFlags .Instance), T)
                  >>>
                  >>>
                  >>>
                  >>Bill McCarthy wrote:
                  >>> From generic code the only constraint available for creating instances
                  >>>is New
                  >>>>
                  >>>>
                  >>>"Simon Woods" <simonjwoods@ho tmail.comwrote in message
                  >>>news:u1lPCga wIHA.2188@TK2MS FTNGP04.phx.gbl ...
                  >>>>Thx Bill
                  >>>>>
                  >>>>When I introduce the New constraint, an error is generated
                  >>>>>
                  >>>>"'MyOwnClas ses.MyOwnClass' must have a public parameterless instance
                  >>>>construct or to satisfy the 'New' constraint for type parameter 'T'."
                  >>>>where the factory is instantiated with
                  >>>>>
                  >>>>Dim l_factory As MyOwnClasses.Fa ctory(Of MyOwnClasses.My OwnClass) _
                  >>>> = MyOwnClasses.Fa ctory(Of MyOwnClasses.My OwnClass).Insta nce
                  >>>>>
                  >>>>(It's a singleton btw)
                  >>>>>
                  >>>>I'm not too sure how to get round this, as it seems that it is looking
                  >>>>for a public New constructor which I don't want to supply because I
                  >>>>want the factory to be doing all the newing
                  >>>>>
                  >>>>Thanks again
                  >>>>>
                  >>>>S
                  >>>>>
                  >>>>>
                  >>>>Bill McCarthy wrote:
                  >>>>>Public Class MyOwnFactory(Of T As {New, MyOwnBase})
                  >>>>>>
                  >>>>>>
                  >>>>>"Simon Woods" <simonjwoods@ho tmail.comwrote in message
                  >>>>>news:esbzi BawIHA.3680@TK2 MSFTNGP05.phx.g bl...
                  >>>>>>Hi
                  >>>>>>>
                  >>>>>>I have a factory class which I want to use to create new objects
                  >>>>>>which inherit a certain base type so I have a declaration
                  >>>>>>>
                  >>>>>> Public Class MyOwnFactory(Of T As MyOwnBase)
                  >>>>>>>
                  >>>>>>As I understand it, a new MyOwnFactory will get instantiated for
                  >>>>>>every different type of class implementing MyOwnBase. (This is what
                  >>>>>>I want anyway as the factory is holding a cache of previously
                  >>>>>>created objects of this type)
                  >>>>>>>
                  >>>>>>>
                  >>>>>>In the class, I have some methods :
                  >>>>>>>
                  >>>>>>>
                  >>>>>> Public Function GetMyObject(ByV al p_key As String, ByVal p_reload
                  >>>>>>As Boolean) As T
                  >>>>>>>
                  >>>>>> ....
                  >>>>>>>
                  >>>>>> Dim l_def As T = CreateMyObject( l_dto)
                  >>>>>>>
                  >>>>>> ...
                  >>>>>>>
                  >>>>>> Return l_def
                  >>>>>> End Function
                  >>>>>>>
                  >>>>>> Private Function CreateMyObject( ) As T
                  >>>>>>>
                  >>>>>> Dim l_def As T = New T()
                  >>>>>>>
                  >>>>>> If LoadCommonData( l_def, l_data) then
                  >>>>>>>
                  >>>>>> endif
                  >>>>>>>
                  >>>>>> Return l_def
                  >>>>>>>
                  >>>>>> End Function
                  >>>>>>>
                  >>>>>>I'm getting the error
                  >>>>>>>
                  >>>>>>"'New' cannot be used on a type parameter that does not have a 'New'
                  >>>>>>constrain t." against the line
                  >>>>>>>
                  >>>>>> Dim l_def As T = New T()
                  >>>>>>>
                  >>>>>>Because it is a factory, I want to keep the New-ing local to the
                  >>>>>>factory (so I don't want public constructors on the types inheriting
                  >>>>>>MyOwnBase ) and I wondering if someone could explain a way to do this
                  >>>>>>or perhaps an alternative way of looking at this.
                  >>>>>>>
                  >>>>>>Thx in advance
                  >>>>>>>
                  >>>>>>Simon
                  >>>>>>>
                  >>>>>>>
                  >>>>>>>
                  >>>>>>>
                  >>>>>>
                  >>>>
                  >>

                  Comment

                  • Simon Woods

                    #10
                    Re: Instantiating new objects with Generics

                    Bill McCarthy wrote:
                    Right as it should. There is no overload that matches that .
                    >
                    CType(Activator .CreateInstance (GetType(T), CreateInstance Or [Public] Or
                    Instance, Nothing, Nothing, Nothing), T)
                    That did it ... thx (I was in a vb6 mode thinking the params were optional!)


                    Comment

                    Working...