Passing a Derived Object as a Reference Parameter

  • Thread starter Thread starter Bruce
  • Start date Start date
B

Bruce

How can I create a method that will take any object derived from a
base class as a reference parameter?

Something like this:

void DestroyObject(ref BaseObject obj)
{
obj.Dispose();
obj = null;
}

DestroyObject(ref derivedObject1);
DestroyObject(ref derivedObject2);
DestroyObject(ref derivedObject3);

Where derivedObject1, 2 and 3 are of a class type derived from
BaseObject?

Thanks....

--Bruce
 
Something like this:

void DestroyObject(ref BaseObject obj)
{
obj.Dispose();
obj = null;
}

DestroyObject(ref derivedObject1);
DestroyObject(ref derivedObject2);
DestroyObject(ref derivedObject3);

Yes, but I have to ask why you are explicitly disposing of objects instead
of letting the garbage collector do its work ? Or do your classes hold
references to Windows resources or file handles?

Joanna
 
Where derivedObject1, 2 and 3 are of a class type derived from
BaseObject?

BaseObject bo = derivedObject1;
DestroyObject(ref bo);

But I don't understand why you use a ref parameter. If you used a by
value parameter, you could pass in derivedObject1 directly.



Mattias
 
Dispose doesn't clean up the same resources as the GC. Its there to allow timely clean up of *non-memory* resources. The GC only cleans up memory. If an object implements IDisposable and you have ownership of the object then you should call dispose on it. You are confusing GC with finalization. Finalization is something that happens (hopefully) after the GC realizes a finalizable object is collectable.I say hopefully because it isn't guaranteed to run. You need to make your object finalizable (implement a C# destructor) if and only if you are managing non-managed resrouces like windows HANDLEs and the like. If you implement a finalizer then you should also implement IDisposable. However, there are good reasons to implement IDisposable even if you don;t imeplement a finalizere (for example you have memebers which themselves implement IDisposable).

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Something like this:

void DestroyObject(ref BaseObject obj)
{
obj.Dispose();
obj = null;
}

DestroyObject(ref derivedObject1);
DestroyObject(ref derivedObject2);
DestroyObject(ref derivedObject3);

Yes, but I have to ask why you are explicitly disposing of objects instead
of letting the garbage collector do its work ? Or do your classes hold
references to Windows resources or file handles?

Joanna
 
Mattias Sjögren said:
BaseObject bo = derivedObject1;
DestroyObject(ref bo);

But I don't understand why you use a ref parameter. If you used a by
value parameter, you could pass in derivedObject1 directly.

I suspect the point is that the variable will be set to null
afterwards. I can't say it's a pattern I really like though...
 
Dispose doesn't clean up the same resources as the GC.

That was what I was trying to say in the last sentence of my post; you just
said it better :-))

Joanna
 
Back
Top