senthilkr

What the use of properties in OOPS.Could anyone give real time example.What is role it plays in 3 tier architecture



Re: Visual Basic Language "Properties" in OOPS

John Oliver (UK)MSP, VSIP

Hi,

I am not sure I have heard of the 3 tier architecture.

However properties play the role of allowing a programmer to model things in the real world by use of descriptions so that you can "tell" the computer what a particular object is and what you can do with it, that is how can you consume or utilise an object in terms of human-to-computer interaction.

Take as an example a ( usually ) solid static object like a house built of bricks, unlike a mobile home such as a caravan.

In OOP ( Object Orientated Programming ) Systems you can say what size the house is, how many bedrooms, how many bathrooms, how many other rooms and assign other properties like the house address, owners name, telephone number, the number of current occupants and whether it has a garage or not, number of gardens, size of each garden, how it is heated etcetera etcetera.

You could also specify rules of interaction between objects with lists and say a house can contain objects a , b, c or whatever but can not contain certain other objects.

E.G. A car can not contain a house ( normally, in the real world ) but a house can contain a car in the garage if the house has a garage.

Of course certain OBJECTs can be described within a STRUCTURE however to add functionality to an object you would do it in a CLASS to add methods such as TurnRight, TurnLeft, Forward, Reverse, Stop, Accelerate for a Car object.

Finally for a visual game or whatever you could add an image to the house and an appropriate image for a car.

I would not normally add any SUBs or FUNCTIONs to a STRUCTURE ( although it is possible, I added these to an example for quickness of coding ), you could put the METHODs in a CLASS.

This code uses one button on a Form.

Code Snippet

Public Class Form1

'You can do ALL of the following within a CLASS too. :-)

'A STRUCTURE representing certain properties of a car.

Public Structure car

Dim make As String

Dim model As String

Dim capacity As Short

Dim colour As Color

Dim engineType As String

Dim dateOfManufacture As Date

Dim dateOfFirstRegistration As Date

Dim vehicleIdNumber As String

Dim isLeftHandDrive As Boolean

Dim numberOfGears As Short

Dim currentSpeed As Short

'Adding methods although I am not going to to add

'any code to the SUBs for this example.

Public Sub moveRight()

End Sub

Public Sub moveLeft()

End Sub

'Adding a Function.

Public Function stopMe()

currentSpeed = 0

Return currentSpeed

End Function

Public Sub reverse()

End Sub

Public Sub forward()

End Sub

Public Sub accelerate()

End Sub

End Structure

'The next highlighted line should be all one line in your code window.>>>>

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Change the culture for the DATE values set

'later on in a string.

My.Application.ChangeCulture("en-US")

My.Application.ChangeUICulture("en-Us")

End Sub

'The next highlighted line should be all one line in your code window.>>>>

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Create a car and run it at 160 miles per hour.

Dim myLambo As New car

With myLambo

.capacity = 2000 'cc

.colour = Color.Red

.currentSpeed = 160 'mph ( miles per hour ).

.engineType = "Petrol"

'August 23rd,2000 at 10am.

.dateOfFirstRegistration = "08/23/2000 10:00:00"

'April 27th,2000 at midnight.

.dateOfManufacture = "04/27/2000 00:00:00"

.isLeftHandDrive = False

.make = "Lamborghini"

.model = "Diablo"

.vehicleIdNumber = "W1X2ABC1234567890"

End With

'The next highlighted line should be all one line in your code window.>>>>

MessageBox.Show("Car 'myLambo' speed is currently = " & myLambo.currentSpeed.ToString & " mph.")

'Stop the car!!>>

myLambo.stopMe()

'The next highlighted line should be all one line in your code window.>>>>

MessageBox.Show("Car 'myLambo' speed is currently = " & myLambo.currentSpeed.ToString & " mph.")

End Sub

End Class

Regards,

John






Re: Visual Basic Language "Properties" in OOPS

John Oliver (UK)MSP, VSIP

Hi,

This code uses one button on a Form.

I would be more inclined to do something like this.>>

Code Snippet

Public Class Form1

'You can do ALL of the following within a CLASS too. :-)

'A STRUCTURE representing certain properties of a car.

Public Structure car

Dim make As String

Dim model As String

Dim capacity As Short

Dim colour As Color

Dim engineType As String

Dim dateOfManufacture As Date

