473,503 Members | 5,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best method to retrieve a specific class instance from a collection

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; }
}
}

Oct 13 '06 #1
2 1460
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" <Wi*************@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
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; }
}
}

Oct 13 '06 #2
PS
"BSamp" <Wi*************@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
>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; }
}
}
Oct 14 '06 #3

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

Similar topics

17
6623
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
1
2318
by: m. pollack | last post by:
Hi all, I'm still trying to get to the bottom of the problem I am having with the CollectionBase class and the Object Collection Editor. Briefly put, I am exposing a strongly-typed collection...
4
2118
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
2
9149
by: Jon Davis | last post by:
The garbage handler in the .NET framework is handy. When objects fall out of scope, they are automatically destroyed, and the programmer doesn't have to worry about deallocating the memory space...
0
4197
by: Anonieko Ramos | last post by:
ASP.NET Forms Authentication Best Practices Dr. Dobb's Journal February 2004 Protecting user information is critical By Douglas Reilly Douglas is the author of Designing Microsoft ASP.NET...
18
4705
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
9
7813
by: raylopez99 | last post by:
What's the best way of implementing a multi-node tree in C++? What I'm trying to do is traverse a tree of possible chess moves given an intial position (at the root of the tree). Since every...
6
1412
by: Jack | last post by:
I have a set of functions to wrap a library. For example, mylib_init() mylib_func() mylib_exit() or handle = mylib_init() mylib_func(handle)
7
1799
by: cbmeeks | last post by:
Hope I'm using the right terminology. Anyway, say I have a class like: class Animal { public double GetValues() {......} public void FilterBy(string text); {......}
0
7188
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
7258
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
5558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3146
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
366
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.