473,320 Members | 1,876 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,320 software developers and data experts.

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

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
Jan 12 '08 #1
5 6025
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" <si*******@heintze.comwrote in message
news:uh**************@TK2MSFTNGP06.phx.gbl...
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


Jan 12 '08 #2
Sorry, you don't need: Private arraySize As Integer

"Scott M." <sm**@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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" <si*******@heintze.comwrote in message
news:uh**************@TK2MSFTNGP06.phx.gbl...
>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



Jan 12 '08 #3
Thanks! That works! Can you explain the logic of using redim instead of new?
It seems new should work.
Siegfried
Jan 12 '08 #4
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
Jan 13 '08 #5
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" <si*******@heintze.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>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

Jan 13 '08 #6

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

Similar topics

13
by: Kai Grossjohann | last post by:
It seems that Ctrl-N in Mozilla opens a new empty browser window. That's fine, I don't need to do anything about it. But Ctrl-N in IE appears to clone the current window. Is there a way to...
822
by: Turamnvia Suouriviaskimatta | last post by:
I 'm following various posting in "comp.lang.ada, comp.lang.c++ , comp.realtime, comp.software-eng" groups regarding selection of a programming language of C, C++ or Ada for safety critical...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
3
by: Alex Shirshov | last post by:
Hi all! How can i specify, that the generic class have the constructor? With c# it's a new constraint. For instance, i write Class cls1(Of T) Public Sub some() Dim tt As New T() ' here is...
4
by: Lasse Vågsæther Karlsen | last post by:
Ok, I realize that it doesn't really matter because it simply isn't possible, but does anyone know the reason why the constraint on the generic parameters for new() doesn't allow parameters? Ie....
19
by: Brett Romero | last post by:
I'd like to do this: public class myclass<T> { myclass() {} public void CreateNew() { T myp = new T();
1
by: Adam Clauss | last post by:
So, we can use the new() constraint to specify that a given type must have a parameterless constructor. Why can't we say something like new(string) to specify that it must have a parameter...
3
by: =?Utf-8?B?Um9nZXIgTWFydGlu?= | last post by:
When using the Web Site Administration Tool, I get the following error when trying to delete a user in a web application I configured to use membership, roles, and profiles: "An error was...
9
by: Simon Woods | last post by:
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.