473,382 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

baseClass & unknown type object as parameter

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
4 1792
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: DKode | last post by:
Ok, here is another question I have that is sort of unrelated to my last posting about composition. I have three Collection classes: EmployeeCollection HourCollection InfractionCollection
11
by: Arpan | last post by:
<% If(False) Then Response.Write("False") Else Response.Write("True") End If %> In the above code, the Else condition will be executed. Why?
5
by: Michel | last post by:
Hi there, What is the best way to serialize unknown data? I have a class that contains a list of parameter objects. The parameter has a value which can be a simple value of a complex class,...
0
by: Dan Noel | last post by:
I am implementing a DataStore/Settings object that stores objects in XML by serializing the objects in XML and then associating these objects with key names. At some later point, I want to...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
10
by: Michael Maes | last post by:
Hi, I have a BaseClass from which many Classes Derive. In short: the BaseClass provides the functionalities (Methods) and the Derived Classes extend it with Properties. One of the (Base)...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
45
by: mdh | last post by:
Hi All, The section is titled Variable-length Argument lists. May I ask a question about the use of "macros". To quote K&R. "The standard header <stdarg.hcontains a set of macro definitions...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.