locked
lifespan and scope of shared variables and functions RRS feed

  • Question

  • I am trying to understand what happens when the shared attribute is used.  And first, I have a question about syntax.

    Are the following three the same upon compilation

    dim x as new assigningfunction
    dim x as new assigningfunction()
    dim x as assigningfunction =  new assigningfunction()

    now, my questions about the shared property:

    Are shared variables droped when the program leaves the scope of the calling method, or do the persist throughout the life of the routine?

    What is the scope of methods within imported classes?  When importing a class with shared methods, do you create seperate instances of those imported methods and variables within different methods of the importing class, or are they persistant within that importing class?

    I am trying to understand when it is best to use shared methods and variables.  It seems, overall, a liability towards code portability.  Even the many shared methods as part of the system.* seem more for convienence of avoiding the typing of creating instances.  Any general advice?

    Saturday, July 31, 2010 4:59 AM

Answers

  • dim x as new assigningfunction
    dim x as new assigningfunction()
    dim x as assigningfunction =  new assigningfunction()

    They are all the same, however with every version it becomes more basic.

    This last one by instance is only in framework 1.0 really needed.

    It reflects a little bit the difference in thinking between a C and VB language type programmer.
    The C one want to write it his whole life like he has learned it at school, the VB one wants to have progress also in the language

    Shared classes are never dropped.

    Shared classes are in VB in fact almost exactly the same as modules (This is not in C# where you have history wise mixed shared(static) classes). It stays available forever in a program (dependend however to its access level (Public friend private). A shared class (module) is not an object it is created on the stack. Only objects (instanced classes) are released during the time of a program. This is done by the Garbage Collector, which releases an object as soon as it is not needed any more (be aware this has very few to do with setting it to nothing or using the method dispose).  An object looses its own reference when it goes out of scope, but as long as something else has a reference to that, it is not released from the managed heap.

    About what is better are the meanings different. I tell everybody starting with OOP to try to instance everything. It learns that there is no need anymore to set everything to zero or whatever. However, some think that instancing slows down while others think that shared classes takes to much memory. In current computers those arguments are in my idea from history. So my advice is, try to instance as much as possible, it learns you at least to well develop OOP.

     

     


    Success
    Cor
    • Marked as answer by sivant Saturday, July 31, 2010 12:37 PM
    Saturday, July 31, 2010 6:51 AM
  • @Acamar

    Your reply is full of static.

    Static is in VB a keyword for a variable which can be in a method and is in fact a global field in Net.

    http://msdn.microsoft.com/en-us/library/z2cty7t8.aspx

    The in C type languages keyword Static comes from the time that C languages where teletype programs and that parts of programs could be placed fixed (static) to a certain memory address. Both shared and the C# static keyword aren't in fact anymore true, although in my idea because it is newer, does Shared much more describe what it logical does. 


    Success
    Cor
    • Marked as answer by sivant Saturday, July 31, 2010 12:45 PM
    Saturday, July 31, 2010 10:31 AM

All replies

  • dim x as new assigningfunction
    dim x as new assigningfunction()
    dim x as assigningfunction =  new assigningfunction()

    They are all the same, however with every version it becomes more basic.

    This last one by instance is only in framework 1.0 really needed.

    It reflects a little bit the difference in thinking between a C and VB language type programmer.
    The C one want to write it his whole life like he has learned it at school, the VB one wants to have progress also in the language

    Shared classes are never dropped.

    Shared classes are in VB in fact almost exactly the same as modules (This is not in C# where you have history wise mixed shared(static) classes). It stays available forever in a program (dependend however to its access level (Public friend private). A shared class (module) is not an object it is created on the stack. Only objects (instanced classes) are released during the time of a program. This is done by the Garbage Collector, which releases an object as soon as it is not needed any more (be aware this has very few to do with setting it to nothing or using the method dispose).  An object looses its own reference when it goes out of scope, but as long as something else has a reference to that, it is not released from the managed heap.

    About what is better are the meanings different. I tell everybody starting with OOP to try to instance everything. It learns that there is no need anymore to set everything to zero or whatever. However, some think that instancing slows down while others think that shared classes takes to much memory. In current computers those arguments are in my idea from history. So my advice is, try to instance as much as possible, it learns you at least to well develop OOP.

     

     


    Success
    Cor
    • Marked as answer by sivant Saturday, July 31, 2010 12:37 PM
    Saturday, July 31, 2010 6:51 AM
  • There is not a lot of discussion about the scope of static methods, but what there is indicates that all variables must be in the stack, which implies that the code is shared and is not locked by default.  For example: "Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter." Static members of the containing class (or any other class) are safe, as are instance variables as long as they are on the stack. Private members of the containing class are not safe.  Presumably, this includes variables local to the static method, so care must be taken in writing a static method to ensure that the method arguments are not accessed from local copies, but are only referenced as the argument variable. There is a good discussion here:

     http://odetocode.com/Articles/313.aspx

     

    Saturday, July 31, 2010 8:13 AM
  • @Acamar

    Your reply is full of static.

    Static is in VB a keyword for a variable which can be in a method and is in fact a global field in Net.

    http://msdn.microsoft.com/en-us/library/z2cty7t8.aspx

    The in C type languages keyword Static comes from the time that C languages where teletype programs and that parts of programs could be placed fixed (static) to a certain memory address. Both shared and the C# static keyword aren't in fact anymore true, although in my idea because it is newer, does Shared much more describe what it logical does. 


    Success
    Cor
    • Marked as answer by sivant Saturday, July 31, 2010 12:45 PM
    Saturday, July 31, 2010 10:31 AM
  • You can quibble about the terminology all you like - it doesn't change the facts.  Shared methods are called static in C#, and all the discusion relates to C#, but it is just as relevant to VB.  You just need to do a little bit of translating on the fly.  The article I referred to makes that clear.
    Saturday, July 31, 2010 11:25 AM