views:

192

answers:

4

Could someone explain to me the concept of Get and Set property? It's just not sinking in for me.

Thanks!

+2  A: 

I don't use Visual Basic, but the principle works like this:

You have a private variable in your class, let's call it myNumber (which would be some numeric type). You don't want to allow public access to this variable for whatever reason.

You'd create a get and a set method for this variable (also called accessor and mutator methods) which would have an access level of public. This will allow you more control over how the value is set or retrieved. Here's some pseudocode:

getMyNumber(){
     return myNumber;
}
setMyNumber(value){
     if(value > 0){
         myNumber = value;
     }
}

With this setter method, you can make sure that myNumber can never be set to 0 or a negative value (for example).

Make sense?

inkedmn
Yes, a lot more than before. With the Set method you can set it up so that values can be given that don't cause exceptions. Correct?Also, the Get method can retrieve values so you don't have to rewrite the method; its automatically returned?
tahdhaze09
+1  A: 

You have one property that can have some combination of a getter and a setter. The getter is the code that runs when you read from the property. This code must return a value. The setter is the code that runs when you assign to the property.

Joel Coehoorn
+1  A: 

Making a property with Get and Set accessors allows you to make functions that behave like a variable.

For example, let's say that you're making a UserControl that has a label, and you want to let the people who use the user control get and set the text of the label, without giving them access to the label itself. (Note that if they really wanted to, they could access it through the Controls property)

You could make a pair of methods like GetLabelText and SetLabelText, which other people could call to change the label. However, calling methods like that is awkward. FOr example, if someone wanted to append the letter A to the label, they'd have to do something like this: control.SetLabelText(control.GetLabelText() + "A"). If you make a LabelText property, wh=ith Get and Set accessors instead of GetLabelText and SetLabelText, they could simply write control.LabelText += "A".

When you write a property, you write the Get and Set accessors like regular functions. Get takes no parameters and returns the value of the property, and Set takes a new value for the property as a hidden parameter called Value and doesn't return anything.

For example:

Public Property LabelText
    Get
        Return label.Text
    End Get
    Set
        label.Text = value
    End Set
End Property
SLaks
+1  A: 

This is not a concept native to vb.net. It is part of the .net framework and oop. To make a long story short it is just the way a client uses/interacts with the object in order to force him/her to follow a particular pattern of usage. It is a way of reading/setting values of the private members/variables after a layer where some logic can be implemented. For e.g. in the setter implementation of a class called Account. Lets say it has a property called Balance which is of string datatype (for example sake), but has only numeric values.

Dim acc as New Account("CustID-1234")
acc.Balance = "1234" 'This is valid
acc.Balance = "Ten thousand" 'this is wrong

Hence inorder to provide consistency in the data of an object (while either reading/setting) we have getters and setters resp.

Now the setter for the above class can be written like this:

Public Class Account
    '...Var dec
    Public Property Balance() As String
        Get
            Return m_iBal.ToString()
        Set (value As String)
            Dim i As Integer
            If Integer.TryParse(value, i) Then
                m_Bal = i
            Else
                'You can throw a nasty error
            End If
    End Property

End Class
deostroll