none
Shared Class Members Horror... RRS feed

  • Question

  • I have a problem that I am assuming is related to Shared methods and properties, making me wonder what the real scope of a shared class member is, because apparently it's not what I thought it was.

    I have a mapping control in its own DLL, we'll call it WidgetMap (pictured below).  Normally, a client coder would only place one instance of this control in their application, hence, I have never run into this issue before. 

    Now, someone is using 2 instances of this control in their app, on the same form.

    Their form has a tab control, and Tab1 has one instance of the WidgetMap, and Tab2 has another instance.

    I've been encountering a lot of weird and unexpected behavior that I am assuming is because the map uses a lot of classes that have nothing but Shared Functions, Subs, and Properties in them.

    Can you tell me, is there any reason that a Shared Function, Shared Sub, or Shared Property code in WidgetInstanceA could in any way interact with, or cause a change in behavior of WidgetInstanceB ???

    Since each WidgetMap instance is it's own DLL, aren't the Shared class members in one WidgetMap instance completely insulated from the other ???

    Thanks for your thoughts.

    Saturday, September 15, 2012 7:57 AM

Answers

  • Maybe the control has variables that are declared as static.

    That is, they are shared between instances of the control. Therefore changing it for one instance also changes it for the other running instance and thus causing unpredictable behavior.

    I have seen in many .NET user controls where the developer has declared a variable as "static" purely so they can initialize it outside a method (function) forgetting that this variable (once defined as static) will affect all instances of the control.

    Just my thoughts :)

    Milton


    MFN

    Saturday, September 15, 2012 10:39 AM
  • Hi Simpleton,

    Nice to see you.

    >>Can you tell me, is there any reason that a Shared Function, Shared Sub, or Shared Property code in WidgetInstanceA could in any way interact with, or cause a change in behavior of WidgetInstanceB ???

    A shared property will be shared in all the instances. All instances just use this same one property. So when one instance changed it, the other instances  are affected. 

    To avoid this, you can make a unsharing one.

    >>Since each WidgetMap instance is it's own DLL, aren't the Shared class members in one WidgetMap instance completely insulated from the other ???

    No, they don't. there only one property instance, and all Class instances use the same one.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, September 19, 2012 9:29 AM
    Moderator

All replies

  • Maybe the control has variables that are declared as static.

    That is, they are shared between instances of the control. Therefore changing it for one instance also changes it for the other running instance and thus causing unpredictable behavior.

    I have seen in many .NET user controls where the developer has declared a variable as "static" purely so they can initialize it outside a method (function) forgetting that this variable (once defined as static) will affect all instances of the control.

    Just my thoughts :)

    Milton


    MFN

    Saturday, September 15, 2012 10:39 AM
  • Hi Simpleton,

    Nice to see you.

    >>Can you tell me, is there any reason that a Shared Function, Shared Sub, or Shared Property code in WidgetInstanceA could in any way interact with, or cause a change in behavior of WidgetInstanceB ???

    A shared property will be shared in all the instances. All instances just use this same one property. So when one instance changed it, the other instances  are affected. 

    To avoid this, you can make a unsharing one.

    >>Since each WidgetMap instance is it's own DLL, aren't the Shared class members in one WidgetMap instance completely insulated from the other ???

    No, they don't. there only one property instance, and all Class instances use the same one.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, September 19, 2012 9:29 AM
    Moderator
  • Hi Simpleton,

    Do you have any update?

    I have marked Milton's reply and mine as anwsers, if you think them provides no help, please feel free to unmark it and follow up.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, September 24, 2012 1:48 PM
    Moderator