tags:

views:

232

answers:

4
+2  Q: 

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.

+1  A: 

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.

Eric J.
this is an object collection. am using it to hold winsock collection
Smith
+2  A: 

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>();
AllenG
this is an object collection. am using it to hold winsock collection
Smith
@Oghenero: as @Cacek said in his comment, just substitute your correct object type (Socket, I believe in this case) where I have 'string'.
AllenG
Thanks you, very helful
Smith
+9  A: 

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

Joel Coehoorn
This. The C# compiler is expecting the `System.Collections.ObjectModel.Collection<T>` class, and Oghenero is (apparently) trying to use `Microsoft.VisualBasic.Collection`.
Anthony Pegram
@Joel - Beat me to the punch!@Oghenero - Yes, it's a namespace problem, but also you probably shouldn't use the MS.VB.Collection anyways and choose a specific generic one instead.
Reddog
ErrorThe type or namespace name 'Collection' does not exist in the namespace 'Microsoft.VisualBasic' (are you missing an assembly reference?)
Smith
@Oghenero Make sure you reference the Microsoft.VisualBasic assembly in your project. The collection class in there: http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.collection.aspx Or even better, read the more important part of my post and _don't_ reference that assembly.
Joel Coehoorn
+2  A: 

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>();
Gacek
this is an object collection. am using it to hold winsock collection
Smith
@Oghenero - winsock collection? So what is the type of this "winsock"? Just put it instead of T in the code and it should work for you
Gacek
i wrote a winsock component in vb.net . i want to convert it to c#. i use the collection to store collections or the component in case there is more that one. should i post the class file instead?
Smith
No. When you will rewrite your winsock component to C# and i.e. you will call the class implementing it `WinSock`, then just use `System.Collections.ObjectModel.Collection<WinSock>` as your collection (or even better, `List<WinSock>`)
Gacek