VB.NET: Interfaces and Encapsulation

I am learning ASP.NET 2.0 using VB.

I understand the reason for encapsulation however I am not sure how to implement  it within the website. What goes in the class and how do I use it on the web page?

Also where do interfaces come into this?

A very broad question I know, hoping I can get some help understanding this.
LVL 8
LotokAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

itsfactCommented:
http://www.4guysfromrolla.com/webtech/091800-2.shtml
a good artical to use encapsulation in ASP .Net.
0
LotokAuthor Commented:
Is there a version of this with VB.NET examples?

VB.NET foes this differently with GET/SET
0
Arthur_WoodCommented:
in VB.NET, the KungFoo NameSpace and Foo class would look like this:

The body of the discussion about encapsualtion is language independent.

AW
namespace KungFoo 
 
  Public Class Foo 
 
    Private _phoneNumber as String
 
    Public Sub New()
      _phoneNumber = "703-567-8860"
    End Sub
 
    Public Property PhoneNumber() as String
      Get
        Return _phoneNumber
      End Get
      Set (Value as String)
        _phoneNumber = Value
      End Set
    End property
End Class

Open in new window

0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

LotokAuthor Commented:
Would you create a class in the app_code folder or would this all be on the code-behind page?
Where do interfaces come into it?
0
Arthur_WoodCommented:
If the class would be used in multiple places throughout your application, then you would create the class in the app_code folder - if it only applies to a specific page, the in the code_behind for that page.

Interfaces allow you to have much more general code, which might be much easier to allow for code reuse.

In the case of the sample class shown here, an interface would not do much good.  But, just for the sake of argument, let's say that your applicxation migth be used for both US and foerign users.  The claas might then support several differnt Phone Number formats.  In the US, we use Area Code, Prefix and Number, while in Europe they use a Country Code, and Number (no prefix, to the best of my knowledge).  In that case you might define an Interface

Public Interface IPhone
    Property PhoneCode as String
    Property Number as String
    ReadOnly Property Display_Number as string
End Interface

then you could have a US_Phone Class and a EU_Phone Class:

Public Class US_Phone Implements IPhone
    Private _AreaCode as String
    Private _Prefix as String
    Private _PhoneNumber as String
    Public Property AreaCode() as String Implements IPhone.PhoneCode
        Get
             Return _AreaCode
        End get
        Set (Value as String)
             _AreaCode = Value
        End Set
   End Property
   Public Property Prefix() as String
       Get
           Return _Prefix
       End Get
       Set (Value as String)
           _Prefix = Value
       End Set
   End property
   Public Property PhoneNumber() as String Implements IPhone.Number
      Get
         Return _PhoneNumber
      End Get
      Set (Value as String)
         _PhoneNumber = Value
      End Set
   End Property
   Public ReadOnly Property Display_Number() as String Implements IPhone.Display_Number
      Get
           If _AreaCode.Length > 0 Then
              Return "(" & _AreaCode & ") " & _Prefix & "-" & _PhoneNumber
           Else
              Return  _Prefix & "-" & _PhoneNumber
           End If
      End Get
End Class
       
Public Class EU_Phone Implements IPhone
    Private _CountryCode as String
    Private _PhoneNumber as String
    Public Property CountryCode() as String Implements IPhone.PhoneCode
        Get
             Return _CountryCode
        End get
        Set (Value as String)
             _CountryCode= Value
        End Set
   End Property
   Public Property PhoneNumber() as String Implements IPhone.Number
      Get
         Return _PhoneNumber
      End Get
      Set (Value as String)
         _PhoneNumber = Value
      End Set
   End Property
   Public ReadOnly Property Display_Number() as String Implements IPhone.Display_Number
      Get
           If _CountryCode.Length > 0 Then
              Return "+" & _CountryCode & _PhoneNumber
           Else
              Return  _PhoneNumber
           End If
      End Get

End Class

That way you would treat a US_Phone object and a EU_Phone object as instances of an IPhone object, and be certain the they would both have a PhoneCode property and a Number Property, and that they would both have a Display_Number property which was ReadOnly and would show the properly formatted Phone Number.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LotokAuthor Commented:
Thanks for the explanation, Really appreciate it.
0
Arthur_WoodCommented:
Glad to be of assistance

AW
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.