473,396 Members | 1,933 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,396 software developers and data experts.

how to setup a generic base class to fill collection objects?

Hi,

I'm trying to write a generic base class to loop through data and fill my collection objects.
The thing is that the base class doesn't know the type of the object I guess.

The code should look something like this....
--------------------------------------------------------------------------------------
public class baseCollection : System.Collections.CollectionBase
{
protected void Fill(string strSQL)
{
SqlConnection Conn = new SqlConnection("conn settings");
Conn.Open();
SqlCommand cmdContent = new SqlCommand("strSQL", Conn);
SqlDataReader drContent;
drContent = cmdContent.ExecuteReader();
while (drContent.Read())
{
// add the specific item to this collection
}
drContent.Close();
Conn.Close();
}
}

public class Employers : baseCollection
{
Fill("select * from tblEmployer")
}

public class Shops : baseCollection
{
Fill("select * from tblShop")
}
--------------------------------------------------------------------------------------

anyone?
Nov 17 '05 #1
1 1587
Rapataa,

You need to indicate somewhere what the type is, create one for each row
that you add (assuming that one row corresponds to one item in the
collection).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"rapataa" <dg@rapataa.frup> wrote in message
news:43***************************@msgid.xenosite. net...
Hi,

I'm trying to write a generic base class to loop through data and fill my
collection objects.
The thing is that the base class doesn't know the type of the object I
guess.

The code should look something like this....
--------------------------------------------------------------------------------------
public class baseCollection : System.Collections.CollectionBase
{
protected void Fill(string strSQL)
{
SqlConnection Conn = new SqlConnection("conn settings");
Conn.Open();
SqlCommand cmdContent = new SqlCommand("strSQL", Conn);
SqlDataReader drContent;
drContent = cmdContent.ExecuteReader();
while (drContent.Read())
{
// add the specific item to this collection
}
drContent.Close();
Conn.Close();
}
}

public class Employers : baseCollection
{
Fill("select * from tblEmployer")
}

public class Shops : baseCollection
{
Fill("select * from tblShop")
}
--------------------------------------------------------------------------------------

anyone?
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
8
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; ...
2
by: dcew | last post by:
Here's what I'm trying to understand; how can you store a generic collection in a variable/field? If I have an abstract generic collection class as follows... public abstract class...
6
by: SharpCoderMP | last post by:
hi, until .net 2.0 i've been writing my own collections derived from CollectionBase. Now with the generics it is much easier to do that... but! i have some questions about generic lists of...
1
by: conchur | last post by:
Is there an elegant way to have a class utilise an inherited method to return a Typed Collection of itself? Example: class MyList<T> : List<T> where T : MyRoot { Add... Remove... }
1
by: Craig Buchanan | last post by:
I have a class named 'Filter'. It has a property named 'Destinations' (an ArrayList). I would like the Destinations property to be a collection of objects. The objects that can be collected are:...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.