• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

.Net Statics

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    .Net Statics



    Protected Overrides Function DeviceHasBeenEnabled() As Boolean
    Static blnFirstTime As Boolean = False


    The above code throws a null reference exception.

    I am guessing it's because the ultimate caller is in a SyncLock ??

    Obviously I can do it a different way with no problems but why the hell can't I allocate a static in this circumstance.

    #2
    Because you don't understand threading and the nature of Static data. Like most VB6 to VB.NET people.

    Comment


      #3
      Originally posted by ASB
      Obviously I can do it a different way with no problems but why the hell can't I allocate a static in this circumstance.
      Why the hell would you want to? Statics are for constants (which should be declared final) or (though personally I think it's bad practice) for use as member variables in static classes (ie singletons). That is it. Anything else is a threading disaster waiting to happen...
      Listen to my last album on Spotify

      Comment


        #4
        Originally posted by DimPrawn
        Because you don't understand threading and the nature of Static data. Like most VB6 to VB.NET people.
        Maybe, maybe not. But don't you think that might have been part of the reason for the question?

        Would you care to enlighten me O great one.

        Comment


          #5
          Originally posted by Cowboy Bob
          Why the hell would you want to? Statics are for constants (which should be declared final) or (though personally I think it's bad practice) for use as member variables in static classes (ie singletons). That is it. Anything else is a threading disaster waiting to happen...
          Fair comment, but there are of course differing views.

          In this particular case the method has to retain state. Now I can retain it as a member in the class for example. However the issue with that is scope.

          Since it is only valid within the context of the method then I would prefer to reduce visibility to that method. If it's a member variable it's all too easy for somebody to come along and misuse it at a later date. Hence the thought of the static. Something I know you cannot do in C# (or couldn't).

          What I was curious of was what is the specific threading issue that causes the allocation to fail, because clearly there is something I misunderstood. Perhaps DP will enlighten me rather than just taking the piss.

          Comment


            #6
            Taking the piss is much more fun than helping people.

            Anyway. Static in VB.NET is some syntactic sugar to generate a hidden Shared (static in C#) class level variable in the IL to simulate a local static variable. Sounds like a typical VB.NET kludge to me and trap for the unwary.

            This Blog entry describes how the static local variable is intitialised and the impact of multiple threads.

            http://weblogs.asp.net/psteele/pages/7717.aspx


            Now since you have:

            Protected Overrides Function DeviceHasBeenEnabled() As Boolean
            Static blnFirstTime As Boolean = False

            You are trying to combine an Instance method with a static field and I am guessing here (I do C#) that the behind the scenes kludge is designed to work with:

            Shared Function DeviceHasBeenEnabled() As Boolean
            Static blnFirstTime As Boolean = False


            Lesson: Learn C#. Understand IL. Understand threading and initialisation.

            HTH

            Comment


              #7
              Yes but...

              That is referring to static members within shared (static) methods. Not instance methods. Here I have an instance method.

              The MS documentation specifically describes them as instance variables in this case:

              "If the procedure is not Shared, its local variables are instance variables, including the Static variables."

              So it *should* have been alright. If I can get round to it then I'll reproduce it and look at the IL (or more likely just avoid them like the plague).

              So my threading problem (and yes the exception is raised by Monitor.Enter) was not because of any lack of understanding on my part. Just by MS lieing in the docs.

              As for C# I have a plan. But it doesn't seems to be getting any closer.

              Comment


                #8
                Look at the ThreadStatic attribute as well, just in case you really want to hear that nice ka-plonking sound

                Comment


                  #9
                  Originally posted by Weltchy
                  Look at the ThreadStatic attribute as well, just in case you really want to hear that nice ka-plonking sound
                  Sorry, you've lost me.

                  I'm not using the attribute, or is VB being "helpful" and marking it for me. ??

                  Comment


                    #10
                    I have a black car

                    Comment

                    Working...
                    X