Form Instances

G

Guest

Hi,
I'm using VS.NET 2003. I've a small problem using the OOP concepts. I want
to know before creating an instance of a class, to check whether any
instances earlier created exists.

Now for example. I'm calling a method to create a Instance of Form, and
Before creating an object instance of the form, I want to make sure that
there isn't any object instances created earlier which still exists and want
to know how to destroy that previous Instance.

example:
(assume that TForm is a base class)

Dim objForm1 as New TForm
If not any previous object instances of TForm exists then
objForm1.ShowDialog
else
Destroy the previous Instance

How can I write this logic as VB.NET code?????
 
C

Cor Ligthert [MVP]

Hifini,

Dim objForm1 as New TForm
If you do this, than objForm1 is absolute a new object.

There can be another object with the same name, however it is not necessary
that it is the same, by instance

Sub one
dim objForm1 as New TForm1
end sub

Sub two
dim objForm1 as New TForm2
end sub

This are two complete different objects.

Another question is if you want to reshow an already opened form object. For
that is the not in the standard intelisence showed IsDisposed

if Not myform.IsDisposed then
myform.show
else
dim myform as new WhatEverFormType
myform.show
End if

I hope this helps,

Cor


I hope that this gives an idea

Cor
 
G

Guest

Hi Cor,
Thanx for your valuable information. However I got another question. Now
after using the form object I would like to distroy it to save memory. In
what Form event can I call:

objForm1.dispose or what ever method which destroys the form object instance?

Thanx,

--
Mohammed Hifni Shahzard Nazeer,
JB Securities Pvt. Ltd.,
Colombo.
 
C

Cor Ligthert [MVP]

Hifni,

It depends forever where you have instanced your form object.

Did you do that private or global, than at least the address lives forever
as long as the class where it is in is lives. A form has a bunch of controls
referencing to it.

Therefore don't worry if not really necessary about the memory used by a
form. The GC will release it, when it is sure that it will not be reused
because there is no reference anymore to it. What can mean that there will
be a lot of releasinges before that.

This is exactly why it is called managed code.

I hope this helps,

Cor
 

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