Dim dateOfFirstRegistration As Date

Dim vehicleIdNumber As String

Dim isLeftHandDrive As Boolean

Dim numberOfGears As Short

Dim myControls As carControl

End Structure

'This next highlighted line should be one line in your code window.>>>>

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Change the culture for the DATE values set

'later on in a string.

My.Application.ChangeCulture("en-US")

My.Application.ChangeUICulture("en-Us")

End Sub

'This next highlighted line should be one line in your code window.>>>>

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Create a car.

Dim myLambo As New car

With myLambo

.capacity = 2000 'cc

.colour = Color.Red

.engineType = "Petrol"

'August 23rd,2000 at 10am.

.dateOfFirstRegistration = "08/23/2000 10:00:00"

'April 27th,2000 at midnight.

.dateOfManufacture = "04/27/2000 00:00:00"

.isLeftHandDrive = False

.make = "Lamborghini"

.model = "Diablo"

.vehicleIdNumber = "W1X2ABC1234567890"

End With

Dim lamboControls As New carControl

myLambo.myControls = lamboControls

'Run the car at 160 'mph.

myLambo.myControls.currentSpeed = 160 'mph ( miles per hour ).

'This next highlighted line should be one line in your code window.>>>>

MessageBox.Show("Car 'myLambo' speed is currently = " & myLambo.myControls.currentSpeed.ToString & " mph.")

'Stop the car!!>>

myLambo.myControls.stopMe()

'This next highlighted line should be one line in your code window.>>>>

MessageBox.Show("Car 'myLambo' speed is currently = " & myLambo.myControls.currentSpeed.ToString & " mph.")

End Sub

End Class

Public Class carControl

Private mySpeed As Short

Public Property currentSpeed()

Get

Return mySpeed

End Get

Set(ByVal value)

mySpeed = value

End Set

End Property

'Adding methods although I am not going to to add

'any code to the SUBs for this example.

Public Sub moveRight()

End Sub

Public Sub moveLeft()

End Sub

'Adding a Function.

Public Function stopMe()

mySpeed = 0

Return mySpeed

End Function

Public Sub reverse()

End Sub

Public Sub forward()

End Sub

Public Sub accelerate()

End Sub

End Class

Regards,

John.






Re: Visual Basic Language "Properties" in OOPS

nobugz

A property acts just like a public variable in a class, the syntax to read and write the property is exactly the same. The power is in the additional layer of code you can put in the Get and Set accessors. You can completely change the internal implementation of your class but not have to change the client code that uses your class. It makes code very maintainable. Never use a public variable, always use a property. Even if the initial Get and Set simply copy a private variable. Some day, you'll be really glad you did.

This is a general OOP feature, not specific to 3 tier architecture.





Re: Visual Basic Language "Properties" in OOPS

senthilkr

I understood concept but not clear visually.Could you provide example to support what your below statement....

You can completely change the internal implementation of your class but not have to change the client code that uses your class. It makes code very maintainable. Never use a public variable, always use a property. Even if the initial Get and Set simply copy a private variable.




Re: Visual Basic Language "Properties" in OOPS

senthilkr

#Region " Private Property Variables "

Private _userID As String

Private _customerNumber As Int32

Private _userType As String

Private _firstName As String

Private _lastName As String

#End Region

Public Property UserID() As String

Get

Return _userID

End Get

Set(ByVal value As String)

_userID = value

End Set

End Property

Public Property CustomerNumber() As Int32

Get

Return _customerNumber

End Get

Set(ByVal value As Int32)

_customerNumber = value

End Set

End Property

Public Property UserType() As String

Get

Return _userType

End Get

Set(ByVal value As String)

_userType = value

End Set

End Property

Public Property FirstName() As String

Get

Return _firstName

End Get

Set(ByVal value As String)

_firstName = value

End Set

End Property

Here is a code we use..why should we need to use property to assign value insted of assignin class1.userid=textbox1.text





Re: Visual Basic Language "Properties" in OOPS

TaDa

Consider this:

Code Snippet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Tmp As New Temperature

Tmp.Farenheit = 32

MsgBox("Celsius:" & Tmp.Celsius)

MsgBox("Kelvin:" & Tmp.Kelvin)

Tmp.Kelvin = 300

MsgBox("Celsius:" & Tmp.Celsius)

