Connecting Tech Pros Worldwide Forums | Help | Site Map

'New' cannot be used on a type parameter that does not have a 'New' constraint

Siegfried Heintze
Guest
 
Posts: n/a
#1: Jan 12 '08
On line 5 of the program below I get the error message in the subject line.
(1) How may I correct it?
(2) How may I enhance my Arr class to work with a foreach statement?
(3) In C++ I can pass integer constants (like 1990) as template (generic)
parameters. Can I do that in VB.NET or must I use the constructor for that?

Thanks,
Siegfried





Module Module1
Class Arr(Of T)
Private data() As T
Public Sub New(ByVal n As Integer)
data = New T(n)
End Sub
Default Public Property Item(ByVal index As Integer) As T
Get
Return data(index - 1990)
End Get
Set(ByVal Value As T)
data(index - 1990) = Value
End Set
End Property
End Class
Declare Function _getch Lib "msvcrt.dll" () As Integer
Sub Main(ByVal args() As String)
Dim out As System.IO.TextWriter = System.Console.Out
Dim arr1 As Arr(Of Double) = New Arr(Of Double)(3)
Dim arr2 As Arr(Of String) = New Arr(Of String)(3)
arr1(1990) = 1.3
arr1(1991) = 4.5
arr1(1992) = 3
arr2(1990) = "A"
arr2(1991) = "B"
arr2(1992) = "C"
Dim ii As Integer
For ii = 1990 To 1992
out.WriteLine(arr1(ii) + ":" + arr2(ii))
Next
out.Write("hit return to continue: ")
_getch()
End Sub
End Module



Scott M.
Guest
 
Posts: n/a
#2: Jan 12 '08

re: 'New' cannot be used on a type parameter that does not have a 'New' constraint


It looks like you are using generics inappropriately here. From what I can
tell, you are using generics to build arrays of sizes that will be known
later. Because the Doubld type does not have a constructor, yet you are
trying to make a new instance of a Double array (T(n)), you get the error.

Try this:

Private data As T()
Private arraySize As Integer

Public Sub New(ByVal n As Integer)
ReDim data(n)
End Sub


To be able to use For Each, the class must implement the IEnumerable
interface.

I can't help you with your C++ question.



"Siegfried Heintze" <siegfried@heintze.comwrote in message
news:uhMGlOWVIHA.4440@TK2MSFTNGP06.phx.gbl...
Quote:
On line 5 of the program below I get the error message in the subject
line.
(1) How may I correct it?
(2) How may I enhance my Arr class to work with a foreach statement?
(3) In C++ I can pass integer constants (like 1990) as template (generic)
parameters. Can I do that in VB.NET or must I use the constructor for
that?
>
Thanks,
Siegfried
>
>
>
>
>
Module Module1
Class Arr(Of T)
Private data() As T
Public Sub New(ByVal n As Integer)
data = New T(n)
End Sub
Default Public Property Item(ByVal index As Integer) As T
Get
Return data(index - 1990)
End Get
Set(ByVal Value As T)
data(index - 1990) = Value
End Set
End Property
End Class
Declare Function _getch Lib "msvcrt.dll" () As Integer
Sub Main(ByVal args() As String)
Dim out As System.IO.TextWriter = System.Console.Out
Dim arr1 As Arr(Of Double) = New Arr(Of Double)(3)
Dim arr2 As Arr(Of String) = New Arr(Of String)(3)
arr1(1990) = 1.3
arr1(1991) = 4.5
arr1(1992) = 3
arr2(1990) = "A"
arr2(1991) = "B"
arr2(1992) = "C"
Dim ii As Integer
For ii = 1990 To 1992
out.WriteLine(arr1(ii) + ":" + arr2(ii))
Next
out.Write("hit return to continue: ")
_getch()
End Sub
End Module
>
>

Scott M.
Guest
 
Posts: n/a
#3: Jan 12 '08

re: 'New' cannot be used on a type parameter that does not have a 'New' constraint


Sorry, you don't need: Private arraySize As Integer



