473,395 Members | 1,986 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,395 software developers and data experts.

Advice on best way to handle object disposal...

I have three classes. One (Class ObjectC) and two other classes (Class1 and
Class2) that will hold instances of the ObjectC class. I would like to
transfer an instance of OjectC from Class1 into Class2, while making sure
that no resource leak occurs. Is the following the best way to hanlde this?
Is there a more efficient way?

public class ObjectC
{
public ObjectC()
{
// Initialize some data.
}
public void Dispose()
{
// Destroy initialized data.
}
}
public class Class1 : System.Collections.CollectionBase
{
public Class1()
{
}
public void CreatObject()
{
ObjectC oc = new ObjectC();
List.Add(oc);
}
public void TransferObject(Class2 c2)
{
if(List.Count>0)
{
c2.AddObject(List[0]);
((ObjectC)List[0]).Dispose();
List.RemoveAt(0);
}
}
}
public class Class2 : System.Collections.CollectionBase
{
public Class2()
{
}
public void AddObject(Object o)
{
List.Add(o);
}
}

At execution time I would then:

Class1 theC1 = new Class1();
Class2 theC2 = new Class2();

// Create 100 ObjectC objects.
for(int i=0;i<100;i++)
{
theC1.CreateObject();
}
I then would transfer the newly instantiated ObjectC objects into theC2 from
theC1:

// Transfer 100 ObjectC objects from theC1 to theC2.
for(int i=0;i<100;i++)
{
theC1.TransferObject(theC2);
}

Any ideas or comments welcomed.
Jan 10 '06 #1
2 1658
If you implement the IDisposable interface correctly, you shouldn't have to
worry. The destructor method can dispose the object when it is garbage
collected (if it is overlooked). See:

http://msdn.microsoft.com/library/de...classtopic.asp

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"Robert Vasquez" <Ro***********@discussions.microsoft.com> wrote in message
news:B5**********************************@microsof t.com...
I have three classes. One (Class ObjectC) and two other classes (Class1
and
Class2) that will hold instances of the ObjectC class. I would like to
transfer an instance of OjectC from Class1 into Class2, while making sure
that no resource leak occurs. Is the following the best way to hanlde
this?
Is there a more efficient way?

public class ObjectC
{
public ObjectC()
{
// Initialize some data.
}
public void Dispose()
{
// Destroy initialized data.
}
}
public class Class1 : System.Collections.CollectionBase
{
public Class1()
{
}
public void CreatObject()
{
ObjectC oc = new ObjectC();
List.Add(oc);
}
public void TransferObject(Class2 c2)
{
if(List.Count>0)
{
c2.AddObject(List[0]);
((ObjectC)List[0]).Dispose();
List.RemoveAt(0);
}
}
}
public class Class2 : System.Collections.CollectionBase
{
public Class2()
{
}
public void AddObject(Object o)
{
List.Add(o);
}
}

At execution time I would then:

Class1 theC1 = new Class1();
Class2 theC2 = new Class2();

// Create 100 ObjectC objects.
for(int i=0;i<100;i++)
{
theC1.CreateObject();
}
I then would transfer the newly instantiated ObjectC objects into theC2
from
theC1:

// Transfer 100 ObjectC objects from theC1 to theC2.
for(int i=0;i<100;i++)
{
theC1.TransferObject(theC2);
}

Any ideas or comments welcomed.

Jan 10 '06 #2
Robert Vasquez <Ro***********@discussions.microsoft.com> wrote:
I have three classes. One (Class ObjectC) and two other classes (Class1 and
Class2) that will hold instances of the ObjectC class. I would like to
transfer an instance of OjectC from Class1 into Class2, while making sure
that no resource leak occurs. Is the following the best way to hanlde this?
Is there a more efficient way?


Just to check - do you actually have any non-memory resources? Are you
sure you really need to do *anything*?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 10 '06 #3

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

Similar topics

2
by: J.Bijleveld | last post by:
Hello colleagues, At this moment we have a real big problem using a .NET application with an Oracle database (v8.1.6). I hope someone has encountered this problem before and is able to help me...
14
by: Howard | last post by:
Hi, I recently had a problem where I decided to store objects in a vector. (Previously, I had always stored pointers in vectors). Well, naturally, when storing an object in a vector, using...
4
by: Olaf Baeyens | last post by:
I do not want to start a ASP versus PHP discussion, because I favour ASP(X), but I seek advice since I have no clue about PHP yet. I want to work with a friend on some web designs. At this point...
6
by: V. Jenks | last post by:
I apologize if this is the wrong forum for this, I could not locate one that was exactly appropriate for this topic. Over the last couple of years I've been doing a lot of reading on design...
5
by: wapsiii | last post by:
On a asp.net project I'm using generated classes to access the database. I'm developing business logic classes to handle all business logic and pass on info to the data access classes. On the code...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
2
by: Gary Howlett | last post by:
Hi, Ive have a webservice method as such... public UserDetails Login(string Username, string Password) { /* Do login */
8
by: george.leithead | last post by:
Hi all, I'm looking for some advice on how best to achitect the following requirement. I'm basically writing a Fantasy Football (FF) Web site, and would like to have it fully OO and have it...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
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,...

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.