By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,166 Members | 1,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,166 IT Pros & Developers. It's quick & easy.

Destructor for VB.NET class

P: n/a
Hi!

My application uses self made Class. When I create an instance of it,
class creates temporary file in constructor, and this file is in use as
long as instance of class is in use.

After instance of class will be removed, temporary needed file should be
deleted too. So how can I do it? It there destructor for VB.NET class
where this file can be deleted or something like that? I'm quite newbie
with VB.NET OOP programming, but I have studied UML. Maybe I have missed
something :)

I mean something like...

Dim cl As MyClass = New MyClass
'// Here temporary file is created
cl.DoSomething()
'// Here temporary file is in use
cl = Nothing
'// After previous line temporary file should be deleted to avoid next
line, but how ?

If (File.Exists(strTempFilePath)) Then File.Delete(strTempFilePath)

--
Thanks in advance!

Mika
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Mika M" <mahmik_nospam@removethis_luukku.com> schrieb:
My application uses self made Class. When I create an instance of it,
class creates temporary file in constructor, and this file is in use as
long as instance of class is in use.

After instance of class will be removed, temporary needed file should be
deleted too. So how can I do it? It there destructor for VB.NET class
where this file can be deleted or something like that?


Take a look at the chapters about the GC, finalization and the 'IDisposable'
('Dispose' method) interface. Feel free to ask if this doesn't answer your
questions...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2

P: n/a
If you use the If File.Exists method you may come across a file access
error. My advice would be to sleep the application a split second whilst you
close the file, destroy the stream writer or whatever you are using... &
then use the File.Exists method

Another way would be to do the above in dispose. Calling the garbage
collector will clean up the object reference, but not the temporary file

I hope this was of some help

Crouchie1998
BA (HONS) MCP MCSE
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.