Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi

 

Is it possible for an object to destroy itself?

 

In the constructor in my class i have a method call that builds the object. But if anything goes wrong here I don't want the object to be created. How do I acomplish that?

  • *Gurus*
Posted
You can't, in the constructor. If there is a chance the object creation might fail, use a static public function to instantiate the class and make the constructor private, like the Graphics class. You can then return null (Nothing in VB) if it fails.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

Thanks divil, nice solution...did it in another way though. If anyone's interested;

 

If the object doesn't succed in the creation it throws an exception

 

try

myObject obj = new object()

'creation ok

catch

'no object was created

 

end try

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...