Is it an error on msdn document?

  • Thread starter Thread starter Franz
  • Start date Start date
F

Franz

http://support.microsoft.com/?kbid=317109

In this document, the NAR method for C# is as follow.

private void NAR(object o)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
}
catch {}
finally
{
o = null;
}
}

What is the use of the line "o = null"? Since the parameter is not a ref
object, it can't change the value of o. Is it a COM issue?
 
| http://support.microsoft.com/?kbid=317109
|
| In this document, the NAR method for C# is as follow.
|
| private void NAR(object o)
| {
| try
| {
| System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
| }
| catch {}
| finally
| {
| o = null;
| }
| }
|
| What is the use of the line "o = null"? Since the parameter is not a ref
| object, it can't change the value of o. Is it a COM issue?
|
|

The argument o is a copy of the callers object reference, so here the value
of the copy is set to null. While it's not an error to do so, it's also not
necessary as it will be done automatically at object return.

Willy.
 
Also, in a non-debug build, it may actually prolong the lifetime of the
object.

In the end, that line is pointless.
 
True, and it's rather common to see this in code originally written in VB,
converted to VB.NET and finally translated into C#.

Willy.

message | Also, in a non-debug build, it may actually prolong the lifetime of the
| object.
|
| In the end, that line is pointless.
|
| --
| - Nicholas Paldino [.NET/C# MVP]
| - (e-mail address removed)
|
| | >
| > | > | http://support.microsoft.com/?kbid=317109
| > |
| > | In this document, the NAR method for C# is as follow.
| > |
| > | private void NAR(object o)
| > | {
| > | try
| > | {
| > | System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
| > | }
| > | catch {}
| > | finally
| > | {
| > | o = null;
| > | }
| > | }
| > |
| > | What is the use of the line "o = null"? Since the parameter is not a
| > ref
| > | object, it can't change the value of o. Is it a COM issue?
| > |
| > |
| >
| > The argument o is a copy of the callers object reference, so here the
| > value
| > of the copy is set to null. While it's not an error to do so, it's also
| > not
| > necessary as it will be done automatically at object return.
| >
| > Willy.
| >
| >
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top