VB.net Getting Started (10): Add: Class ~ Attribute

Source: Internet
Author: User
Tags integer
Just found that the class has not finished writing, there is a larger part: properties.

property is unheard of in both Java and C + +. It is generally assumed that all classes have members and methods. As for attributes, when used, properties look like members, attributes and members have many similarities. But attributes have more unique, it can be said to be a major feature of VB. My understanding is that attributes exist for better handling of members.

A property is something that looks like a member, but in essence it is a function. The definition of a property is this:
Public Property MyProperty () as Object ' MyProperty is the name of the attribute
Get ' This place can't play parentheses
Return Mvarmyproperty
End Get

Set (Byval Value as Object)
Mvarmyproperty = Value
End Set
End Property

As you can see, the properties are actually composed of two functions: Get and Set (). When we read the attribute, it calls get, so there must be a return statement in the get, and when the property is set, it calls set (), so set () is a parameter. When the outside reads MyProperty, MyProperty returns to Mvarmyproperty. Mvarmyproperty is a variable that is declared well in advance. When the outside setting is MyProperty, the assigned value is assigned to Mvarmyproperty. Here, mvar-is the default prefix used in VB6, indicating that it is a variable that holds the attribute.

As you may understand here, the property itself does not save the data, it is read and written through another variable. But this is another strange thing: is it necessary to be so troublesome? I write directly:
Public MyProperty as Object

Isn't it simpler?
Here's a simple explanation: by property, we can hide real members, like Mvarmyproperty, which we can define as:
Private Mvarmyproperty as Object

In this way, the outside world can not see mvarmyproperty, can only see MyProperty. When the outside read and write to it, there is a process in the middle to avoid the incorrect assignment of members. In fact, in this case, Java and C + + also have a similar approach, is to use the Setmyproperty () function and the Getmyproperty () function. VB use property This special format to do together, it is more simple and convenient.

OK, here, you don't want to see the complete example? A human class has been written here, with an age attribute. Let's see how it prevents a person's age from being set to a negative value:
Imports System

Public module MyModule
Sub Main
Dim Laowang As New Human
Laowang.name = "Old King"
Laowang.age = 52
Laowang.age = 330 ' Does this sentence have the old King's age set to 330 years old? Look at the result of the next sentence and know it.
Console.WriteLine ("{0}" is now {1} years old.) ", Laowang.name, Laowang.age)
Console.ReadLine ()
End Sub
End Module

public class Human
Public Name as String
Dim mvarage As Integer ' here does not indicate whether public or private, because the default state is private

Public Property Age () As Integer
Get
Return Mvarage
End Get
Set (Byval Value As Integer)
If value<=0 or value>=200 then ' normal age should not be less than 1 or greater than 200
Console.WriteLine (Value &) year old? Am I dead? ")
Else
Mvarage = value
End If
End Set
End Property
End Class

Here you should close your eyes and repose for a while: The original attribute is like this!








But the topic is not finished yet.

For example, if a member is an array, how can I create a property for it? For each of these elements? What if the size of the array changes? The property is not so stupid. Let's give an example. For example, we add an array member children to the human class, indicating how many children a person has. We first define Mvarchildren:
Dim Mvarchildren () as Human

There are two ways to establish a property for it. One is to set the type of the property directly to an array:
Public Property Children () as Human ()
Get
Return Mvarchildren
End Get
Set (Byval Value as Human ())
Mvarchildren = value
End Set
End Property

Then we can use this property as if we were using an array.

The other is to pass in the parameter index when reading the property:
Public Property Children (ByVal index as Integer) as Human
Get
Return Mvarchildren (Index)
End Get
Set (ByVal Value as Human)
Mvarchildren (Index) = Value
End Set
End Property

This allows the incoming subscript to be checked.
It is mentioned here that the parameters can be given when reading properties. This is a very interesting thing. For example, the old King has 3 children, one of them is called "Wang Hua". I want to get this kid by name, I can write a function
Public Function Getchildbyname (Byval Name As String) as Human ' content omitted

and then call
Laowang.getchildbyname ("Wang Hua")

It's OK.
To write a property, we can say this:
Public Property Child (ByVal Name As String) as Human
Get
Return Getchildbyname (Name)
End Get
Set (ByVal Value As Human)
Getchildbyname (Name) = Value
End Set
End Property

In this way, we can directly use:
Laowang.child ("Wang Hua"). Age = 20

Isn't that convenient?





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.