473,383 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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(ByVal 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


Jun 27 '08 #1
9 3785
Public Class MyOwnFactory(Of T As {New, MyOwnBase})
"Simon Woods" <si*********@hotmail.comwrote in message
news:es**************@TK2MSFTNGP05.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(ByVal 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

Jun 27 '08 #2
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.Factory(Of MyOwnClasses.MyOwnClass) _
= MyOwnClasses.Factory(Of MyOwnClasses.MyOwnClass).Instance

(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" <si*********@hotmail.comwrote in message
news:es**************@TK2MSFTNGP05.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(ByVal 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

Jun 27 '08 #3
From generic code the only constraint available for creating instances is
New
"Simon Woods" <si*********@hotmail.comwrote in message
news:u1**************@TK2MSFTNGP04.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.Factory(Of MyOwnClasses.MyOwnClass) _
= MyOwnClasses.Factory(Of MyOwnClasses.MyOwnClass).Instance

(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" <si*********@hotmail.comwrote in message
news:es**************@TK2MSFTNGP05.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(ByVal 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

Jun 27 '08 #4
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.Reflection.BindingFlags.NonPublic Or _
'
System.Reflection.BindingFlags.Instance), T)

Bill McCarthy wrote:
From generic code the only constraint available for creating instances
is New
"Simon Woods" <si*********@hotmail.comwrote in message
news:u1**************@TK2MSFTNGP04.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.Factory(Of MyOwnClasses.MyOwnClass) _
= MyOwnClasses.Factory(Of MyOwnClasses.MyOwnClass).Instance

(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" <si*********@hotmail.comwrote in message
news:es**************@TK2MSFTNGP05.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(ByVal 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


Jun 27 '08 #5
The reflection call should work. You want to include the
System.Reflection.BindingFlags.CreateInstance flag
"Simon Woods" <si*********@hotmail.comwrote in message
news:ua**************@TK2MSFTNGP04.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.Reflection.BindingFlags.NonPublic Or _
' System.Reflection.BindingFlags.Instance), T)

Bill McCarthy wrote:
> From generic code the only constraint available for creating instances
is New
"Simon Woods" <si*********@hotmail.comwrote in message
news:u1**************@TK2MSFTNGP04.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.Factory(Of MyOwnClasses.MyOwnClass) _
= MyOwnClasses.Factory(Of MyOwnClasses.MyOwnClass).Instance

(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" <si*********@hotmail.comwrote in message
news:es**************@TK2MSFTNGP05.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(ByVal 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
>
>
>
>
Jun 27 '08 #6
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.Reflection.BindingFlags.NonPublic Or
_
System.Reflection.BindingFlags.CreateInstance), T)

but it's erroring with

System.MissingMethodException: Constructor on type
'MyOwnClasses.MyOwnClass' 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.Reflection.BindingFlags.CreateInstance flag
"Simon Woods" <si*********@hotmail.comwrote in message
news:ua**************@TK2MSFTNGP04.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.Reflection.BindingFlags.NonPublic Or _
' System.Reflection.BindingFlags.Instance), T)

Bill McCarthy wrote:
>> From generic code the only constraint available for creating
instances is New
"Simon Woods" <si*********@hotmail.comwrote in message
news:u1**************@TK2MSFTNGP04.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.Factory(Of MyOwnClasses.MyOwnClass) _
= MyOwnClasses.Factory(Of MyOwnClasses.MyOwnClass).Instance

(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" <si*********@hotmail.comwrote in message
news:es**************@TK2MSFTNGP05.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(ByVal 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
>>
>>
>>
>>
>
Jun 27 '08 #7
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
Jun 27 '08 #8
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" <si*********@hotmail.comwrote in message
news:e%****************@TK2MSFTNGP06.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.Reflection.BindingFlags.NonPublic Or _
System.Reflection.BindingFlags.CreateInstance), T)

but it's erroring with

System.MissingMethodException: Constructor on type
'MyOwnClasses.MyOwnClass' 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.Reflection.BindingFlags.CreateInstance flag
"Simon Woods" <si*********@hotmail.comwrote in message
news:ua**************@TK2MSFTNGP04.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.Reflection.BindingFlags.NonPublic Or _
' System.Reflection.BindingFlags.Instance), T)

Bill McCarthy wrote:
From generic code the only constraint available for creating instances
is New
"Simon Woods" <si*********@hotmail.comwrote in message
news:u1**************@TK2MSFTNGP04.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.Factory(Of MyOwnClasses.MyOwnClass) _
= MyOwnClasses.Factory(Of MyOwnClasses.MyOwnClass).Instance
>
(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" <si*********@hotmail.comwrote in message
>news:es**************@TK2MSFTNGP05.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(ByVal 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
>>>
>>>
>>>
>>>
>>
Jun 27 '08 #9
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!)
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

49
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another...
5
by: Gomaw Beoyr | last post by:
Hello Is there any explanation why Microsoft chose to implement arrays as objects allocated on the heap instead of structs allocated on the stack? For "mathematical stuff", one normally...
48
by: Andrew Quine | last post by:
Hi Just read this article http://www.artima.com/intv/choices.html. Towards the end of the dicussions, when asked "Did you consider including support for the concept of immutable directly in C#...
2
by: FredC | last post by:
S Name Microsoft Windows XP Professional Version 5.1.2600 Service Pack 2 Build 2600 Total Physical Memory 1,024.00 MB MDE 2003 Version 7.1.3008 ..NET Framework 1.1 Version 1.1.4322 SP1...
6
by: Gary Frank | last post by:
What are the ramifications if I were to instantiate an object tens of thousands of times and add them to an array? Or hundreds of thousands of times? Do you know if the act of instantiating a...
3
by: Nagesh | last post by:
hi, I have seen the winvnc(tightvnc server) source code in this I seen that class member funtions are calling without instantiating the object i.e. like vncService::ShowDefaultProperties() where...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
20
by: Keith Elder | last post by:
I ran into a unique situation today whereby I have a core library that uses generics to return users from Active Directory. Example: List<ADUser> users = ADUser.GetByName("First", "Last"); ...
2
by: Lapu-Lapu | last post by:
I have authored a web service using ASP 2.0. The web services return objects that use generics and that also contain circular references. Programmatically, everything works well, as long as you...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.