By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,670 Members | 1,512 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,670 IT Pros & Developers. It's quick & easy.

baseClass & unknown type object as parameter

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
First, oTest is an instance of an object. New requires a type, not an
instance.

DalePres

"rapataa" <dg@rapataa.frup> wrote in message
news:c0***************************@msgid.xenosite. net...
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

Nov 16 '05 #2

P: n/a
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
First, oTest is an instance of an object. New requires a type, not
an instance.

DalePres

"rapataa" <dg@rapataa.frup> wrote in message
news:c0***************************@msgid.xenosite. net...
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


Nov 16 '05 #3

P: n/a
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

Nov 16 '05 #4

P: n/a
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 [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:e$**************@TK2MSFTNGP09.phx.gbl...
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

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.