C# reentrancy and VB static variable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In C#, how do I replace using VB local static variable declarations to handle
method reentrancy. Note, if you use a class-scope variable instead of a local
one, then you run the risk of inadvertently changing the variable's value in
another method that is performing the same technique.

The Interlocked.Exchange class uses a class-scope class attribute.

Reentrancy is not just a problem for user events (e.g., overzealous users
double-clicking on a button). Reentrancy is also a problem in systems that
use concurrency, where methods may be overstacked in the call-stack.

VB example:

Private Function foo()
Static st_blnInHere As Boolean

Try
'$ To avoid reentrancy:
If st_blnInHere Then
Exit Try
Else
st_blnInHere = True
End If

'$ critical code here...

st_blnInHere = False

Catch ex As Exception
st_blnInHere = False
Finally
End Try
End Function
 
XPhaktor,

You will have no choice but to use a class level variable to handle
this. C# doesn't support local static variables.

Hope this helps.
 
The VB.NET compiler really just creates class-level fields for each
function static variable anyways. So there is no difference in doing
this yourself in C# (other than in C# you'll know what's going on).

For example, this VB.NET code:

public class Whatever
shared sub Main
static test as string
test = "hello"
Console.WriteLine(test)
end sub

end class

translates to this C# code

public class Whatever
{
private static string $STATIC$Main$001$test;
public static void Main()
{
Whatever.$STATIC$Main$001$test = "hello";
Console.WriteLine(Whatever.$STATIC$Main$001$test);
}

}


HTH,

Sam



B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
 
Samuel,

Actually, there is a difference. If you try and access that variable,
the compiler should enforce that and give you an error.
 
Back
Top