[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

How to enforce Usage Rule

Calvin Guo

7/14/2003 5:43:00 PM

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
2 Answers

Jay B. Harlow [MVP - Outlook]

7/14/2003 7:06:00 PM

0

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

"Calvin" <szguoxz@hotmail.com> wrote in message
news:004e01c34a2f$6744db30$a501280a@phx.gbl...
> 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


Calvin Guo

7/14/2003 9:26:00 PM

0

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,
>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
>
>"Calvin" <szguoxz@hotmail.com> wrote in message
>news:004e01c34a2f$6744db30$a501280a@phx.gbl...
>> 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
>
>
>.
>