Results 1 to 3 of 3

Thread: encapsulation examples

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    1

    encapsulation examples

    Hi, I have searched and searched for a code example which shows some basic programming that does not use encapsulation and then the same code but changed to represent encapsulation...

    All i can find is examples of encapsulated code

    Could someone please post me some simple code snippets

    -snippet of some simple code that doesnt use encapsulation

    and

    -snippet of that same code modified to represent encapsulation

    any help would be greatley appriciated

  2. #2
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: encapsulation examples

    Is this for a class?
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  3. #3
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: encapsulation examples

    ' A class without encapsulation:

    Code:
    Class A
       Public Field As Integer
    
       Public Function MethodA() As Integer
            Return Field + 1
       End Function
    End Class
    The class above exposes its field directly to the user so it can be accessed from anywhere.

    Code:
    Class B
       Private _Field As Integer
       Private Function MethodB() As Integer
           Return _Field + 1
       End Function
    
       Public Property Field As Integer
           Get
               Return _Field
           End Get
           Set(ByVal value As Integer)
               _Field = value
           End Set
        End Property
    
        Public ReadOnly Property Result As Integer
           Get
              Return MethodB
           End Get
        End Property
    End Class
    The second method encapsulates a private field which can be accesses only through a public property and you can perform validation in the 'Set' block. Also it encapsulates a 'business logic' method (MethodB) and only returns result through the readonly property. Using this technique you can protect your private members from any outside interference.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width