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

Inherited collection reference lost after for each loop

hi,

I have a class that has inheritted a collectionbase class
The class works fine generally, I can add items to the collection however
whenever I execute a For Each loop, the dispose event is called and then
inherited collection disapears. (The same problem occurs regardless of which
collection type I inherit)
I cannot figure this out as I still have a ref to the object that inherits
the collection. I think it is something like the collection object is not
referenced to the class that inherits it however I cannot see how to fix it.

Any ideas on this would be appreciated

alex
Nov 22 '05 #1
5 1376
Alex <al*********@hotmail.co.uk> wrote:
I have a class that has inheritted a collectionbase class
The class works fine generally, I can add items to the collection however
whenever I execute a For Each loop, the dispose event is called and then
inherited collection disapears. (The same problem occurs regardless of which
collection type I inherit)
I cannot figure this out as I still have a ref to the object that inherits
the collection. I think it is something like the collection object is not
referenced to the class that inherits it however I cannot see how to fix it.

Any ideas on this would be appreciated


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2
It is not easy to post a working example however I have constructed some
simple pseudo code to demonstrate. The application
is written in vb however to attempt to write the example in vb would make it
unclear.

class MyList: Inherits Collections.Specialized.ListDictionary
{
bool LoadListFromFile(string _StrFileName as string){}
bool SaveListToFile(string _StrFileName as string){}
bool AddItemToList(string _StrItem as string){}
}
class MyForm
{
// I have declared a list somewhere else and I am simply assigning a
ref to the class here
// the list has already been initialised and loaded
MyList _ListClass= AnotherListClass;

//enumerate object to display items
object _MyObject;

// _ListClass.count=3 There are items in the list
for each _MyObject in _ListClass
// the items are added to the list no problem
list.items.add _MyObject
next

// now after the loop the object appears to go out of scope
// and the dispose method is called by .Net. I could understand this if
the object was declared here
// however the object is declared in somewere else. I do not loose the
ref to the MyList class
// it is only the base class that is disposed of. I have seen the same
behaviour with other similar implementations
// I think the problem is something obvious however just cannot see it.
// _ListClass.count=0 there are now no items
}
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Alex <al*********@hotmail.co.uk> wrote:
I have a class that has inheritted a collectionbase class
The class works fine generally, I can add items to the collection however whenever I execute a For Each loop, the dispose event is called and then
inherited collection disapears. (The same problem occurs regardless of which collection type I inherit)
I cannot figure this out as I still have a ref to the object that inherits the collection. I think it is something like the collection object is not referenced to the class that inherits it however I cannot see how to fix it.
Any ideas on this would be appreciated


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 22 '05 #3
Since writing I have established that the For Each loop has nothing to do
with the problem. I believe that it is now something to do with the Finalize
method being called

When an object that has inherited a collection goes out of scope the
finalize method appears to be called even if there is another ref to the
object. When the finalize method is called the finalize method on the
underlying base collection is called which automatically kills the
collection. I have found a work around and that is to call the
surpressfinalize method which stops this however I have not actually worked
out why this is happening. I am going to have to learn a lot more about the
way references are behave.

If anyone knows what is going on here then i would love to hear from you

thanks
Alex
Nov 22 '05 #4
Alex <al*********@hotmail.co.uk> wrote:
It is not easy to post a working example
Why, out of interest? It sounds like something which should be fairly
simple to reproduce.
however I have constructed some
simple pseudo code to demonstrate. The application
is written in vb however to attempt to write the example in vb would make it
unclear.

class MyList: Inherits Collections.Specialized.ListDictionary
{
bool LoadListFromFile(string _StrFileName as string){}
bool SaveListToFile(string _StrFileName as string){}
bool AddItemToList(string _StrItem as string){}
}
class MyForm
{
// I have declared a list somewhere else and I am simply assigning a
ref to the class here
// the list has already been initialised and loaded
MyList _ListClass= AnotherListClass;

//enumerate object to display items
object _MyObject;

// _ListClass.count=3 There are items in the list
for each _MyObject in _ListClass
// the items are added to the list no problem
list.items.add _MyObject
next

// now after the loop the object appears to go out of scope
// and the dispose method is called by .Net.
The dispose method is called on what though? It should be called on the
enumerator.
I could understand this if
the object was declared here
// however the object is declared in somewere else. I do not loose the
ref to the MyList class it is only the base class that is disposed of.


What exactly do you mean by that? The class itself is not disposed at
all, and there's only one object here - there isn't an instance of the
base class and separate instance of the child class.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #5
Alex <al*********@hotmail.co.uk> wrote:
Since writing I have established that the For Each loop has nothing to do
with the problem. I believe that it is now something to do with the Finalize
method being called

When an object that has inherited a collection goes out of scope the
finalize method appears to be called even if there is another ref to the
object.


I very much doubt that that's happening. Something else must be going
on. Again, a short but complete program demonstrating the problem will
help us to work out what's going on.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #6

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

Similar topics

2
by: Jim Schueler | last post by:
Here is some sample code that uses inherited class methods: sub parseHTML::docomment { my $comment = shift ; print $comment, "\n" ; } sub parseASP::AUTOLOAD { use vars qw( $AUTOLOAD ) ;
5
by: Alex | last post by:
hi, I have a class that has inheritted a collectionbase class The class works fine generally, I can add items to the collection however whenever I execute a For Each loop, the dispose event is...
5
by: Kurt Bauer | last post by:
I have an ASP group calendar application which pulls calendar data from Exchange via webdav into an XML string. I then loop the XML nodes to populate a collection of appointments. Finally I use...
8
by: TS | last post by:
I am trying to get set a property of a control on the inherited class from base class. I imagine i have to use reflection, so could someone give me the code to do it? something like this?...
6
by: newbie_csharp | last post by:
Hi, I have a class structure like this: rootform : System.Windows.Forms.Form midiform : rootform child1 : rootform child11 : child1 child2 : rootform chld21 : child2
35
by: MuZZy | last post by:
Hi All, I got a issue here and hope someone can help me: Let's consider this code: // =================== CODE START ================================= using System; using System.Data; ...
16
by: Ben Hannon | last post by:
Hi, I'm writting a COM Class in VB.NET to be used in a VB6 project (Tired of the VB6 hassles with cloning and serializing an object). All my classes I need cloneable/serializable are now in a...
3
by: muchan | last post by:
I'm a C++ programmer now poking into C#. I wanted to write a snippet of code, equivalent of //--- C++ code --------------- #include <string> #include <vector> class obj { // a POD class...
28
by: Goalie_Ca | last post by:
I have been reading (or at least googling) about the potential addition of optional garbage collection to C++0x. There are numerous myths and whatnot with very little detailed information. Will...
4
by: Clive Lumb | last post by:
Hi, I'm having a problem while adding objects to a collection. (VB.net 2005) I am reading data in from a file to a temporary variable (eg MyClass) and then adding it to a collection. When I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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,...

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.