MSDNAndi wrote:
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 original value of the local."
My code is:
using System;
using System.Collections.Generic;
using System.Text;
namespace RefLockExample
{
class Program
{
protected void MyMethod(ref Object oObject)
{
//something here
//oObject will be used, but no "new" object will be assigned to
oObject
}
Simple example:
protected void MyMethod(ref Object oObject)
{
oObject = new Object();
}
this means that you're passing back a new object, so the following code:
lock (oLockObject)
{
MyMethod(ref oLockObject);
}
could mean that it locks one object, changes it through the method call,
and thus tries to unlock the new object. This is what the warning is
about, that this isn't going to happen, that it's the original object
(which is now replaced with a new one) that is going to be unlocked.
Of course, as you say, you don't replace the object, but the compiler
doesn't know that.
--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2