"Scott M." <smar@nospam.nospamwrote in message
news:%23lhvgaWVIHA.3400@TK2MSFTNGP03.phx.gbl...
Quote:
It looks like you are using generics inappropriately here. From what I can
tell, you are using generics to build arrays of sizes that will be known
later. Because the Doubld type does not have a constructor, yet you are
trying to make a new instance of a Double array (T(n)), you get the error.
>
Try this:
>
Private data As T()
Private arraySize As Integer
>
Public Sub New(ByVal n As Integer)
ReDim data(n)
End Sub
>
>
To be able to use For Each, the class must implement the IEnumerable
interface.
>
I can't help you with your C++ question.
>
>
>
"Siegfried Heintze" <siegfried@heintze.comwrote in message
news:uhMGlOWVIHA.4440@TK2MSFTNGP06.phx.gbl...
Quote:
>On line 5 of the program below I get the error message in the subject
>line.
>(1) How may I correct it?
>(2) How may I enhance my Arr class to work with a foreach statement?
>(3) In C++ I can pass integer constants (like 1990) as template (generic)
>parameters. Can I do that in VB.NET or must I use the constructor for
>that?
>>
>Thanks,
>Siegfried
>>
>>
>>
>>
>>
>Module Module1
> Class Arr(Of T)
> Private data() As T
> Public Sub New(ByVal n As Integer)
> data = New T(n)
> End Sub
> Default Public Property Item(ByVal index As Integer) As T
> Get
> Return data(index - 1990)
> End Get
> Set(ByVal Value As T)
> data(index - 1990) = Value
> End Set
> End Property
> End Class
> Declare Function _getch Lib "msvcrt.dll" () As Integer
> Sub Main(ByVal args() As String)
> Dim out As System.IO.TextWriter = System.Console.Out
> Dim arr1 As Arr(Of Double) = New Arr(Of Double)(3)
> Dim arr2 As Arr(Of String) = New Arr(Of String)(3)
> arr1(1990) = 1.3
> arr1(1991) = 4.5
> arr1(1992) = 3
> arr2(1990) = "A"
> arr2(1991) = "B"
> arr2(1992) = "C"
> Dim ii As Integer
> For ii = 1990 To 1992
> out.WriteLine(arr1(ii) + ":" + arr2(ii))
> Next
> out.Write("hit return to continue: ")
> _getch()
> End Sub
>End Module
>>
>>
>
>

Siegfried Heintze
Guest
 
Posts: n/a
#4: Jan 12 '08

re: 'New' cannot be used on a type parameter that does not have a 'New' constraint


Thanks! That works! Can you explain the logic of using redim instead of new?
It seems new should work.
Siegfried


Siegfried Heintze
Guest
 
Posts: n/a
#5: Jan 13 '08

re: 'New' cannot be used on a type parameter that does not have a 'New' constraint


Since you know you are going to want an array of type T, you declare it as
Quote:
follows:
>
Dim myArray As T()
>
The parenthesis indicate that it's not just a T you want to declare, but
an array of T. But, at the time you declare T(), you don't know the size
of the array you'll need.
So what you are saying is that when I declare an array I also allocate one
(whether I wanted to allocate one or not -- unlike C#)?

Thanks,
Siegfried


Scott M.
Guest
 
Posts: n/a
#6: Jan 13 '08

re: 'New' cannot be used on a type parameter that does not have a 'New' constraint


You allocate the memory address for one, but without specifying an array
size, you need to re-size the memory later when the correct size is known.


"Siegfried Heintze" <siegfried@heintze.comwrote in message
news:%2350MuKhVIHA.5448@TK2MSFTNGP04.phx.gbl...
Quote:
Quote:
>Since you know you are going to want an array of type T, you declare it
>as follows:
>>
> Dim myArray As T()
>>
>The parenthesis indicate that it's not just a T you want to declare, but
>an array of T. But, at the time you declare T(), you don't know the size
>of the array you'll need.
>
So what you are saying is that when I declare an array I also allocate one
(whether I wanted to allocate one or not -- unlike C#)?
>
Thanks,
Siegfried
>

Closed Thread


Similar Visual Basic .NET bytes