Best method to retrieve a specific class instance from a collection


B

BSamp

I have the following business entity classes shown below. I have a data
layer that
retrieves the data from the database and populates a new instance of
the PendingRecord class then adds it to the PendingRecords collection.

i.e. PendingRecords pendingRecords = dataLayer.GetPendingRecords();

What I would like to do is to be able to have a method in the
collection
class that I could pass a wizardId and get the specific PendingRecord
instance based upon that specific wizardId.

such as: PendingRecord rec = pendingRecords.GetById(wizardId)

The current indexer in the
collection class returns the instance at the index of the collection,
but
not the instance for a particular wizardId.

How would I structure the method in the collection class to
accomplish this?

Or is there a better way?

I'm running Framework 1.1 on Visual Studio 2003

public class PendingRecord
{
#region Constructors
public PendingRecord() { }

public PendingRecord(int wizardId, string wizardType, string firstName,
string lastName)
{
_wizardId = wizardId;
_wizardType = wizardType;
_firstName = firstName;
_lastName = lastName;
}
#endregion

#region Properties
private int _wizardId = 0;
public int WizardId
{
get{ return _wizardId; }
set{ _wizardId = value; }
}

private string _wizardType = string.Empty;
public string WizardType
{
get{ return _wizardType; }
set{ _wizardType = value; }
}

private string _firstName = string.Empty;
public string FirstName
{
get{ return _firstName; }
set{ _firstName = value; }
}

private string _lastName = string.Empty;
public string LastName
{
get{ return _lastName; }
set{ _lastName = value; }
}

private string _confirmPage = string.Empty;
public string ConfirmationPage
{
get{ return _confirmPage; }
set{ _confirmPage = value; }
}
#endregion
}

public class PendingRecords : CollectionBase
{
public void Add(PendingRecord item)
{
InnerList.Add(item);
}

public void Remove(PendingRecord item)
{
InnerList.Remove(item);
}

public PendingRecord this[int index]
{
get { return (PendingRecord)InnerList[index]; }
set { InnerList[index] = value; }
}
}
 
Ad

Advertisements

S

sloan

I usually do a "Contains" method.

See my class below:

If you went this route, youd have a

PendingRecord rec = pendingRecords.Contains(wizardId)



public class OrderCollection : System.Collections.CollectionBase
{

public void Add ( BusinessObjects.Order cust )
{
base.InnerList.Add(cust);
}

public BusinessObjects.Order this[int index]
{
get
{
return (BusinessObjects.Order )base.InnerList[index];
}
}


public BusinessObjects.Order Contains( int orderId )
{

foreach( BusinessObjects.Order item in base.InnerList )
if( item.OrderID .Equals(orderId) )
return item;
return null;
}


protected override void OnValidate(object value)
{
base.OnValidate(value);
if (!(value is BusinessObjects.Order))
{
throw new ArgumentException("Collection only supports Order objects.");
}
}


}









BSamp said:
I have the following business entity classes shown below. I have a data
layer that
retrieves the data from the database and populates a new instance of
the PendingRecord class then adds it to the PendingRecords collection.

i.e. PendingRecords pendingRecords = dataLayer.GetPendingRecords();

What I would like to do is to be able to have a method in the
collection
class that I could pass a wizardId and get the specific PendingRecord
instance based upon that specific wizardId.

such as: PendingRecord rec = pendingRecords.GetById(wizardId)

The current indexer in the
collection class returns the instance at the index of the collection,
but
not the instance for a particular wizardId.

How would I structure the method in the collection class to
accomplish this?

Or is there a better way?

I'm running Framework 1.1 on Visual Studio 2003

public class PendingRecord
{
#region Constructors
public PendingRecord() { }

public PendingRecord(int wizardId, string wizardType, string firstName,
string lastName)
{
_wizardId = wizardId;
_wizardType = wizardType;
_firstName = firstName;
_lastName = lastName;
}
#endregion

#region Properties
private int _wizardId = 0;
public int WizardId
{
get{ return _wizardId; }
set{ _wizardId = value; }
}

private string _wizardType = string.Empty;
public string WizardType
{
get{ return _wizardType; }
set{ _wizardType = value; }
}

private string _firstName = string.Empty;
public string FirstName
{
get{ return _firstName; }
set{ _firstName = value; }
}

private string _lastName = string.Empty;
public string LastName
{
get{ return _lastName; }
set{ _lastName = value; }
}

private string _confirmPage = string.Empty;
public string ConfirmationPage
{
get{ return _confirmPage; }
set{ _confirmPage = value; }
}
#endregion
}

public class PendingRecords : CollectionBase
{
public void Add(PendingRecord item)
{
InnerList.Add(item);
}

public void Remove(PendingRecord item)
{
InnerList.Remove(item);
}

public PendingRecord this[int index]
{
get { return (PendingRecord)InnerList[index]; }
set { InnerList[index] = value; }
}
}
 
Ad

Advertisements

P

PS

BSamp said:
I have the following business entity classes shown below. I have a data
layer that
retrieves the data from the database and populates a new instance of
the PendingRecord class then adds it to the PendingRecords collection.

i.e. PendingRecords pendingRecords = dataLayer.GetPendingRecords();

What I would like to do is to be able to have a method in the
collection
class that I could pass a wizardId and get the specific PendingRecord
instance based upon that specific wizardId.

such as: PendingRecord rec = pendingRecords.GetById(wizardId)

The current indexer in the
collection class returns the instance at the index of the collection,
but
not the instance for a particular wizardId.

How would I structure the method in the collection class to
accomplish this?

You should also implement a private hashtable to handle lookups by your key.
You add and remove from the hashtable as you add and remove from the
collection. If your key was not an integer then you would normally have 2
indexers, one by key and one by index. Because your key is an integer you
need a method like GetPendingRecord(int id) { return
(PendingRecord)myHashTable[id];}

PS
Or is there a better way?

I'm running Framework 1.1 on Visual Studio 2003

public class PendingRecord
{
#region Constructors
public PendingRecord() { }

public PendingRecord(int wizardId, string wizardType, string firstName,
string lastName)
{
_wizardId = wizardId;
_wizardType = wizardType;
_firstName = firstName;
_lastName = lastName;
}
#endregion

#region Properties
private int _wizardId = 0;
public int WizardId
{
get{ return _wizardId; }
set{ _wizardId = value; }
}

private string _wizardType = string.Empty;
public string WizardType
{
get{ return _wizardType; }
set{ _wizardType = value; }
}

private string _firstName = string.Empty;
public string FirstName
{
get{ return _firstName; }
set{ _firstName = value; }
}

private string _lastName = string.Empty;
public string LastName
{
get{ return _lastName; }
set{ _lastName = value; }
}

private string _confirmPage = string.Empty;
public string ConfirmationPage
{
get{ return _confirmPage; }
set{ _confirmPage = value; }
}
#endregion
}

public class PendingRecords : CollectionBase
{
public void Add(PendingRecord item)
{
InnerList.Add(item);
}

public void Remove(PendingRecord item)
{
InnerList.Remove(item);
}

public PendingRecord this[int index]
{
get { return (PendingRecord)InnerList[index]; }
set { InnerList[index] = value; }
}
}
 

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