473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Passing a Derived Object as a Reference Parameter


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

Nov 17 '05 #1
9 2057
"Bruce" <bv***************@mailblocks.com > a écrit dans le message de news:
lr********************************@4ax.com...
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

--
Joanna Carter
Consultant Software Engineer
Nov 17 '05 #2
"Bruce" <bv***************@mailblocks.com > a écrit dans le message de news:
lr********************************@4ax.com...
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

--
Joanna Carter
Consultant Software Engineer
Nov 17 '05 #3
>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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #4
>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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #5
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

"Bruce" <bv***************@mailblocks.com > a ?crit dans le message de news:
lr********************************@4ax.com...
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
Nov 17 '05 #6
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

"Bruce" <bv***************@mailblocks.com > a ?crit dans le message de news:
lr********************************@4ax.com...
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
Nov 17 '05 #7
Mattias Sjögren <ma********************@mvps.org> wrote:
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.


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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
Mattias Sjögren <ma********************@mvps.org> wrote:
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.


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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9
"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> a écrit dans
le message de news: #R**************@TK2MSFTNGP10.phx.gbl...
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

--
Joanna Carter
Consultant Software Engineer
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
7
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException...
0
by: Bruce | last post by:
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 =...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
6
by: MSDNAndi | last post by:
Hi, I get the following warning: "Possibly incorrect assignment to local 'oLockObject' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
4
by: Deckarep | last post by:
Hello fellow C# programmers, This question is more about general practice and convention so here goes: I got into a discussion with a co-worker who insisted that as a general practice all...
1
by: aeshiels | last post by:
Hopefully I have the correct newsgroup for this question. I've developed a very simple COM server in C++ as im trying to learn COM. The COM server has an array of IUnknown interfaces (MyArray)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.