template for typed arrays in .NET

  • Thread starter Thread starter Boni
  • Start date Start date
B

Boni

Dear all,
currently I only found a possibility to create for each type it's own typed
array. I am sick to create for each of my types a separate array class.
In C++ there are so called templates: template<T > class...
Is it possible in VB or c#? Can you give me an example?
Thanks,
Boni
 
Boni said:
currently I only found a possibility to create for each type it's own
typed array. I am sick to create for each of my types a separate array
class.

I don't see any reason for creating array classes. You can simply create a
typed array using VB.NET's array syntax:

\\\
Dim foo(9) As MyType
....
///
 
I create them for typing ArrayList and Hashtable classes, perhaps this is
what he means?
 
What should I do If I nees an arraylist of unknown size or other collection?
I don't see any reason for creating array classes. You can simply create
a typed array using VB.NET's array syntax:

What should I do If I nees an arraylist of unknown size or other collection
type?
 
If you want to more strongy type an arraylist, derive a new class from
collectionbase and override some of the base class methods.
 
Boni said:
What should I do If I nees an arraylist of unknown size or other collection
type?

I think you're really talking about collection classes, aren't you? In
..NET 1, the usual way is to derive those from CollectionBase to create
collections that are typed by providing methods that have typed
signatures. In .NET 2, Generics provide typed collection classes like
List<T> out of the box, and there's PowerCollections
(http://www.wintellect.com/powercollections/) if you need additional
specialised collection implementations.


Oliver Sturm
 
Hi,

You could do

foreach( MyType t in arraylist )
{}

you will get an exception if any of the elements of arraylist is not of
castable to MyType


cheers,
 
Yes I am. 2005 and .NET 2 are still in Beta so I can't use them in a
production environment. However, with generics this question will become
obsolete as you will be able to write something like Dim x as New
ArrayList<myClass>, I assume (I haven't looked into it yet).
 
Get the Visual Studio 2005 Beta 2 and do something like this:

C#:

List<MyType> myList = new List<MyType>();

Now you can use the strictly typed List<> object to Add(), Insert() and
traverse a list of MyType objects.

The VS 2005 Beta 2 does, indeed, rock!
 
Robin Tucker said:
I create them for typing ArrayList and Hashtable classes, perhaps this is
what he means?

I don't consider arraylists and hashtables to be arrays...
 
"Anders Tornblad, SPCS, Sweden" <anders dot tornblad at telia dot com
nospamplease said:
Get the Visual Studio 2005 Beta 2 and do something like this:

C#:

List<MyType> myList = new List<MyType>();

Now you can use the strictly typed List<> object to Add(), Insert() and
traverse a list of MyType objects.

In VB 2005:

\\\
Dim MyList As New List(Of MyType)
///
 
Back
Top