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

Setting objects to Null in a linked list

I'm still trying to learn c#, and while doing so I am trying to
convert some C++ code to C#. Doing so has kind of challenged my
understanding of the way values and references work in C# when i'm
tyring to duplicate C++ items that use pointers, such as a linked
list.

In the c++ code that i'm trying to convert, I have a linked list of
objects that keeps track of parent, child and sibling objects. My C#
version of it seems to be going ok until I get to a function where I
remove all of the child objects. In the C++ version the list is
traversed and the child objects get deleted, but i'm not sure how to
do it properly in C#.

Here is a bit of sample code of what I have done is C#, followed by
the C++ version of the function that is getting me confused:

Class MyObject
{
MyObject m_ChildObject;
MyObject m_NextSiblingObject;
MyObject m_PreviousSiblingObject;
MyObject m_ParentObject;
//I then have a series of Get and Set methods for
setting the above objects
Public void AddChildObject( MyObject childObjectToAdd)
{
childObjectToAdd.SetParentObject(this);

//I check to see if this is the first child for this object
If (m_ChildObject == null)
{
M_ChildObject = childObjectToAdd;
}
else //this isn’t the first child for this object
{
MyObject tempObject = this.GetChildObject();

While (tempObject.GetNextSiblingObject() != null)
{
tempObject = tempObject.GetNextSiblingObject();
}


tempObject.SetNextSiblingObject(childObjectToAdd);

childObjectToAdd.SetPreviousSiblingObject(tempObje ct);
}

}

Public void RemoveAllChildObjects()
{
MyObject tempObject = this.GetChildObject();

While(tempObject != null)
{
MyObject nextObject = tempObject.GetNextSiblingObject();

//*now I want to delete or set to null each object in the linked
list

tempObject = nextObject;
}
}
}

I don’t know if it is helpful to look at, but here is the c++ version
of the RemoveAllChildObjects that I am trying to duplicate in C#

Public boid RemoveAllChildObjects()
{
MyObject* tempObject = this->GetChildObject();

While(tempObject)
{
MyObject* nextObject = tempObject->GetNextSiblingObject();
SAFE_DELETE(tempObject);
tempObject = nextObject;
}
}
Jun 27 '08 #1
1 1949

In C# you don't have to worry about deleting (setting to null) every
object in the list.
All you need to do is remove every refence you have to this list, and
garbage collector will handle cleaning it up for you.
Jun 27 '08 #2

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

Similar topics

21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
11
by: Zlatko Matiæ | last post by:
Hello. I have a MS Access front-end working with PostgreSQL database. I have successfully created saved File DSN. My paa-through queries are referring to that file as well as linked tables. But I...
4
by: NASAdude | last post by:
I'm working on distributing a database using VS Tools for Office, and am having trouble with linked images on forms/reports. The image objects have a default path\file set on the .Picture property,...
1
by: Nemisis | last post by:
Hi everyone, If i have 3 objects as below. objectA A_id A_Name B_id objectB
12
by: Doug | last post by:
Hi, I learned a little about the model view presenter pattern at a conference this last week and am experimenting with it. It's working pretty well but I have a question. I am trying to use...
7
by: semut | last post by:
Given that the string is of null-terminated type. What could be the possible causes (by experience) the string to have no null character (\0) and cause buffer overflow later. I know it is quite...
1
by: connor7777 | last post by:
Hi guys: We've been weeding out errors off of a java->c# project and have managed to redeem most of our code with the exception of one bug that we for some reason cannot pin down. The following...
1
by: cerise | last post by:
I'm using a Linked list stack with objects. I figured out the reason for my earlier problem (where I couldn't access "rank" and "suit" from one of the objects in my stack), and it was because my...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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
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
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?
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...

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.