formArray close / dispose issue

M

Miro

I have created myself an issue, and perhaps my idea wasn't as bright as I
thought it was originally.
If someone can help me figure out what the correct train of thought should
be - or if there is a solution to this one.

I created a dummy mdi app.
In the main form ( frmMainScreen ) I created a variable as such:
<written in notepad to trim everything down>

Public Shared frmFormsOpen() As frmMyTestForm

Now, in a totally seperate sub form opend from the main form I do this on a
button click:

LengthOfGames = (frmMainScreen.frmFormsOpen.Length - 1)
ReDim Preserve frmMainScreen.frmFormsOpen(LengthOfGames)

frmMainScreen.frmFormsOpen(LengthOfGames) = New frmMyTestForm
frmMainScreen.frmFormsOpen(LengthOfGames).MdiParent = Me.MdiParent
'Remember that this form is also an mdi child of the main form.
'Set my own property for later use
frmMainScreen.frmFormsOpen(LengthOfGames).GameID = anIntegerValue

frmMainScreen.frmFormsOpen(LengthOfGames).Show()

'Works Great
So in other code on other child forms forms I can do stuff like this:

For Each SearchForm As frmMyTestForm In
frmMainScreen.frmFormsOpen
If SearchForm.GameID = mytestID Then 'mytestID is set to a
value from something else
SearchForm.BringToFront()
Exit For
End If
Next


My problem is this:

where do i actually remove the array element when the form is closed. I do
not want it to stay in the array.
I do not think I can do it in the _FormClosed of the
frmMainScreen.frmFormsOpen(LengthOfGames)._FormClosed
because I am actually in the form itself still - so I am assuming I cannot
kill the object when I am still using the object.
( im trying to kill myself from myself ).

That is my issue I have created myself and I was wondering if someone can
point me in the right direction on how to get around this issue.

Thanks,

Miro
 
A

Armin Zingler

Miro said:
where do i actually remove the array element when the form is
closed. I do not want it to stay in the array.

Do yourself a favor and use a List(Of frmMyTestForm) instead of the array.
(which VB version?) Safes you from item shifting and redimming.
I do not think I can do it in the _FormClosed of the
frmMainScreen.frmFormsOpen(LengthOfGames)._FormClosed
because I am actually in the form itself still - so I am assuming I
cannot kill the object when I am still using the object.
( im trying to kill myself from myself ).

You don't kill anything by removing one reference. You can safely remove the
item from the list in the FormClosed event. The object on which the current
method is executed is never destroyed.


Armin
 
M

Miro

Its vb 2008

Thank you for the post.
I was under the impression - since I am 'sitting' in the object still during
_FormClose then I cannot Remove my object from underneith my feet.

Im assuming then by your statement
The object on which the current method is executed is never destroyed.
That once I leave the form and it does close - it will be destroyed in the
background - I dont need to run anything else?

I will google and search up "List" to use a list instead of an array.

Thank you,

Miro
 
A

Armin Zingler

Miro said:
Im assuming then by your statement
That once I leave the form and it does close - it will be destroyed in the
background - I dont need to run anything else?

Yes, nothing else is required.
I will google and search up "List" to use a list instead of an array.

Not required to google - look in the object browser. It's
System.Collections.Generic.List(Of T)

see also:
http://msdn.microsoft.com/en-us/library/41107z8a.aspx


Armin
 
J

Jack Jackson

You are confusing removing a reference to an object from a list with
destruction of the object.

An object can have many references. Removing those references does
nothing other than, when all references have been removed, make the
object available for garbage collection. It does not cause the object
to be destroyed.

And as Armin said, please don't use arrays for this. List (Of T) is
much better.
 
M

Miro

Thank you -
I have changed it to use the List(Of T) and it works great and 10x easier.

I used an array because of my old dos day programming - arrays is all we
had.

Miro
 
M

Miro

I see,

I was the understanding that if i say
dim bla as new form1

then I was under the impression the object instantiated and the object
exists in bla which was dim'd here.

To 'remove' it from this 'dim' would be pulling the rug from under me.

As I understand your comment - the object "REFERENCE" is dim'd here, but the
actaul object exists somewhere else, so I can
"undim" my bla without worries. ( if undim was a word ) but basically
remove it from bla.

Miro
 
J

Jack Jackson

Making a variable no longer point at an object does not "pull the
rug". When all references to an object go away, the object becomes
eligible to be garbage collected at some time in the future.

Forms are a little different in that the framework keeps track of them
and has a reference to all open forms (Application.OpenForms), so even
if you clear your reference there is still one being held by the
framework.

For example:

Public Class MyClass
Public A As Integer = 0
End Class

Dim x As New MyClass ' There is one reference to an instance
' of MyClass
Dim y as MyClass = x ' Now there are two references
' to the instance
x = Nothing ' Now there is one reference (y)
y = Nothing ' Now there are no references and
' the instance is eligible for garbage
' collection.
 
M

Miro

Thank you - excellent example for me to understand. -makes perfect sense
now.

I appreciate your time.

Thank you again,

Miro
 

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

Top