Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / July 2003

Tip: Looking for answers? Try searching our database.

How to enforce Usage Rule

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Calvin - 14 Jul 2003 18:43 GMT
I have a class, has a start(), end() method.

I want to make sure that start() and end() should be
always paired called in the same codeblock or function,
i.e.:
try
{
o.Start();
...
}
Finally
{
o.End();
}

I could write the documentation, but is there anyway to
enforce this rule by compiler or runtime so it will
generate Compiling Error or Runtime Error if they are not
called in Pair?

-----Miss the Destructor in C++

Calvin
Jay B. Harlow [MVP - Outlook] - 14 Jul 2003 20:06 GMT
Calvin,
Unfortunately no. The closest you will get is to have end, renamed Dispose
from the IDisposable interface, then recommend to your clients that they use
the 'using' statement in their code, which will automatically call the
Dispose method.

   using (Calvin o = new Calvin())
   {
       ...
   }

You could set some flag in your start method, that get reset in your end
method, that subsequently in the Finalize method it gets flagged, however
this would be way too late...

Hope this helps
Jay

> I have a class, has a start(), end() method.
>
[quoted text clipped - 19 lines]
>
> Calvin
Calvin - 14 Jul 2003 22:26 GMT
Thanks for the reply.
I am just not sure where the technology is going. The
destructor in C++ is so handy, and they just take it away.
And gone all the goods of C++ compiler. :-)

The attributes is great if they are truly extensible. Will
it be great if we can specify some local variable as:

[RunDestructor]
MyClass myobj = new MyClass();

?? :-)

.Net use Garbage Collector to take care memory problem,
but for some coputer resources, it's so important to
release immediately, or else it will crush the
application. And we really should not depend on the
programmers to call:
obj.Close();
manually.

Calvin

>-----Original Message-----
>Calvin,
[quoted text clipped - 40 lines]
>
>.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.