473,698 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_NextSiblingOb ject;
MyObject m_PreviousSibli ngObject;
MyObject m_ParentObject;
//I then have a series of Get and Set methods for
setting the above objects
Public void AddChildObject( MyObject childObjectToAd d)
{
childObjectToAd d.SetParentObje ct(this);

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

While (tempObject.Get NextSiblingObje ct() != null)
{
tempObject = tempObject.GetN extSiblingObjec t();
}


tempObject.SetN extSiblingObjec t(childObjectTo Add);

childObjectToAd d.SetPreviousSi blingObject(tem pObject);
}

}

Public void RemoveAllChildO bjects()
{
MyObject tempObject = this.GetChildOb ject();

While(tempObjec t != null)
{
MyObject nextObject = tempObject.GetN extSiblingObjec t();

//*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 RemoveAllChildO bjects that I am trying to duplicate in C#

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

While(tempObjec t)
{
MyObject* nextObject = tempObject->GetNextSibling Object();
SAFE_DELETE(tem pObject);
tempObject = nextObject;
}
}
Jun 27 '08 #1
1 1961

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
3926
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 "browse" it). So I expected that when I run this program, I get both c1.A and c2.A pointing to the same address, and changing c1.A means that also c2.A changes too. ----- BEGIN example CODE -----------
18
3050
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 frequently, I thought it'd be a better idea to manage additional containers which are initialized with pointers to the objects in the primary containers and sort those (only pointers have to be copied around then). However, that also means if I...
11
10273
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 don't understand what will happen when I distribute my front-end to some other PC. How will Access know where is File DSN ? Is there any way that I set the connection programmatically. For example, some login form with neccessary informations ?...
4
12841
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, which becomes invalid when the user installs the database (ie: c:\devpath\ becomes c:\program files\mydbapp, but the .Picture property still points to c:\devpath\) The problem is that I don't *know* what installation path the user will select,...
1
1552
by: Nemisis | last post by:
Hi everyone, If i have 3 objects as below. objectA A_id A_Name B_id objectB
12
7496
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 it to reset info in a combo box. Below is some sample code for my view interface and the presenter: public interface IDevToolView
7
2553
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 broad, just like to find out the causes as much as possible so that I could impose stricter checking toward my codes. note: I could not use std::string cause it will require a total rewrite.
1
1725
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 is an implementation of Linked List class for any item "E" (that also implements IMatchable for itself). It defines "LinkedList" as a recursive datatype with no separate "Cell" or "Node" class, much the same as say for instance ML's Programming...
1
1453
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 object didn't have the attributes "rank" and "suit". Turns out my earlier cards were practically just strings. Can anyone help me create a whole deck of cards represented by 52 objects in a linked list stack with the attributes "suit" and "rank"? This...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8901
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7739
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5862
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.