Using the Of keyword with the VB.NET Collection class

  • Thread starter Thread starter Nathan Sokalski
  • Start date Start date
N

Nathan Sokalski

I was trying to use a code sample from a book I bought (.NET Gotchas by
O'Reilly, author = Venkat Subramaniam). The line of code I am trying to use
is the following:

Dim coll As New Collection(Of Double)

However, when I enter this line of code, I recieve the following error:

'Microsoft.VisualBasic.Collection' has no type parameters and so cannot have
type arguments. If I use the following line of code:

Dim coll As New System.Collections.Generic.List(Of Double)

I do not recieve any errors. It seams like the book it trying to tell me to
use the Of keyword with the Collection class when it does not have a
constructor that it can be used with. Is there an error in the book? If not,
what am I doing wrong? I am using Visual Studio .NET 2005 with .NET 2.0.
Thanks.
 
Does the book say whether it's for .Net 1.1 or .Net 2.0? It could be .Net
1.1, because that book came out 5 or 6 months before .Net 2.0 was released
in production (Dec 2005).

Robin S.
 
Nathan said:
I was trying to use a code sample from a book I bought (.NET Gotchas by
O'Reilly, author = Venkat Subramaniam). The line of code I am trying to use
is the following:

Dim coll As New Collection(Of Double)

However, when I enter this line of code, I recieve the following error:

'Microsoft.VisualBasic.Collection' has no type parameters and so cannot have
type arguments. If I use the following line of code:

Dim coll As New System.Collections.Generic.List(Of Double)

I do not recieve any errors.
<snip>

The book is probably assuming that the namespace
System.Collections.ObjectModel is in scope, for that namespace exposes
a generic Collection. To use the example as-is you'd had to place an
"Imports System.Collections.ObjectModel" at the top of the file where
your code lives. Or -- my preferred approach -- create an alias for
the said namespace:

<aircode>
'At the file level
Imports SysCols = System.Collections.ObjectModel

'...
'at declaration scope:
Dim coll As New SysCols.Collection(Of Double)
</aircode>

HTH.

Regards,

Branco.
 
imports System.Collection.Generic

sub main()
dim typedcollection as new Collection(of Double)

end sub


The namespace for the generic collection is "System.Collection.Generic" The
VB Collection class is the same as the VB 6 collection class.

Mike Ober.
 
Back
Top