Property accessors in Visual Basic .Net
Could s开发者_C百科omeone explain to me the concept of Get and Set property? It's just not sinking in for me.
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
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?
Mutators (setters) and Accessors (getters) are a sneaky way of overriding the assignment operator. So, you have a public field, classroomSize. You can get and set its value easily:
int localVariable = 3000000;
MyClass.classroomSize = localVariable;
localVariable = MyClass.classroomSize;
But what if you know the entire school only has 300 chairs? You use a mutator to limit the assignment value to <= 300. Using the mutator, you don't have to burden the user of your class with calling a SetClassroomSize(300), and instead allow them to assign your property like a field.
This is a poor example to make it brief. In truth, you would most likely want to throw an exception when the classroom size exceded a reasonable value, rather than override that value. And throwing exceptions inside of a mutator (or accessor) is a bad form. So, in this case, you would actually want to create a SetClassroomSize() method. But you get the idea.
As for automatic creation that does nothing more than set and get the class' private field, there's little value in them other than, 1) they can be displayed in custom controls, whereas the field cannot, 2) some protocols (COM/DCOM?) require them for assignment, 3) they provide access restrictions (ReadOnly and WriteOnly properties have no equivalent in fields), 4) six months down the road, someone will need to convert your public fields into private fields and create accessors and mutators for all of them and will likely cuss at having to do your drudgery.
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
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.
精彩评论