开发者

Collections in C#

开发者 https://www.devze.com 2023-01-04 01:36 出处:网络
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

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 Sys开发者_运维知识库tem.Collections.Generic;    
using System.ComponentModel;    
using System.Collections.ObjectModel;

Can any body help me please.


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


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>();


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>();


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消