|
George_George wrote: Implement the dispose design pattern on a base type that commonly has derived types that hold onto resources, even if the base type does not.
You will be implementing the IDisposable on the base class and allowing all child classes to override the Dispose(bool disposing) method.
Assume you are creating a windows form that has extra functionalities than the classical windows form. So you will create a new class which inherits from "Form" class. Here you won't implement IDisposable again because the base class already implemented it. You can override the dispose method in your class and clan up the resources created by your class. Call base classes dispose to release it's resources at last.
|
|
|
|
|
Thanks N a v a n e e t h,
I want to confirm with you, the Dispose() method from IDisposable interface is not virtual, and the customized method Dispose (bool) is virtual and we should override Dispose (bool) in Dispose pattern, right?
regards,
George
|
|
|
|
|
Yes. Dispose(bool) can be overridden in the subclass.
|
|
|
|
|
Thanks N a v a n e e t h,
But why "do not implement a Finalize method on the base type" as mentioned in MSDN? Any issues if we implement Finalize on both base, derived and most derived types?
regards,
George
|
|
|
|
|
You don't need to re-write Dipose or Finalizer in the sub classes, it is already implemented in the base class. Assume you have the following class structure
class MyBase : IDisposable
{
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing) { }
disposed = true;
}
}
~MyBase()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
class MyDerived : MyBase
{
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing) { }
disposed = true;
}
base.Dispose(disposing);
}
} You have not re-implemented the IDisposable and finalizer again in the MyDerived class. So if you forget to call MyDerived classes dispose after using it's instance, GC will call MyBase classes finalizer and it will call the overiden method here in the derived class.
|
|
|
|
|
Thanks N a v a n e e t h,
I read again the situation described in the quoted MSDN article. Still a little confused,
1.
Does it mean when in the situation of when both base class and derived class wraps native resource, we only need to put Finalize method to the derived class?
2.
Or only the derived class wraps native resource, and hence we only need to implement Finalize in Derived class?
regards,
George
|
|
|
|
|
George_George wrote: Does it mean when in the situation of when both base class and derived class wraps native resource, we only need to put Finalize method to the derived class?
No. You don't write finalize method to the derived class. You can write it for base class. In derived class just override the Dispose(bool) .
|
|
|
|
|
Thanks for your clarification, N a v a n e e t h!
regards,
George
|
|
|
|
|
George,
Take a look at this article[^] for more details, specifically the "Complete exmample" towards the end.
That being said, you should only implement a finalizer when absolutely necessary as it adds a complexity to your type and isn't necessary in most cases.
Scott.
—In just two days, tomorrow will be yesterday.
—Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
[ Forum Guidelines] [ Articles] [ Blog]
|
|
|
|
|
Thanks Scott,
I like it.
regards,
George
|
|
|
|
|
You're welcome.
Scott.
—In just two days, tomorrow will be yesterday.
—Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
[ Forum Guidelines] [ Articles] [ Blog]
|
|
|
|
|
One more comment, Scott.
1.
C# class like StreamWriter, which wraps a native resource -- file handle. Is StreamWriter managed resource or unmanaged resource?
2.
If my class has a StreamWriter member, and in Finalizer, I do not close it, and I rely on the Finalizer method of StreamWriter itself to close the native resource file handle. In this case, will there be any resource leak?
regards,
George
|
|
|
|
|
Classes like StreamWriter are managed types. They wrap an unmanaged resource but also have the responsibility to clean up after themselves and dispose of those resources properly.
George_George wrote: If my class has a StreamWriter member, and in Finalizer, I do not close it, and I rely on the Finalizer method of StreamWriter itself to close the native resource file handle. In this case, will there be any resource leak?
That's a good question. I believe, technically it isn't a resource leak, but you should probably consider it to be one. The idea here is that you want to acquire the resource as late as possible and free/release/dispose of it as early as possible. Relying on the finalizer to clean up the StreamWriter violates that concept and releases as late as possible.
Scott.
—In just two days, tomorrow will be yesterday.
—Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
[ Forum Guidelines] [ Articles] [ Blog]
|
|
|
|
|
Cool, Scott!
Question answered.
regards,
George
|
|
|
|
|
VC++ have an inbuild function named writeprofilestring( ) to write contents in an INI file.
similarly do we have inbuild function to read/write values in INI file (User created) in Web application C # because doing it via file reading and writing may fetch more time .
for instance the format has to be like below
[settings]
Image=0
Thanks in Advance
Archana
|
|
|
|
|
|
There's an article here on CP that includes a class for reading .ini files, .config files, and xml files in .net
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Thanks for your reply .
I went through the article. It is using API function. How can I use it in webapplication. Can you please tell me
Thanks in Advance
Archana
|
|
|
|
|
Dear All,
When I tried to call method from VC++ component thru RCW concepts, I'm getting "Null Reference Exception"Error. Can anyone assist me on this?
|
|
|
|
|
Did you read this? All I can tell from whatever you've told is that you're not instantiating an object where you should be.
Cheers,
Vikram.
The hands that help are holier than the lips that pray.
|
|
|
|
|
I had instanciated it. However, still its remain same. Kindly, provide better remedy
|
|
|
|
|
With the information that you've provided, that's just not possible. How did you create this wrapper??
|
|
|
|
|
I had created Wrapper from Dotnet by using tlbimp. I used this command to create Wrapper from command prompt of Dotnet. The command is "tlbimp <vc++dll file="" location=""> / out: <rcw class="" location="" where="" you="" want="" to="" create="" it=""> /verdose"
If you execute this command, this would create RCW class for specified VC++ dll. Why did I created it in worng way? Is it so, Kindly provide betterment of it.
|
|
|
|
|
That's doing it the hard way, but it works. Is this a COM-based .DLL (registered using REGSVR32) or a library .DLL??
COM-based .DLL's you can add a reference to and Visual Studio will automatically generate the wrapper for you, without using the command line to do it.
|
|
|
|
|
Is it possible to cancel the deletion of a particular row in a datagrid view when user presses a delete key?
|
|
|
|