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