473,546 Members | 2,196 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.GetPe ndingRecords();

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(wizardI d)

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(i nt 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 ConfirmationPag e
{
get{ return _confirmPage; }
set{ _confirmPage = value; }
}
#endregion
}

public class PendingRecords : CollectionBase
{
public void Add(PendingReco rd item)
{
InnerList.Add(i tem);
}

public void Remove(PendingR ecord item)
{
InnerList.Remov e(item);
}

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

Oct 13 '06 #1
2 1463
I usually do a "Contains" method.

See my class below:

If you went this route, youd have a

PendingRecord rec = pendingRecords. Contains(wizard Id)

public class OrderCollection : System.Collecti ons.CollectionB ase
{

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

public BusinessObjects .Order this[int index]
{
get
{
return (BusinessObject s.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(obje ct value)
{
base.OnValidate (value);
if (!(value is BusinessObjects .Order))
{
throw new ArgumentExcepti on("Collection only supports Order objects.");
}
}
}



"BSamp" <Wi************ *@gmail.comwrot e in message
news:11******** **************@ k70g2000cwa.goo glegroups.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.GetPe ndingRecords();

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(wizardI d)

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(i nt 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 ConfirmationPag e
{
get{ return _confirmPage; }
set{ _confirmPage = value; }
}
#endregion
}

public class PendingRecords : CollectionBase
{
public void Add(PendingReco rd item)
{
InnerList.Add(i tem);
}

public void Remove(PendingR ecord item)
{
InnerList.Remov e(item);
}

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

Oct 13 '06 #2
PS
"BSamp" <Wi************ *@gmail.comwrot e in message
news:11******** **************@ k70g2000cwa.goo glegroups.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.GetPe ndingRecords();

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(wizardI d)

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 GetPendingRecor d(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(i nt 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 ConfirmationPag e
{
get{ return _confirmPage; }
set{ _confirmPage = value; }
}
#endregion
}

public class PendingRecords : CollectionBase
{
public void Add(PendingReco rd item)
{
InnerList.Add(i tem);
}

public void Remove(PendingR ecord item)
{
InnerList.Remov e(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
6625
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. So I shall have an Abstract Base called 'Car' implemented by Toyota, Ford, and Buick. Further I'd like to enable to client to say Car *factory;
1
2325
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 property, using a class derived from CollectionBase, to the user via the PropertyGrid control's popup Collection Editor. I need to know when the...
4
2126
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 access module that creates a large disconnected dataset from a database. I want to pass that dataset back to the calling program. And then perhaps I...
2
9158
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 for those objects. In fact, all the programmer has to worry about is the total sum of objects loaded into RAM at any known point. Memory leaks are not...
0
4202
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 Applications and owner of Access Microsystems. Doug can be reached at doug@accessmicrosystems.com....
18
4715
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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class...
9
7816
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 chess position has around 30 moves, it would mean every node of the tree would have 30 branches (on average), which in turn themselves would average...
6
1416
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
1804
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
7507
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6030
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5361
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
747
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.