Results 1 to 4 of 4

Thread: Properties statement

  1. #1

    Thread Starter
    Member
    Join Date
    May 2009
    Location
    VietNam
    Posts
    56

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Public Class SomeClass
    2.  
    3.     Public SomeField As String
    4.  
    5. End Class
    In code, you would get and set that field like so:
    vb.net Code:
    1. Dim obj As New SomeType
    2.  
    3. obj.SomeField = "some value"
    4.  
    5. 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:
    1. Public Class SomeClass
    2.  
    3.     Private someField As String
    4.  
    5.     Public Property SomeProperty() As String
    6.         Get
    7.             Return Me.someField
    8.         End Get
    9.         Set(ByVal value As String)
    10.             Me.someField = value
    11.         End Set
    12.     End Property
    13.  
    14. End Class
    and you'd use it like so:
    vb.net Code:
    1. Dim obj As New SomeType
    2.  
    3. obj.SomeProperty = "some value"
    4.  
    5. 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:
    1. Public Property SomeProperty() As String
    2.     Get
    3.         Return Me.someField
    4.     End Get
    5.     Set(ByVal value As String)
    6.         If value.Length > 50 Then
    7.             Throw New InvalidArgumentException("Value must be no more than 50 characters long.")
    8.         End If
    9.  
    10.         If Me.someField <> value Then
    11.             Me.someField = value
    12.             Me.OnSomePropertyChanged(EventArgs.Empty)
    13.         End If
    14.     End Set
    15. End Property
    All that extra functionality has been added but the user still treats the property like a field from the outside.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    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.
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  4. #4

    Thread Starter
    Member
    Join Date
    May 2009
    Location
    VietNam
    Posts
    56

    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
  •  



Click Here to Expand Forum to Full Width