set this object to null
-
I need some help on the following problem. I need to have a object set itself to null. Is this in any way possible?
-
I need some help on the following problem. I need to have a object set itself to null. Is this in any way possible?
I will explain myself a bit.
public class A { public void SetToNull() { //this object is null //but how do you do this } }
I know that I can set it like this below. But that is not what i want.
public class A { public void SetToNull() { //this object is null //but how do you do this } } public class B { public A a = new A(); public void SetToNull() { a = null; } }
It needs to be like this.
public class A { public void SetToNull() { //this object is null //but how do you do this } } public class B { public A a = new A(); public void SetToNull() { a.SetToNull(); } }
-
I need some help on the following problem. I need to have a object set itself to null. Is this in any way possible?
Matglas wrote:
I need to have a object set itself to null. Is this in any way possible?
If you mean what I think you mean then the answer is no. Do you mean something like this:
MyObject obj = new MyObject();
...
obj.DoSomeOperationThatSetsObjToNull();
Assert.IsNull(obj);The answer is no because the the object does not have any knowledge of who holds a reference to it. There may, however, be a way around it using weak references. The problem with that solution is that the point at which the references become null is non-deterministic. You could also implement
IDisposable
andDispose
of the internals of the object when you don't need it. However, anything accessing the object would have to be able to handle the fact that the object may exist, but it has already been disposed of. The above work arounds do not take into account the overall big picture. It would be better to know what it is you are trying to achieve. That way we could probably suggest a better solution.
Upcoming events: * Glasgow: Introduction to AJAX (2nd May), SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
-
I will explain myself a bit.
public class A { public void SetToNull() { //this object is null //but how do you do this } }
I know that I can set it like this below. But that is not what i want.
public class A { public void SetToNull() { //this object is null //but how do you do this } } public class B { public A a = new A(); public void SetToNull() { a = null; } }
It needs to be like this.
public class A { public void SetToNull() { //this object is null //but how do you do this } } public class B { public A a = new A(); public void SetToNull() { a.SetToNull(); } }
The answer is no because the the object does not have any knowledge of who holds a reference to it. There may, however, be a way around it using weak references. The problem with that solution is that the point at which the references become null is non-deterministic. You could also implement
IDisposable
andDispose
of the internals of the object when you don't need it. However, anything accessing the object would have to be able to handle the fact that the object may exist, but it has already been disposed of. The above work arounds do not take into account the overall big picture. It would be better to know what it is you are trying to achieve. That way we could probably suggest a better solution.
Upcoming events: * Glasgow: Introduction to AJAX (2nd May), SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
-
I will explain myself a bit.
public class A { public void SetToNull() { //this object is null //but how do you do this } }
I know that I can set it like this below. But that is not what i want.
public class A { public void SetToNull() { //this object is null //but how do you do this } } public class B { public A a = new A(); public void SetToNull() { a = null; } }
It needs to be like this.
public class A { public void SetToNull() { //this object is null //but how do you do this } } public class B { public A a = new A(); public void SetToNull() { a.SetToNull(); } }
Hi, an object can not set itself to null. an object is a piece of code+data, and what you use to access an object really is a reference (often also called object, but that is not strictly correct). Now an object cannot access its reference(s) in general, since an object is not aware of its references. In special cases you can do something, but that requires you get access to the reference, typically by using the ref keyword, as in:
Nullable A=new Nullable(); a.NullifyMyRef(ref a); a.Something(); // NullPointerException !
class Nullable {
public void NullifyMyRef(object ref obj) {
obj=null;
}
}But what is the use of all this ??? Most scenarios I can come up with are a result of bad OO practices. The one good use I could come up with is something like the Dispose() method that may want to nullify its own reference; the fact that it does not proves to me once more it can not be done. :)
Luc Pattyn [My Articles]
-
Matglas wrote:
I need to have a object set itself to null. Is this in any way possible?
If you mean what I think you mean then the answer is no. Do you mean something like this:
MyObject obj = new MyObject();
...
obj.DoSomeOperationThatSetsObjToNull();
Assert.IsNull(obj);The answer is no because the the object does not have any knowledge of who holds a reference to it. There may, however, be a way around it using weak references. The problem with that solution is that the point at which the references become null is non-deterministic. You could also implement
IDisposable
andDispose
of the internals of the object when you don't need it. However, anything accessing the object would have to be able to handle the fact that the object may exist, but it has already been disposed of. The above work arounds do not take into account the overall big picture. It would be better to know what it is you are trying to achieve. That way we could probably suggest a better solution.
Upcoming events: * Glasgow: Introduction to AJAX (2nd May), SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
Ok I hope my english is good enough to explain what I want to achieve. I have a deserialized object from a xml file. This object is shown in a treeview to edit the content. I want to have the option to remove a object by clicking on it and getting it context menu. This context menu is unique for every object. I get it by a GetContextmenu method that is implementated by a Interface. The eventhandlers for each menuitem is set to a method in that object. I hope the situation is clear. Otherwise i have to make some code to show it.
-
Ok I hope my english is good enough to explain what I want to achieve. I have a deserialized object from a xml file. This object is shown in a treeview to edit the content. I want to have the option to remove a object by clicking on it and getting it context menu. This context menu is unique for every object. I get it by a GetContextmenu method that is implementated by a Interface. The eventhandlers for each menuitem is set to a method in that object. I hope the situation is clear. Otherwise i have to make some code to show it.
I think what you need is a ToParent reference for each object, and to have the parent set the specific child to null.
-- CleaKO The sad part about this instance is that none of the users ever said anything [about the problem]. Pete O`Hanlon Doesn't that just tell you everything you need to know about users?
-
Matglas wrote:
I need to have a object set itself to null. Is this in any way possible?
If you mean what I think you mean then the answer is no. Do you mean something like this:
MyObject obj = new MyObject();
...
obj.DoSomeOperationThatSetsObjToNull();
Assert.IsNull(obj);The answer is no because the the object does not have any knowledge of who holds a reference to it. There may, however, be a way around it using weak references. The problem with that solution is that the point at which the references become null is non-deterministic. You could also implement
IDisposable
andDispose
of the internals of the object when you don't need it. However, anything accessing the object would have to be able to handle the fact that the object may exist, but it has already been disposed of. The above work arounds do not take into account the overall big picture. It would be better to know what it is you are trying to achieve. That way we could probably suggest a better solution.
Upcoming events: * Glasgow: Introduction to AJAX (2nd May), SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
This is the object situation. I copyed some code, to show you. The
DeletePage()
needs to delete the page from thePages
property in theDocument
.public class Document : ITreeView { public Document() { } private PageCollection _pages = new PageCollection(); [XmlElement(ElementName = "page")] public PageCollection Pages { get { return _pages; } set { _pages = value; } } #region public methods public Document Deserialize(string file) { //.. } public void Serialize(string file, Document doc) { //... } #endregion #region ITreeView Members public ContextMenu GetContextMenu() { ContextMenu docContext = new ContextMenu(); //... return docContext; } public ObjectTreeNode GetTreeNode() { return new ObjectTreeNode(this, "document", 0, 0); } #endregion } public class Page : ITreeView { public Page() { } #region properties //... #endregion #region ITreeView Members public ObjectTreeNode GetTreeNode() { return new ObjectTreeNode(this, "page " + _pagenumber, 0, 0); } public System.Windows.Forms.ContextMenu GetContextMenu() { ContextMenu pageContext = new ContextMenu(); MenuItem delItem = new MenuItem("Delete", new EventHandler(DeletePage)); pageContext.MenuItems.Add(delItem); return pageContext; } public void DeletePage(object sender, EventArgs e) { //delete this page } #endregion }
-
I think what you need is a ToParent reference for each object, and to have the parent set the specific child to null.
-- CleaKO The sad part about this instance is that none of the users ever said anything [about the problem]. Pete O`Hanlon Doesn't that just tell you everything you need to know about users?