MsgBox("Farenheit:" & Tmp.Farenheit)

End Sub

Public Class Temperature

Dim BaseTemp As Decimal = 0 'In celsius

Public Property Farenheit() As Decimal

Get

Return (BaseTemp * 9) / 5 + 32

End Get

Set(ByVal Value As Decimal)

BaseTemp = (Value - 32) * 5 / 9

End Set

End Property

Public Property Celsius() As Decimal

Get

Return BaseTemp

End Get

Set(ByVal Value As Decimal)

BaseTemp = Value

End Set

End Property

Public Property Kelvin() As Decimal

Get

Return BaseTemp + 273.15D

End Get

Set(ByVal Value As Decimal)

BaseTemp = Value - 273.15D

End Set

End Property

End Class





Re: Visual Basic Language "Properties" in OOPS

senthilkr

Properties ,with which we can give manipulations to the values we get..Then why we need private accessor




Re: Visual Basic Language "Properties" in OOPS

TaDa

We usually keep the actual instance variable private so "the outside" can't change it. In my Temperature example, whoever uses that class doesn't know and doesn't need to know I'm only storing the value in celsius and I wouldn't want them to be able to change that value directly.





Re: Visual Basic Language "Properties" in OOPS

PEng1

Consider this also, using a property as opposed to a public variable will allow you to take out a lot of repeated code, for instance from your code you provided a property for UserID, what you wanted to validate that every time the userID is changed that it is in the correct format beofore allowing the change to happen, with a public variable you could not do this. Instead every time you had to use the variable you would have to write an if statement that would make certain the UserID was valid.






Re: Visual Basic Language "Properties" in OOPS

senthilkr

Thanks,

I am not clear with your explanation..could you provide example for property userID so it could be more helpful.





Re: Visual Basic Language "Properties" in OOPS

senthilkr

I felt this example defines the purpose of Properties...

Recall that a property is data storage within an object. A roperty can be any of VB's data types including arrays and other objects. There are two ways to define a property in a class. The simpler way is to declare a public variable within the class:

Public Class SomeClass
  Public Name As String
...
End Class

Now the variable, or property, is available for use with objects based on the class using the standard ObjectName.PropertyName syntax::

Dim obj As New SomeClass
obj.Name = "Jack"

A second and more flexible way to define class properties is with property procedures. You start by declaring a variable in the class to hold the property value, but this time declare it as Private so it is not available to code outside the class. Then you write a property procedure that provides the interface for the property. It looks like this:

Public Property PropertyName As Type
 
Set(ByVal Value As Type)
  VarName = Value
End Set
 
Get
  PropertyName=VarName
End Get
 
End Property

In this example, PropertyName is the name of the property, Type is the data type of the property, and VarName is the name of the private variable you declared to hold the property value. Here's how it works. When a program sets the value of the property, the property value is passed to the Set...End Set block, which stores the value in the variable. When a program reads the value of the property, the Get...End Get block is called which retrieves the property value from the variable and returns it to the calling code.

So far, using a property procedure does not seem any different from simply declaring a Public variable for a property. The added flexibility comes in two ways. First, you can put additional code in the Set or Get block to perform other actions when the property is set or read, such as value checking (making sure the property value is within permitted limits) or essentially any other action you want the object to take at the time. Second, you can define a property as being read-only or write-only, as follows:

  • For a read-only property, include the ReadOnly modifier in the first line of the property procedure and omit the Set block.
  • For a write-only property, include the WriteOnly modifier in the first line of the property procedure and omit the Get block.

For example, here's a class with a single read-only property:

Public Class SomeClass
 
  Private pData As String
 
  Public ReadOnly Property Data As String
    Get
     Data = pData
    End Get
  End Property
 
End Class

Properties can also be object references and arrays. To implement a property that is an array the property procedure must be passed the index of the array element to access. Here's how that is done:

Public Class ArrayProperty

  Private pData(100) As Integer

  Public Property Data(ByVal Idx As Integer) As Integer
    Get
      Data = pData(Idx)
    End Get
    Set(ByVal Value As Integer)
      pData(Idx) = Value
    End Set
  End Property
End Class

The you would use the property like this:

Dim obj As New ArrayProperty
obj.Data(5) = 22

In a real program, you would include code in the property procedure to validate that the value of the index is within the allowed bounds for the array.