Problems testing refferences of labels in a control array.

Hi All,

I have a two dimentional array of type lable. I am using this array to create a grid (I eventualy gave up on the datagrid as it just wouldn't do the stuff I wanted without an auful lot of complex customisation). During the grids life time, rows will be added and removed.

My problem is this:
Problem 1: When the grid is first created I need to be able to test each cell in the array to see if I need to create (call new) on the lable and add it it too the controls on the user control.

My current solution is this (which I am not happy with due to problem 2):
Try to set a property and if the "refference not set to an instance of an object" error is thrown, catch it and create the lable.

Problem 2: Later on after rows have been added and removed and added again some rows (the ones deleted I think) are not throwing the error (or any error) and I cant therefore add them back on to the user control. I seem to be able to set there properties but they just can't be seen on the control.

I remove the labels with the following code:


Me.Controls.Remove(Labels(colIndex, i))
Labels(colIndex, i).Dispose()

Hope this makes sense,

Danny

[1460 byte] By [CodeScribler] at [2008-2-22]
# 1
Hi,
Calling Dispose won't remove your label in your array. And AFAIK, calling dispose() doesn't mean that your object is already removed on the memory. Your object would be cleaned up when the GC(Garbage Collector) processes it (After calling Dispose()) and removes it in the memory...

IMO, you could try removing your label in the array and calling a dispose() on it. Just let the GC cleanup the mess...
cheers,
Paul June A. Domag

PaulDomag at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...