PC Review


Reply
Thread Tools Rate Thread

C# reentrancy and VB static variable

 
 
=?Utf-8?B?WFBoYWt0b3I=?=
Guest
Posts: n/a
 
      16th Mar 2005
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

 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      16th Mar 2005
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.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"XPhaktor" <(E-Mail Removed)> wrote in message
news:9ED003EF-7E4F-44C3-A907-(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Samuel R. Neff
Guest
Posts: n/a
 
      16th Mar 2005

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.
 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      16th Mar 2005
Samuel,

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


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Samuel R. Neff" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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.



 
Reply With Quote
 
=?Utf-8?B?WFBoYWt0b3I=?=
Guest
Posts: n/a
 
      17th Mar 2005
Thanks.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable to Static Joseph Microsoft Access Database Table Design 3 4th Aug 2008 12:34 AM
Variable to Static Joseph Microsoft Access Form Coding 3 4th Aug 2008 12:34 AM
Static variable RobcPettit Microsoft Excel Programming 1 28th Jan 2007 01:27 PM
Event Recursion/Reentrancy Problem =?Utf-8?B?SGFydmV5?= Microsoft Access Form Coding 11 2nd Feb 2006 05:12 PM
Static Variable Todd Huttenstine Microsoft Excel Programming 7 7th May 2004 11:16 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:28 PM.