|
-
Jun 28th, 2009, 10:39 PM
#1
Thread Starter
Member
Properties statement
hi, i am learning about properties statement. i
i have read on the msdn and know : properties statement used for declaring the name of a property, and the property procedures used to store and retrieve the value of the property.
i learned about structure of it. but i cant figure out it clearly. please give me a simple example for me understanding about it. thanks .  
this is structure of it :
Code:
Class Class1
' Define a local variable to store the property value.
Private PropertyValue As String
' Define the property.
Public Property Prop1() As String
Get
' The Get property procedure is called when the value
' of a property is retrieved.
Return PropertyValue
End Get
Set(ByVal Value As String)
' The Set property procedure is called when the value
' of a property is modified.
' The value to be assigned is passed in the argument to Set.
PropertyValue = Value
End Set
End Property
End Class
-
Jun 28th, 2009, 11:05 PM
#2
Re: Properties statement
You really have to understand what properties are for to be able to create them properly. The idea of a property is that it behaves just like a field from the outside but like method from the inside. Consider the following type with a public field:
vb.net Code:
Public Class SomeClass Public SomeField As String End Class
In code, you would get and set that field like so:
vb.net Code:
Dim obj As New SomeType obj.SomeField = "some value" Dim val As String = obj.SomeField
Clear enough? Properties work in exactly the same way from the outside. You get and set the value of a property in exactly the same way.
Now, what if that field was not allowed to be longer than 50 characters. How would you validate it? As it stands you can't. You could do what they do in Java and make the field private and then set it using a public method, but that becomes less intuitive for the person writing the code. This is where properties come in. They behave just like fields from the outside but they behave like methods from the inside, allowing you do extra work like validation and raising events. In its simplest form, the code above would become:
vb.net Code:
Public Class SomeClass Private someField As String Public Property SomeProperty() As String Get Return Me.someField End Get Set(ByVal value As String) Me.someField = value End Set End Property End Class
and you'd use it like so:
vb.net Code:
Dim obj As New SomeType obj.SomeProperty = "some value" Dim val As String = obj.SomeProperty
As you can see, that code is exactly the same as when using the public field. We now have the freedom to go into the Get and set methods of the property and add things like validation and event raising, e.g.
vb.net Code:
Public Property SomeProperty() As String Get Return Me.someField End Get Set(ByVal value As String) If value.Length > 50 Then Throw New InvalidArgumentException("Value must be no more than 50 characters long.") End If If Me.someField <> value Then Me.someField = value Me.OnSomePropertyChanged(EventArgs.Empty) End If End Set End Property
All that extra functionality has been added but the user still treats the property like a field from the outside.
-
Jun 29th, 2009, 01:16 AM
#3
Fanatic Member
Re: Properties statement
Property statement :
what are Properties? And what are they good for? Properties are a way of encapsulating data structures in a class. Usually, we declare data structures (such as variables) inside of a class, and we want other classes to have access to that data. We could do this by creating helper functions to set and retrieve the data. Or we could directly access the data. Better yet, we could use what are called properties (which are essentially helper functions, except they have loads of other useful features). It's better programming practice to indirectly access the data in classes. Properties allow you to indirectly access the data of a class, and allow additional coding before the value is set/returned.
A Basic Property
Lets take a look at a basic property in a class.
Code:
Public Class myClass
Private strName as string
Public Property Name()
Get
Name() = strName
End Get
Set(ByVal Value)
strName = Value
End Set
End Property
End Class
Lets look at this code. We have a class named myClass. And we have a property called Name. A property has two parts (unless its ReadOnly/WriteOnly, which we will get to later), a Get and a Set portion. As we mentioned earlier, properties typically encapsulate data, and in this case it is providing a way to access the data in strName (notice how the variable is declared as private to the class. We want only the class to have direct access to the data)
Under the Get portion, we see that we set the property (Name) equal to the local strName. This returns the value of strName to the caller. Under the Set portion, we set the local strName variable equal to Value. Value is a default variable that is created for properties, and holds the information passed by the caller.
Simple enough. Lets see how to use the property in code.
Code:
'declare and create a new instance of the class
Dim clsTest as myClass
clsTest = new myClass()
'Set the name property in the class to "Bob"
'This utilizes the Set portion
clsTest.Name() = "Bob"
'Retrieve the data from the Name property
'This utilizes the Get portion
MsgBox(clsTest.Name)
Properties are not limited to merely setting and retrieving data. They can contain code that can do a variety of tasks (for instance, if you want different data returned based on other things). You can include code and even catch exceptions in the property.
ReadOnly Properties
But what if you want a property to be read only (no Set portion)? Heres how:
Code:
Public ReadOnly Property Name()
Get
'Your code here
End Get
End PropertySimple as that. Just add the ReadOnly keyword before the Property keyword, and remove the Set portion of code.
WriteOnly Property
The WriteOnly property is near identical to the ReadOnly:
Code:
Public WriteOnly Property Name()
Set
'Your code here
End Set
End Property
Static Properties
Regular properties and data structures belong to the objects of the class (each new instance of the class has a new set of properties and data structures, independent of other classes). But sometimes you have a set of class instances that require shared data. Essentially, this means that each instance of the class has access to the data declared as Shared (instead multiple sets of data structures, there is one accessible by all class instances). Heres how to set up a property to access that shared variable:
Code:
Public Class myClass
'note the Shared keyword in the declaration of the variable and the property
Private Shared strName as string
Public Shared Property Name()
Get
Name() = strName
End Get
Set(ByVal Value)
strName = Value
End Set
End Property
End Class
Thats it. Everything else functions as a normal property, except its access the shared data from the class.
This just covers the basic usage of properties. There are many other features, such as Inheritance, Polymorphism, and Abstract Properties. But this will get you going.
-
Jun 29th, 2009, 01:28 AM
#4
Thread Starter
Member
Re: Properties statement
Thanks all of you. i am examing step by step your suggest.
if i have any trouble please explain for me./
thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|