How To Do It - Shared Class Variables Part III


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: How To Do It - Shared Class Variables Part III

  1. #1
    Patrick Ireland Guest

    How To Do It - Shared Class Variables Part III


    For you consideration, this is a discussion
    of shared methods, the use of properties, and
    other implications for shared variables in
    classes. These is the third of four parts
    of the How To. Part IV will follow soon.

    Imports System.Console
    Class clsTestShare
    Private Shared mlngValue As Long = 6
    Private mlngNonShared As Long
    ' NOTE 1
    Property lngValue() As Long
    Get
    lngValue = mlngValue
    End Get
    Set
    mlngValue = value
    End Set
    End Property
    Public Shared Function GetTheValue() As Long
    Return mlngValue
    End Function
    Public Shared Function GetNonSharedValue() As Long
    ' NOTE 2
    ' Return mlngNonShared
    Return 0
    End Function
    ' NOTE 3
    Public Function GetShared() As Long
    Return mlngValue
    End Function
    Public Function AnotherGetNonShared() As Long
    Return mlngNonShared
    End Function
    ' NOTE 4
    Public Sub New(ByVal lngArgvValue As Long)
    mlngNonShared = lngArgvValue
    End Sub
    End Class
    Module modTestShare
    Sub Main()
    ' NOTE 5
    ' WriteLine("Value = " & clsTestShare.lngValue.ToString())
    ' NOTE 6
    WriteLine("Value = " & clsTestShare.GetTheValue.ToString())
    ' NOTE 7
    Dim PatsClass As New clsTestShare(9)
    ' NOTE 8
    WriteLine("Value = " & PatsClass.lngValue.ToString())
    ' NOTE 9
    WriteLine("Value = " & PatsClass.GetTheValue.ToString())
    ' NOTE 10
    WriteLine("Value = " & PatsClass.AnotherGetNonShared.ToString())
    End Sub
    End Module


    NOTE 1
    Properties can not be shared. Therefore they only work for an
    instantiated object.

    NOTE2
    Shared methods can only access shared variables of the class.

    NOTE 3
    Normal (nonshared) methods can acess both shared and nonshared
    variables of the class. This is because the method exists within
    the context of the instatiated object therefore any nonshared
    variables of the class also exist.

    NOTE 4
    In this example, the constructor is a nonshared method so it can
    access any variable shared or nonshared. Please note that the
    constructor can also be a shared method but that's the discussion
    for Part IV.

    See Kathleen, I did finally get closer to discussing shared
    constructors 8-).

    NOTE 5
    Send lngValue is a property of the class, it is not accessible
    through the class name definition rather it can only be used
    with an instantiated object of the class.

    NOTE 6
    GetTheValue is a shared method therefore is accessible via the class
    definition name.

    NOTE 7
    Instantiate a instance of the class and pass an initialization value
    to the constructor (nonshared NEW method in this example). At this
    point I can access any shared or nonshared method of the class using
    the instantiated object. I can also access any property of instantiated
    object. Finally, I can access and pubic or public shared variable of
    the class. Variable reference was covered in Part I of my How To for
    shared class variables.

    NOTE 8
    Property is available through the instantiated object.

    NOTE 9
    Shared method is available through the instantiated object.

    NOTE 10
    Nonshared method is available through the instantiated object.

    Output from this program is as follows:
    Value = 6
    Value = 6
    Value = 6
    Value = 9


  2. #2
    Mattias Sjögren Guest

    Re: How To Do It - Shared Class Variables Part III

    Hi Patrick,

    >NOTE 1
    >Properties can not be shared. Therefore they only work for an
    >instantiated object.


    Wrong, a class *can* have shared properties.


    Public Class C
    Private Shared _x As Integer

    Shared Sub New()
    _x = 5
    End Sub

    Public Shared Property X() As Integer
    Get
    Return _x
    End Get
    Set
    _x = value
    End Set
    End Property
    End Class

    --

    Console.WriteLine(C.X) ' 5
    C.X = 10
    System.Console.WriteLine(C.X) ' 10


    Mattias

    ====================================
    Mattias Sjögren - mattias @ mvps.org
    http://www.msjogren.net/dotnet/

    CodeHound - The Software Developer's Search Engine
    http://www.codehound.com

  3. #3
    Rob Teixeira Guest

    Re: How To Do It - Shared Class Variables Part III


    mattias.dont.want.spam@mvps.org (Mattias Sjögren) wrote:
    >Hi Patrick,
    >
    >>NOTE 1
    >>Properties can not be shared. Therefore they only work for an
    >>instantiated object.

    >
    >Wrong, a class *can* have shared properties.


    Correct. But it's also worth noting that like other OO languages (ie Java),
    shared members can't access non-shared members. The following would be an
    error since m_X isn't shared also.

    Public Class Class1
    Public m_X As Integer

    Public Shared Property X() As Integer
    Get
    X = m_X
    End Get
    Set
    m_X = value
    End Set
    End Property
    End Class

    -Rob

  4. #4
    Patrick Ireland Guest

    Re: How To Do It - Shared Class Variables Part III


    Hi Rob,

    Well you and Mattias have both gotten me that leaves Jeff and Kathleen 8-(
    I'll post a correction. As I said to Mattias, I tried several variations
    obvisionly not enough and I tried the Beta1 help (or lack of). I'll also
    correct my sample slides I use in my presentations. Thanks 8-)

    Pat


    "Rob Teixeira" <RobTeixeira@@msn.com> wrote:
    >
    >mattias.dont.want.spam@mvps.org (Mattias Sjögren) wrote:
    >>Hi Patrick,
    >>
    >>>NOTE 1
    >>>Properties can not be shared. Therefore they only work for an
    >>>instantiated object.

    >>
    >>Wrong, a class *can* have shared properties.

    >
    >Correct. But it's also worth noting that like other OO languages (ie Java),
    >shared members can't access non-shared members. The following would be an
    >error since m_X isn't shared also.
    >
    >Public Class Class1
    > Public m_X As Integer
    >
    > Public Shared Property X() As Integer
    > Get
    > X = m_X
    > End Get
    > Set
    > m_X = value
    > End Set
    > End Property
    >End Class
    >
    >-Rob



  5. #5
    Burt Guest

    Re: How To Do It - Shared Class Variables Part III


    I really don't see how this encourages OOP and a clean coding style.
    I realize that sometimes you just _have_ to have a global variable, but there's
    always some sort of rule that governs the use of that variable and as such
    shouldn't it be encapsulated in a property of a class that has the appropriate
    scope in your project?

    I can see how a class containing a bunch of shared methods could be particularly
    useful for a utility method store, but this can be done in a module, except
    where you'd want to compile these utilities into a dll.

    please help me see how you reconcile these philosophies.

  6. #6
    Patrick Ireland Guest

    Re: How To Do It - Shared Class Variables Part III


    Burt,

    Sorry my post response seems to have gotten lost in the ether world.
    I'll try again.

    Pat

    >I really don't see how this encourages OOP and a clean coding style.


    OOP doesn't say that you can't use a class containing only shared items
    that you don't instantiate and a class containing global data does make
    sense in many applications. What I was demonstrating were techniques
    that just different. It can be said that using only shared items in
    an uninstantiated class makes the programmer more aware of the special
    nature of the data just as some can argue that its not the best way.
    Your best way may not be the same as mine.

    >I realize that sometimes you just _have_ to have a global variable, but

    there's
    >always some sort of rule that governs the use of that variable and as such
    >shouldn't it be encapsulated in a property of a class that has the appropriate
    >scope in your project?


    Another way to skin a cat 8-).

    >I can see how a class containing a bunch of shared methods could be particularly
    >useful for a utility method store, but this can be done in a module, except
    >where you'd want to compile these utilities into a dll.


    Yes, it could but you and others may not know another method exists
    that could at some day on some distant planet on some application make
    your programming effort easier.

    >please help me see how you reconcile these philosophies.


    I have a simple philosophy for programming. Call it the Zen of Pat or
    what every suites your fancy.

    A programmer's job is to
    (1) Develope algoritmic solutions of problems based on his skill and any
    input (sometimes very little) from the client
    (2) Program the solution it the appropriate tool. Generally, I use
    VB.NET or VB6-- for the GUI portion with C/C++/C# for the busines
    logic.
    (3) Program the solution for Mack truck conditions. (I get killed by a
    Mack truck driving home tonight and some poor untrained and unprepared
    programmer has to pick up my code and get it running, and of course
    the project is behind schedule and I leave for home with the program
    blue screening but I need to get home even though I know what the
    solution is because the series finale of StarTrek Voyager is on
    tonight).
    (4) Program the solution so I can figure out what I did 3 months ago. This
    includes the use of comments and Hungarian notation. Naturally, I
    made a few modifications where it really counts, to paraphrase Hans
    Solo.
    (5) Program to avoid paying for the same piece of ground a second time.
    This means sometimes saving intermediate results for avoid repeated
    function calls.
    (6) Use state tables for problems that lend themselves to state
    sequencing.
    (7) Continue to retrain myself in the new tools (Net for instance)
    (8) OOPs is just another developement in the effort to produce order from
    the chaos of programming. Programming is still more art that science.
    This is demonstrated by the ability of people to program without formal
    programming training.
    (9) If the shop I'm in has a set of rules then I obey them no matter if
    they were written by an oger who last programmed in IBM 1401 autocoder.
    By the way, I did, many moons ago.
    (A) Note the hex number 8-). Progamming continues to evolve, I can be
    a dinasour or flying with the eagles (see 7).
    (B) Try not to get my ego involved in my programming. I can and do make
    mistakes. If someone else finds them, they saved me time trying to
    find the problem.
    (C) There is life outside of programming. Computers are only allowed to
    consume 16 hours of my time a day. I've got to draw a line someplace
    or the computers would suck out the last bit of my marrow. Also being
    a geek is fun but sex is still better 8-).

    I hope you take these with a grain of salt and have as good a laugh as I
    had writing them. These are not carved the clay tablets of Moses. I tried
    to answer your questions but use of OOP is still up to up


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center