C# question: How do you delete a Grid object

  • Thread starter Thread starter dongarbage
  • Start date Start date
D

dongarbage

I'm building an application (WPF) and I have a Panel in the
application which will be populated dynamically with objects of the
type Grid, TextBox, etc. For simplicity, let's say I'm just adding and
deleting Grid objects. I can add the objects easily by creating Grid
objects. The problem I have is that I don't know how to delete the
objects. How do you delete a Grid object? I'd prefer to not have to
turn the "visible" switch off and wait for the garbage collector to
come around.

Any ideas?
 
Turning the visible switch will not cause the GC to collect the object.
It will just set the visibility of the control to false, as the reference to
the object will still be held by the parent object.

I also don't know how you are using a Panel, and not a derived class, as
Panel is marked abstract.

Regardless, any class that derives from Panel that you are using has a
Children property which has all the child controls that are on the panel.
Pass your Grid to the Remove method on the UIElementCollection that is
exposed through the Children property and it will be removed.
 
I'm building an application (WPF) and I have a Panel in the
application which will be populated dynamically with objects of the
type Grid, TextBox, etc. For simplicity, let's say I'm just adding and
deleting Grid objects. I can add the objects easily by creating Grid
objects. The problem I have is that I don't know how to delete the
objects.
How do you delete a Grid object? I'd prefer to not have to
turn the "visible" switch off and wait for the garbage collector to
come around.

You always have to wait for the garbage collector to come around; you can't
deallocate memory explicitly in .NET .. in any case, making an object not
visible doesn't mark it for garbage collection, it makes it not visible

Can't say I've ever done what you're doing but can't you just grid.Dispose()
it?
 
Liz said:
You always have to wait for the garbage collector to come around;

That's not entirely true. You can use stackalloc to allocate memory on
the stack (away from the garbage collector).

It's good if you frequently need to allocate moderately large arrays of
objects. Garbage collection hurts in that situation.

Alun Harford
 
That's not entirely true. You can use stackalloc to allocate memory on the
stack (away from the garbage collector).

It's good if you frequently need to allocate moderately large arrays of
objects. Garbage collection hurts in that situation.

glad you mentioned stackalloc, Alun ... it is useful and too often ignored
(or so it seems); so I'll rephrase and say you always have to wait for the
GC where the GC is capable of doing anything ... certainly the OP has no
choice but to wait for it with respect to his dynamically instantiated grids
 
I kind of don't see the point of mentioning this, as you can't allocate
pointers to reference type arrays using stackalloc. You can only use value
types.
 
Controls in WPF do not implement IDisposable, so this isn't a feasible
solution.
 
Nicholas said:
I kind of don't see the point of mentioning this, as you can't
allocate pointers to reference type arrays using stackalloc. You can
only use value types.

I just didn't want people to think .NET/C# is a inherrently slow. In
situations where GC is very bad, you *can* avoid it (if you really must).

Alun Harford
 
Alun,

That's the thing, unless you are going to use value types for
everything, then no, you are not going to be able to avoid GC. Even in
IDisposable implementations, you might be able to eliminate the chance of
^finalization^ occuring, the object itself is still subject to garbage
collection. You can't avoid it for reference types.
 
Controls in WPF do not implement IDisposable, so this isn't a feasible
solution.


didn't know that ... so what do we do in WPF? grid = null; ??


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Liz said:
You always have to wait for the garbage collector to come around; you
can't deallocate memory explicitly in .NET .. in any case, making an
object not visible doesn't mark it for garbage collection, it makes it
not visible

Can't say I've ever done what you're doing but can't you just
grid.Dispose() it?
 
Turning the visible switch will not cause the GC to collect the object.
It will just set the visibility of the control to false, as the reference to
the object will still be held by the parent object.

I also don't know how you are using a Panel, and not a derived class, as
Panel is marked abstract.

Regardless, any class that derives from Panel that you are using has a
Children property which has all the child controls that are on the panel.
Pass your Grid to the Remove method on the UIElementCollection that is
exposed through the Children property and it will be removed.

Thanks,

<parent Control>.Children.Remove(UIElement) did what I wanted.

Regards,
D
 
See my original response for the answer.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Liz said:
Controls in WPF do not implement IDisposable, so this isn't a feasible
solution.


didn't know that ... so what do we do in WPF? grid = null; ??


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Liz said:
I'm building an application (WPF) and I have a Panel in the
application which will be populated dynamically with objects of the
type Grid, TextBox, etc. For simplicity, let's say I'm just adding and
deleting Grid objects. I can add the objects easily by creating Grid
objects. The problem I have is that I don't know how to delete the
objects.

How do you delete a Grid object? I'd prefer to not have to
turn the "visible" switch off and wait for the garbage collector to
come around.

You always have to wait for the garbage collector to come around; you
can't deallocate memory explicitly in .NET .. in any case, making an
object not visible doesn't mark it for garbage collection, it makes it
not visible

Can't say I've ever done what you're doing but can't you just
grid.Dispose() it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top