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

R

rapataa

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")
}
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

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")
}
 

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