none
Difference between Variable and Property

    Question

  • I have a class project which contains public shared properties for the solution project I'm working on.  For example:

     

    Public Class ConnectionStrings

    Private Shared _payroll As String = Nothing

    Public Shared Property Payroll() As String

    Get

    Return _payroll

    End Get

    Set(ByVal value As String)

    _payroll = value

    End Set

    End Property

    End Class

     

    How does that differ from doing it this way:

     

    Public Class ConnectionStrings

    Public Shared Payroll as string

    End Class

     

    I understand that by using the property method I am able to easily add other actions when the _payroll value is changed for instance:

     

    set(byVal value as String)

    _payroll = value

    payrollTableAdapter.connection.connectionstring = value

    end set

     

    But are there any other advantages/differences between the two methods of sharing a public variable?

     

     

    Thursday, April 10, 2008 5:32 PM

Answers

All replies

  •  c_manboy wrote:

    I have a class project which contains public shared properties for the solution project I'm working on.  For example:

     

    Public Class ConnectionStrings

    Private Shared _payroll As String = Nothing

    Public Shared Property Payroll() As String

    Get

    Return _payroll

    End Get

    Set(ByVal value As String)

    _payroll = value

    End Set

    End Property

    End Class

     

    How does that differ from doing it this way:

     

    Public Class ConnectionStrings

    Public Shared Payroll as string

    End Class

     

    I understand that by using the property method I am able to easily add other actions when the _payroll value is changed for instance:

     

    set(byVal value as String)

    _payroll = value

    payrollTableAdapter.connection.connectionstring = value

    end set

     

    But are there any other advantages/differences between the two methods of sharing a public variable?

     

     

     

    Hi,

     

    A "Property" can define any aspect of any object such as the colour ( color ) of a car.

     

    A property can contain more than one value too ( it doesn't have to though ). A property is something

     that is associated with ( or belongs to ) another object such as the SIZE of a FORM.

     

    Code Snippet

     

    Option Strict On

    Public Class Form1

     

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

     

    'Here is a CONSTant>>

    Const Pi As Double = Math.PI

     

    'Here is a variable.>>

    Dim myVariable As Double = Double.MaxValue / 2

     

    MessageBox.Show((Pi * myVariable).ToString)

    ' SIZE here is a STRUCTURE.>>

    Dim mySize As Size

     

    'These aspects of a SIZE structure are INTEGER values

    'and are considered to be properties of a SIZE object

    'as they belong to the SIZE object.

    'Like any c.d. that you own is your property!!

    mySize.Height = 200

    mySize.Width = 100

     

    ' SIZE here is a property of this FORM "FORM1" known

    ' within the same code block as ME as SIZE

    'is a property belonging to a FORM. >>

    Me.Size = mySize

    End Sub

    End Class

     

     

     

    A variable is an object that can have its value changed or varied hence

     the word variable as opposed to a CONSTant that can not be changed.

     

    Additionally if you think of two identical cars.

    Say two Ferraris ( I wish!! ), anyway, apart from the VIN number of the vehicles the only way to tell them apart is the mileage they have done and the condition they are in ( one might be dirty ) and their registration plates and where they are in 3D space. I think that has most things covered.

     

    You would have two "instances" of TYPE Ferrari of the Class or object known as a car which in turn could be a sub-class of the CLASS "vehicle".

     

    I have heard that using the SHARED keyword shares that value ( or property or method ) between

     every instance of a CLASS or object or whatever.

     

    I suggest that you use the SHARED keyword where things are not going to change betweeen 2 or more similiar objects such as the number of wheels on a car. For most cars especially on a Ferrari this is 4 but you can get cars with 3 wheels.

     

    If anyone else wishes to confirm ( or otherwise ) any of this then please feel free to correct me if I am wrong.

     

     

     

     

    Regards,

     

    John

     

    Thursday, April 10, 2008 6:58 PM
  • I thinkyou might be better served to ask the question:  What is the value of encapsulation in OOD?  Declaring class variables as Private and then exposing them through properties achieves the level of protection that good OOD demands.  It does not guarantee that protection -- we're all more than capable of subterfuging encapsulated design one way or another, but OO languages provide properties more tools to achieve flexibility, security and readability by means of keywords and attributes and in the way that classes interract with each other. 

     

    So while you can expose a class variable it will not be implementing all the potential that the language provides to a property.  I'm sure you'll get all kinds of examples of this by members chiming in so I'll let those who know more give you the hard examples.

     

    Thursday, April 10, 2008 6:59 PM
  •  c_manboy wrote:

    I have a class project which contains public shared properties for the solution project I'm working on.  For example:

     

    But are there any other advantages/differences between the two methods of sharing a public variable?

     

     

    Hi again,

     

    Using the Set section of code as you have discovered you can do more than one thing with the value that is passed in the SET routine. You could write value to a FILE as well if you wanted to.

     

    By using a PRIVATE variable and the GET and SET methods of a property you also encapulate or "contain" the value of the PRIVATE variable within your CLASS and this does not make it directly accessible without creating an instance of your CLASS.

     

    E.G.

     

    With.>>

     

    Code Snippet

    Public Class ConnectionStrings

    Public Shared Payroll as string

    End Class

     

     

     

    you can do this ( but you shouldn't really ).>>

     

    ConnectionStrings.Payroll = "Whatever string"

     

    Using a PRIVATE variable and the GET and SET methods you have to do something like.>>

     

    Code Snippet

     

    Dim conStr1 as New ConnectionStrings

    conStr1.Payroll = "Whatever string"

     

     

     

     

    As your _PayRoll variable is PRIVATE it is not directly accessible.

     

     

     

     

    Regards,

     

    John

     

    Thursday, April 10, 2008 7:29 PM
  •  

    As stated in a previous reply, the idea of encapsulation is the key.  I had asked a very similar question when I first went from VB6 to .NET, and I found this to be a very helpful explanation: http://www.codinghorror.com/blog/archives/000654.html

     

    Good luck!

    Thursday, April 10, 2008 8:12 PM
  • Thanks for all your replies.  The link you provided, Corey, was helpful and seemed to simply my question.  I'm taking from all this that, for my case, I could use either method becuase I'm using Shared, but that the property approach is arguably a better way in the long run. 

     

    In my case I am creating a class project which contains all of my datasets, etc. which I'm exposing through the public shared classes.  I suspect that it's wrong to do it this way but my reason is that I have three distinct aspects to my solution:

     

    1.  A Shared class which contains strongly typed datasets made public through the shared properties of the class,

    2.  The main UI project which uses the data from the shared classes to bind to controls,

    3.  And a class project made up of a couple of different classes to perform complex calculations on the data from the shared class.

    Thursday, April 10, 2008 9:03 PM