Generics/Constructor

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to follow along with an example about how to create a generic
collection which is "Type" drivern (the article is here
http://www.ftponline.com/vsm/2007_02/magazine/features/dfergus/page4.aspx - I
think you have to have an account to actually see the article...but anyway).
In the example, they declare a couple arraylist. When I attempt to create a
new instance of the class, I get a null exception on the array list which
were declared. In looking at the example further, I do not see where there
is a constructor for the class. In the example, there is code for VB and
then code for C#. In VB, I believe the example show a constructor which
looks like the following:

Public Sub New()
_deletedList = New ArrayList(5)
_addedList = New ArrayList(5)
End Sub



The example shows no constructor for C#, so I guessing it was just omitted
in error. I am trying to figure out the constructor (shouldn't be
difficult), but I can't figure it out. He is a sample of my generic class..


public class CollectionEx<T> : System.Collections.CollectionBase
where T : ICollectionEx
{


}

If I try to add a construtor which looks like the following, I get an
error....
public CollectionEx<T>()
{
_DeletedList = new ArrayList();

}

What am I missing?
 
Did not read the article, but your constructor will compile if you change
public CollectionEx<T>() to public CollectionEx(T var).

Not sure why it is using generics as I don't see any code to take advantage
of it, but as I said, I did not read the article.
 
Hey, thanks Laura, but I'm still a bit confused.

Creating the constructor you identified, would require me to pass the
"Type" of the collection object I am creating each time. The example
provided samples of creating a new instance of the generic class and it
looked like the following:

_TblColumns = new CollectionEx<TableColumn>();

This appears to allow me to create an instance of the CollectionEx class
without having to pass a variable and still at the same time assign the
"Type" to the instance of CollectionEx.

If I make the change you suggested, then the above does not function as I
now have to pass the "Type" as part of the constructor. I can make that
happen, but it just not feel right...
 
If I try to add a construtor which looks like the following, I get an
error....
public CollectionEx<T>()
{
_DeletedList = new ArrayList();

}

What am I missing?

Have you tried it with just

public CollectionEx()
{
_DeletedList = new ArrayList();
}



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top