Smith
Smith

Reputation: 5951

Collections in C#

am converting a vb.net component to c#, i get this error

Using the generic type 'System.Collections.ObjectModel.Collection<T>' requires '1' type arguments

This is what i did

in VB.NET i had this

 Private _bufferCol As Collection

i did this in c#

private Collection _bufferCol = new Collection();

My declaration is

using Microsoft.VisualBasi;    
using System.Collections;    
using System.Collections.Generic;    
using System.ComponentModel;    
using System.Collections.ObjectModel;

Can any body help me please.

Upvotes: 2

Views: 4065

Answers (4)

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

It's confused because there is a type named "Collection" in both the Microsoft.VisualBasic and the System.Collections.ObjectModel namespaces. You can fix things for a direct translation like this:

private Microsoft.VisualBasic.Collection _buffer = new Microsoft.VisualBasic.Collection();

But I don't recommend doing that. The Microsoft.VisualBasic namespace is intended mainly for easier migration away from older vb6-era code. You really shouldn't use it's Collection type any more.

Instead, adapt this code to use a generic List<T> or Collection<T>.

Upvotes: 10

Gacek
Gacek

Reputation: 10312

As it says, you need to specify the type of objects stored in collection.

When you have:

System.Collections.ObjectModel.Collection<T>

this T means the type of your objects in collection.

If you need to accept all types of objects, just use

var _bufferCol = new System.Collections.ObjectModel.Collection<object>();

Upvotes: 2

AllenG
AllenG

Reputation: 8190

You need a Type declaration fro your collection. That is, what type is your collection holding?

For instance:

//strings
private Collection<string> _bufferCol = new Collection<string>();

Upvotes: 3

Eric J.
Eric J.

Reputation: 150108

You are attempting to use a typed collection in C# but not specifying the type.

Try

private Collection<MyType> _bufferCol = new Collection<MyType>();

Where MyType is the type of the thing you want to put in the collection.

C# allows you to strongly type collections, lists, sets, etc so that the kind of thing they contain is known and can be checked at compile time.

Upvotes: 1

Related Questions