Classes and property variables
- I was reading the help file regarding using Classes. I came across the following sentence regarding property values.'To protect their values from direct modification, variables used to store property values should be declared as Private'. I was wondering how a variable could be placed in code that was not declared as private so that it's value could be at risk of being directly modified? Thanks for any help concerning this.
Answers
- Which house would rather live in?
Public Class UnsafeHouse
Public Shared myMoney As Decimal
End Class
Public Class SafeHouse
Private myMoney As Decimal
End Class
Dim myStolenMoney as Decimal = UnsafeHouse.myMoney; ' I've been robbed!
Dim mySafeMoney as Decimal = SafeHouse.myMoney; ' Access denied, this line does not compile either.
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971." - Here is another example where the user can set the value of a color in a class as long as that color is not blue . The private variable is protected against direct modification to not allow the color.blue . Users can access or change the public property only .
Public Class MyColorClass Private TheColor As Color Public Property Color() As Color Get Return TheColor End Get Set(ByVal value As Color) If Not value = Color.Blue Then TheColor = value End If End Set End Property End Class
coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
Please format the code in your posts with the
button . Makes it easier to read .- Marked As Answer byJack_Oh Saturday, November 07, 2009 4:51 AM
- Looks like the others have answered your question ... but if you want to read some additional information on classes, I have some blog posts that cover object-oriented principles here:
http://msmvps.com/blogs/deborahk/archive/2009/08/31/what-is-oo.aspx
Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!- Marked As Answer byJack_Oh Saturday, November 07, 2009 4:52 AM
- Classes can expose their fields either directly, as a Public variable, or indirectly, as a property. Public variables are accessed in exactly the same way as a property. By exposing the field indirectly through a property the programmer is able, in the property setter, to handle any special conditions that may apply, and to ensure that any necessary supporting code gets executed. Validation is the most common reason for preferring a property over a Public field, but it's certainly not the only one. Here's an example of how using a property instead of a Public field ensures that data within the class remains consistent. In this case, the value is adjusted if ti is not valid (the alternative is to return an error) and, because the value affects a graphical setting, the control is refreshed An event is also raised.
If there is no need to maintain consistency or to do validity checking or to have events raised when the value changes, then just use a Public variable - it is actually quite a bit faster than using a property. However many people consider it poor practice (and the debate can become quite heaed), although the reasons given are sometimes spurious.''' <summary> ''' Sets or gets the minimum distance value represented by the control. ''' </summary> ''' <value>The distance value indicated by the centre of the control.</value> ''' <returns>The value as last set, or Maximum - 1.</returns> ''' <remarks>The Minimum property is the numeric value represented by the point at the centre of ''' the control. When the pointer is positioned at the centre, the <see cref="Distance"/> ''' value returned will be the Minimum.<para /> ''' The Minimum must be less than the <see cref="Maximum"/>. If the Minimum is set to a value ''' equal to or greater than the Maximum, then the supplied value is ignored and it is set to a value one ''' less than the Maximum.<para /> ''' The default property value in the designer is zero. ''' </remarks> <Category("Behavior"), _ Description("Minimum distance value that the pointer can indicate"), _ DefaultValue(0.0!), _ DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), _ EditorBrowsable(True), _ Browsable(True), _ MergableProperty(True)> _ Public Property Minimum() As Single 'Number less than maximum that indicates the minimum value that the control can indicate Get Return myMinimum End Get Set(ByVal Value As Single) If myMinimum <> Value Then If Value > Maximum Then myMinimum = myMaximum - 1 Else myMinimum = Value NotifyPropertyChanged("Minimum") Me.Refresh() End If End Set End Property- Marked As Answer byJack_Oh Saturday, November 07, 2009 4:52 AM
All Replies
I was reading the help file regarding using Classes. I came across the following sentence regarding property values.'To protect their values from direct modification, variables used to store property values should be declared as Private '. I was wondering how a variable could be placed in code that was not declared as private so that it's value could be at risk of being directly modified? Thanks for any help concerning this.
Declare the variable as Public.
Mark the best replies as answers. "Fooling computers since 1971."Thanks for your reply to my question. I was wondering if you could give me a code example of accessing a class property public variable. And if you could can you explain why direct modification is not good. Thanks again.
- Which house would rather live in?
Public Class UnsafeHouse
Public Shared myMoney As Decimal
End Class
Public Class SafeHouse
Private myMoney As Decimal
End Class
Dim myStolenMoney as Decimal = UnsafeHouse.myMoney; ' I've been robbed!
Dim mySafeMoney as Decimal = SafeHouse.myMoney; ' Access denied, this line does not compile either.
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971." - Here is another example where the user can set the value of a color in a class as long as that color is not blue . The private variable is protected against direct modification to not allow the color.blue . Users can access or change the public property only .
Public Class MyColorClass Private TheColor As Color Public Property Color() As Color Get Return TheColor End Get Set(ByVal value As Color) If Not value = Color.Blue Then TheColor = value End If End Set End Property End Class
coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
Please format the code in your posts with the
button . Makes it easier to read .- Marked As Answer byJack_Oh Saturday, November 07, 2009 4:51 AM
- Looks like the others have answered your question ... but if you want to read some additional information on classes, I have some blog posts that cover object-oriented principles here:
http://msmvps.com/blogs/deborahk/archive/2009/08/31/what-is-oo.aspx
Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!- Marked As Answer byJack_Oh Saturday, November 07, 2009 4:52 AM
- Classes can expose their fields either directly, as a Public variable, or indirectly, as a property. Public variables are accessed in exactly the same way as a property. By exposing the field indirectly through a property the programmer is able, in the property setter, to handle any special conditions that may apply, and to ensure that any necessary supporting code gets executed. Validation is the most common reason for preferring a property over a Public field, but it's certainly not the only one. Here's an example of how using a property instead of a Public field ensures that data within the class remains consistent. In this case, the value is adjusted if ti is not valid (the alternative is to return an error) and, because the value affects a graphical setting, the control is refreshed An event is also raised.
If there is no need to maintain consistency or to do validity checking or to have events raised when the value changes, then just use a Public variable - it is actually quite a bit faster than using a property. However many people consider it poor practice (and the debate can become quite heaed), although the reasons given are sometimes spurious.''' <summary> ''' Sets or gets the minimum distance value represented by the control. ''' </summary> ''' <value>The distance value indicated by the centre of the control.</value> ''' <returns>The value as last set, or Maximum - 1.</returns> ''' <remarks>The Minimum property is the numeric value represented by the point at the centre of ''' the control. When the pointer is positioned at the centre, the <see cref="Distance"/> ''' value returned will be the Minimum.<para /> ''' The Minimum must be less than the <see cref="Maximum"/>. If the Minimum is set to a value ''' equal to or greater than the Maximum, then the supplied value is ignored and it is set to a value one ''' less than the Maximum.<para /> ''' The default property value in the designer is zero. ''' </remarks> <Category("Behavior"), _ Description("Minimum distance value that the pointer can indicate"), _ DefaultValue(0.0!), _ DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), _ EditorBrowsable(True), _ Browsable(True), _ MergableProperty(True)> _ Public Property Minimum() As Single 'Number less than maximum that indicates the minimum value that the control can indicate Get Return myMinimum End Get Set(ByVal Value As Single) If myMinimum <> Value Then If Value > Maximum Then myMinimum = myMaximum - 1 Else myMinimum = Value NotifyPropertyChanged("Minimum") Me.Refresh() End If End Set End Property- Marked As Answer byJack_Oh Saturday, November 07, 2009 4:52 AM


