C# Generics

E

Elliott810

I have to be honest I haven't used many generics in my coding; I
really want to start using more of them but am having trouble with
what is the best way to use a generic with my Send method because the
Send method needs to point to different data factories. I really
would
like to hear some suggestions.

using System;
using System.Collections;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;
using System.Xml;
namespace PartsNow.Site.Database.Data
{
public class Groups
{
#region Fields
ArrayList m_items = new ArrayList();
#endregion
#region Properties
public virtual Group this[int index]
{
get
{
return (Group)m_items[index];
}
}
#endregion
#region Constructors
public Groups(Group[] objs)
{
foreach (Group obj in objs)
{
this.List.Add(obj);
}
}
internal Groups(SqlDataReader dbReader)
{
while (dbReader.Read())
{
m_items.Add(new Group(dbReader));
}
}
#endregion
#region Methods
public virtual void Add(Group obj)
{
m_items.Add(obj);
}
public virtual Group[] ToArray()
{
return (Group[])m_items.ToArray(typeof(Group));
}
public void Send()
{
SqlDataRecord dbRecord = new
SqlDataRecord(Factory.Catalog.MetaData);
SqlContext.Pipe.SendResultsStart(dbRecord);
foreach (Catalog catalog in base.List)
{
Factory.Catalog.SendResultsRow(catalog, dbRecord);
}
SqlContext.Pipe.SendResultsEnd();
}
#endregion
}
 
N

Nicholas Paldino [.NET/C# MVP]

In this case, do you have different types deriving from the Group class?
If so, you would want to have the Groups class be Groups<T>, with a
constraint on the type T to be of type Group. You will have a slight issue
with the constructor, as you can't enforce that those classes implement a
certain constructor pattern, so you might have a run time error when trying
to instantiate it through reflection.

If you don't have different types deriving from the Group class, and
it's just Group instances, then you could do this (note the class won't
complie as you make a call to base.List in the Send method, but this class
derives from Object, which doesn't have a List member):

public class Groups
{
List<Group> m_items = new List<Group>();

public virtual Group this[int index]
{
get
{
return m_items[index];
}
}

public Groups(Group[] objs)
{
foreach (Group obj in objs)
{
this.List.Add(obj);
}
}

internal Groups(SqlDataReader dbReader)
{
while (dbReader.Read())
{
m_items.Add(new Group(dbReader));
}
}

public virtual void Add(Group obj)
{
m_items.Add(obj);
}

public virtual Group[] ToArray()
{
return m_items.ToArray();
}

public void Send()
{
SqlDataRecord dbRecord = new SqlDataRecord(Factory.Catalog.MetaData);
SqlContext.Pipe.SendResultsStart(dbRecord);
foreach (Catalog catalog in base.List)
{
Factory.Catalog.SendResultsRow(catalog, dbRecord);
}
SqlContext.Pipe.SendResultsEnd();
}
}


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Elliott810 said:
I have to be honest I haven't used many generics in my coding; I
really want to start using more of them but am having trouble with
what is the best way to use a generic with my Send method because the
Send method needs to point to different data factories. I really
would
like to hear some suggestions.

using System;
using System.Collections;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;
using System.Xml;
namespace PartsNow.Site.Database.Data
{
public class Groups
{
#region Fields
ArrayList m_items = new ArrayList();
#endregion
#region Properties
public virtual Group this[int index]
{
get
{
return (Group)m_items[index];
}
}
#endregion
#region Constructors
public Groups(Group[] objs)
{
foreach (Group obj in objs)
{
this.List.Add(obj);
}
}
internal Groups(SqlDataReader dbReader)
{
while (dbReader.Read())
{
m_items.Add(new Group(dbReader));
}
}
#endregion
#region Methods
public virtual void Add(Group obj)
{
m_items.Add(obj);
}
public virtual Group[] ToArray()
{
return (Group[])m_items.ToArray(typeof(Group));
}
public void Send()
{
SqlDataRecord dbRecord = new
SqlDataRecord(Factory.Catalog.MetaData);
SqlContext.Pipe.SendResultsStart(dbRecord);
foreach (Catalog catalog in base.List)
{
Factory.Catalog.SendResultsRow(catalog, dbRecord);
}
SqlContext.Pipe.SendResultsEnd();
}
#endregion
}
 

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