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

  • Thread starter Thread starter rapataa
  • Start date Start date
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")
}
 
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")
}
 
Back
Top