baseClass & unknown type object as parameter

R

rapataa

hi,

I'm trying to fill a collection using the following 'generic' code:
-----------------
public class baseCollection : System.Collections.CollectionBase
{
protected void Fill(string strSQL, object oTest)
{
SqlConnection Conn = new SqlConnection(Settings.sDBConn);
Conn.Open();
SqlCommand cmdContent = new SqlCommand(strSQL, Conn);
SqlDataReader drContent;
drContent = cmdContent.ExecuteReader();
while (drContent.Read())
{
this.Add(new oTest( drContent.GetInt32(0) )); ///here is where it
goes wrong
}
drContent.Close();
Conn.Close();
}
}
-----------------

I have several collection classes that (could) derive from this class.
For example (and I would like to use it in this way, if possible):

public class ContentItemsTest : baseCollection
{
public ContentItemsTest(int iType)
{
Fill("SELECT as_ID FROM tblWhhatever" , new myItemClass());
}
}
-------------------------
The "baseCollection.Fill" doesn't know I'm sending a "myItemClass" as
object, and that's exactly what I want. Unfortunately this doesn't work.
Anyone any ideas?

cheers
 
D

DalePres

First, oTest is an instance of an object. New requires a type, not an
instance.

DalePres
 
W

w

hi-

DalePres is right- you probably wanna say something like:

this.Add(typeof(MyItemClass)oTest);

or assign it earlier:

MyItemClass mIC = (MyItemClass)oTest;

Now all the properties of the class are exposed and you can add whatever you need

ie this.Add(mIC.Name);

I wasn't able to work out what you were adding to the collection so sorry this is a bad example
 
R

Richard Blewett [DevelopMentor]

hmmm, that first construct with typeof won't compile. What were you trying to suggest with that one?

From looking at the code I would guess that the OP needs to use the type name of oTest rather than oTest in the new construct - I assume that it has a constructor that takes an int

But without more details abiout the type of oTest and what the Add method takes its hard to work out what the solution is

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

hi-

DalePres is right- you probably wanna say something like:

this.Add(typeof(MyItemClass)oTest);

or assign it earlier:

MyItemClass mIC = (MyItemClass)oTest;

Now all the properties of the class are exposed and you can add whatever you need

ie this.Add(mIC.Name);

I wasn't able to work out what you were adding to the collection so sorry this is a bad example
 
R

rapataa

let's say there's a Plumber object.
The collection object "Plumbers" will be filled based on the Plumbers in
tblPlumber

strSQL = "SELECT ID FROM tblPlumber"

This string is send to the base collection object ( "public class
baseCollection : System.Collections.CollectionBase" ) together.
The void "Fill" will use the plumber info from the DB to fill the collection
object with objects of type "Plumber". The constructor of Plumber takes an
int.

It's also possible to create a (second) "Add" function in the derived class,
but I think that's butt_ugly.

--------



Richard Blewett said:
hmmm, that first construct with typeof won't compile. What were you trying to suggest with that one?

From looking at the code I would guess that the OP needs to use the type
name of oTest rather than oTest in the new construct - I assume that it has
a constructor that takes an int
But without more details abiout the type of oTest and what the Add method
takes its hard to work out what the solution is
 

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

Top