checking to see if an object has been instantiated...

B

Brad Pears

I want to check to see if a particular object exists using vb.net 2005.

I have a class called clsContract. When the user clicks the 'New Contract'
button, I want to check to see if a clsContract object already exists and if
so what the state of that object is before doing anything else.

I thought I could use ...

if IsNothing(clsContract) then
'somc code
else
' some code
endif

BUT

"clsContract" is underlined and the error says
"clsContract is a type and can not be used as an expression"

How do you check to see if an object exists?

Thanks, Brad
 
R

Rory Becker

How do you check to see if an object exists?

You have to check a variable to see whether it contains an object of the
right type

Thus
-------------------------------------------------------------
Dim X as clsContract

If X is nothing then
MessageBox.Show("There is No Object here")
Endif
X = New clsContract

If Not (X is nothing) then
MessageBox.Show("There is an Object here")
Endif
-------------------------------------------------------------


However the variable you test can be a shared variable within the class.
This is an example of the 'singleton' pattern.

Thus
-------------------------------------------------------------
Public Class clsContract
Private Sub New()
End Sub
Private Shared mSingleton as clsClass
Public Shared Function GetSingleton as clsContract
If mSingleton is nothing then
mSingleton = New clsContract
Endif
return mSingleton
End function
Public Shared function Exists() as boolean
return not (mSingleton is nothing)
End function
End Class
 
T

Tom Shelton

I want to check to see if a particular object exists using vb.net 2005.

I have a class called clsContract. When the user clicks the 'New Contract'
button, I want to check to see if a clsContract object already exists and if
so what the state of that object is before doing anything else.

I thought I could use ...

if IsNothing(clsContract) then
'somc code
else
' some code
endif

BUT

"clsContract" is underlined and the error says
"clsContract is a type and can not be used as an expression"

How do you check to see if an object exists?

Thanks, Brad


Brad - your getting the error because clsContract is a type - not an
object. There is a differentiation between an Object and Type. Type
defines a template for an object - what does the object look like.
When we say object, we mean an actual instantiation of the type. In
other words, it is taking up actual phyiscal memory... IsNothing
doesn't work, because all it's telling you is if a variable is
actually refering to a live object.

I'm a little unclear on what your trying to accomplish here... It
appears your trying to see if there is an instantiated contract object
already, but what the actual goal of this test is would go a long way
in helping you find a solution to your problem.
 
Z

zacks

I want to check to see if a particular object exists using vb.net 2005.

I have a class called clsContract. When the user clicks the 'New Contract'
button, I want to check to see if a clsContract object already exists and if
so what the state of that object is before doing anything else.

I thought I could use ...

if IsNothing(clsContract) then
'somc code
else
' some code
endif

BUT

"clsContract" is underlined and the error says
"clsContract is a type and can not be used as an expression"

How do you check to see if an object exists?

Thanks, Brad

Sounds like you don't know how classes and objects work. You define a
class in a class module. Then, in the code in usually some other
class, you declare an instance of that object by declaring a variable
and instantiating it. Let's use your clsContract class.

Dim objContract as clsContract ' defines the variable

objContract = new clsContract ' creates an instance of the clsContract
class that is referenced with the objContract variable

After the objContract variable has been instantiated with the new
keyword, then that object is no longer "nothing".

If objContract Is Nothing Then
' do something if it is nothing
Else
' do something else if it is not nothing
End If
 
B

Brad Pears

Hi there, yes I understand that part of it... I was using the wrong syntax.
What I was trying to accomplish was to see if the user had already
instantiated the contract class object - previous to clicking the 'New'
button. If they had instantiated it already, then I want to ask them if they
want to save the chagnes (if any ) or save the contract they are currently
working on before clearing the form etc... etc... I didn't realize that to
see if an instance of a class (an object) already exists I had to dim a
variable as that object type (my contract class). I thought there may have
been some code that I could use where I just pass the name of my object and
a true of false was returned - depending on if the object existed or not...
I think the first response I received on this one actually lists the correct
syntax for me...

Thanks for your help!!!
 
B

Brad Pears

Perfect... That's what I was missing...

I'll get the hang of this yet!

I read a post earlier about using a public "Exists" function which indicates
whether or not an object would exist... This function woul dbe created as a
method on the contract class.

Are you familiar with how this type of thing would be implemented in the
class and then actually used in code somewhere??

What would the best way be to implement this and what would the correct
syntax for usage be?

Thanks, Brad
 
B

Brad Pears

Hi there... I am trying out your code but I must obviously be missing
something. I have code on the click event of a "New Contract" button that
checks for the existence of a contract class object (clsContract) as you
indicated in your message. If clsContract = nothing, I create the object by
issuing the "new" keyword (x = new clsContract). Nowhere in this code am I
destroying the object.

When the "New Contract" button is clicked again, I am expecting my newly
instantiated clsContract object to exist already and therefore I should get
the message that "a contract already exists" (this is what I have my msgbox
indicating when clsContract is NOT = nothing)

BUT, for some strange reason, when this code runs again, the object has been
disposed of somewhere along the line and is = nothing again - so I just keep
creating a new object everytime isntead of being able to use the old one!!

I am obviously not grasping object persistence or something... What am I
missing?

Help!

Thanks, Brad
PS, my clsContract class is defined as public.
 
G

Guest

If your dim statement is in the click event handler, then the object is local
to that handler and is disposed at the end of the routine. It sounds like
you are expecting the object to be retained while the form is alive. If this
is correct, then declare it at near the top of your form class with a
declaration of:

private clsContract as Contract

You will now have an instance available anywhere in the form's code.

Brad Pears said:
Hi there... I am trying out your code but I must obviously be missing
something. I have code on the click event of a "New Contract" button that
checks for the existence of a contract class object (clsContract) as you
indicated in your message. If clsContract = nothing, I create the object by
issuing the "new" keyword (x = new clsContract). Nowhere in this code am I
destroying the object.

When the "New Contract" button is clicked again, I am expecting my newly
instantiated clsContract object to exist already and therefore I should get
the message that "a contract already exists" (this is what I have my msgbox
indicating when clsContract is NOT = nothing)

BUT, for some strange reason, when this code runs again, the object has been
disposed of somewhere along the line and is = nothing again - so I just keep
creating a new object everytime isntead of being able to use the old one!!

I am obviously not grasping object persistence or something... What am I
missing?

Help!

Thanks, Brad
PS, my clsContract class is defined as public.
 
R

Rory Becker

Yup... what he said... :)

However it also sounds like you might actively want to destroy an object.

This is effectively done by ensuring that there are no references to it.
once this is the case, the garbage collector is free to clear up the memory
used by the object.

Consider however that if you expose this object beyond your control with
a property, then you can no longer be sure there are no references to it
and therefore you can not be sure if it has been destroyed.

That is without implementing IDispose.

That however is another kettle of fish. feel free to ask about this if it
is something you would like to know about.

Also you could both expose your object and then , (some time later) set all
your references to nothing. At which point your own GetSingleton method would
decide it was valid to allow the creation of another item.

Again "Dispose" might help you here

I hope some of this helps.
 

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