473,394 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

instance control

hi,

I want to create a class in C# that will only have 'n' instances at any
given time. If (n+1)th instance is requested, an Exception will be
thrown. These instances must be maintained in a collection in the
class. Note that once an instance is acquired, it can be released as
well.
thanks
murali

Mar 23 '06 #1
3 1135
Hi Murali,
there is no native way in .Net to do this to an object. You can either
make an object pool which has a get and release method i.e.

MyObject ObjectPool.Get()
ObjectPool.Release(MyObject objectToRelease)

or you can build this into the object itself and have static methods:

class MyObject
{
//Stop anyone from creating these objects
private MyObject()
{
}

public static MyOBject Get()
{
}

public static void Release(MyObject object)
{
}
}

Which internally has a collection of MyObjects. Then you can get an
instance like:

MyObject x = MyObject.Get();
MyObject.Release(x);

Mark Dawson
http://www.markdawson.org
"murali@pune" wrote:
hi,

I want to create a class in C# that will only have 'n' instances at any
given time. If (n+1)th instance is requested, an Exception will be
thrown. These instances must be maintained in a collection in the
class. Note that once an instance is acquired, it can be released as
well.
thanks
murali

Mar 23 '06 #2
Hi,

"murali@pune" <mu***********@gmail.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
hi,

I want to create a class in C# that will only have 'n' instances at any
given time. If (n+1)th instance is requested, an Exception will be
thrown. These instances must be maintained in a collection in the
class. Note that once an instance is acquired, it can be released as
well.


How they will be released ? by whom?

You said it will be kept in a collection so the class itself will have to
release them.

Take a look at the Singleton pattern
http://www.yoda.arachsys.com/csharp/singleton.html , you can extend it to
something like what you want.

Also a good idea to refresh the different locking mechanism
http://www.yoda.arachsys.com/csharp/.../locking.shtml

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Mar 23 '06 #3
"murali@pune" <mu***********@gmail.com> a écrit dans le message de news:
11*********************@j33g2000cwa.googlegroups.c om...

| I want to create a class in C# that will only have 'n' instances at any
| given time. If (n+1)th instance is requested, an Exception will be
| thrown. These instances must be maintained in a collection in the
| class. Note that once an instance is acquired, it can be released as
| well.

Make the default constructor private and add a static method that will
control and return a number of instances; use a static field to keep track
of the count. You may have difficulty keeping track of instances being freed
as garbage collection is non-deterministic and can be difficult to track.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Mar 26 '06 #4

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

Similar topics

0
by: muralidharan | last post by:
WebForm1.aspx Code: <%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %> <ComponentArt:TreeView id="TreeView1" Height="520"...
2
by: gaetanog | last post by:
Hi there.. I have a question about instance usage... I created a VStudio 2003 solution within 3 projects as follow: 1) Proj One: Main form 2) Proj Two: User Control 3) Proj Three: Class I...
4
by: Frawls | last post by:
Hi, I get the following error when trying to run a search on my aspx site, this error only occours if the product im searching for does not exist. Can anybody explain this please and help me...
3
by: Islam Elkhayat | last post by:
I hve Dataclasses one for each dbtable & i have user control with MoveNext() & MovePrev() methods i want the toolbar to use the current class so i created an Interface (IDbClasses) with the 2...
3
by: Brano | last post by:
HI all, I have a problem i have a web application that was working fine and this morning when i run it and click on a button that does Reponse.Redirect to a page i get this error : Server...
0
by: jwb | last post by:
I've created a control within a Web Control library, using ASP.NET 1.1 (VB.NET). The control has a value property and a ValueChanged event. I have implemented the IPostBackEventHandler interface...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
0
by: Tamer Ibrahim | last post by:
Hi, Sometimes, I got the following error message when I use ajax calendar control on some aspx pages : Object reference not set to an instance of an object. Description: An unhandled exception...
2
by: rhino | last post by:
I am trying to create a second instance on a Windows XP machine that has DB2 Personal Edition installed. It's not going awfully well so I thought I'd ask some questions here. My 'db2icrt...
8
by: xz | last post by:
Why C++ (as well as Java) adopts class-based access control instead of instance-based access control? I had never paid attention to whether an access-control is class-based or instance-based but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.