Discussion on the use of the properties in .NET (2)

zhaozj2021-02-16  53

Discussion on the use of the properties in .NET (2)

Codeprince http: //tangyong.net@163.com

3. Problems need to pay attention to in writing in the GET process

When defining a scalar attribute, as the cityname defined above, this is a readable and writable attribute, of course, you can define it as a read-only attribute. We need to consider how to write a GET process. (More people are the same as I started, don't do anything, write a return statement, I want to say this is safe, but we still need to know some other things). The first question I want to say is: GET function should not have any obvious side effects. In other words, you can't afford to get the status of this class, for example, you write the property of the cityname, but you Want to reflect some relationship between urban and cities through CityName, which will increase the side effect of CityName, which is a bad behavior. why? This is because calling the GET process is logically similar to the access field. If we discover your own updated in the GET function, then my suggestion (also the recommendation of Andy Olsen and other experts) is to implement this GET process as a method. The method is that it can also generate side effects.

Maybe someone will say that, since this, is the method not replaced attributes, is it to do it? Is this in itself an alternative and included relationship! In fact, I think so too, but I have to say that there are three situations that may touch your heart to use attributes.

1. To get the existing field value in the class. In this case, the GET function is only used as a package that has already appeared in the class. The GET function returns the value of the private field. (This is also our most often encountered)

2. To calculate the derived value in the class. In this case, the GET process uses data in the class to calculate the new value, and for object-oriented analysis and design, this is called class derived characteristics.

3. To perform "lazy" initialization. (The private field in the initialization class) In this case, the class has a rare field, or some fields are not expected to be values ​​when creating objects (of course, the compiler can assign a default) The value), for efficiency, we can postpone the initialization of this field until it passes through the SET process (or other method displayed). When the user needs this field, we will get it through the GET process. If the client application never requests this field, then we will never use it, so it improves performance.

An example is analyzed below:

Class City

Private mcityName as string 'name

Private MbuildTime as datetime 'Construction Time

Private MchargedRegionsquares As String 'The regional area jurisdiction

Public Sub New (Byval CityName As String, ByVal Buildtime As DateTime)

MCITYNAME = cityname

MBUILDTIME = CityHistory

Mchargedregionsquares = ""

End Sub

Public property cityname () AS STRING

Get

Return MCITYNAME

END GET

Set (byval value as string)

MCITYNAME = Value

End set

End Property

The 'cityhistory property represents the city existing time public readonly property cityhistory () AS integerget

Dim today as datetime = datetime.now

DIM Years as integer = datetime.now.year-mbuildtime.year

IF (Today.Month

(Today.Month = MBUILDTIME.MONTH AND

Today.day

Years- = 1

END IF

Return Years

END GET

End Property

Public Readonly Property ChargedRegionsquares () AS String

Get

IF (MchargedRegion = "" THEN

DIM CONN AS OLEDBCONNECTION

DIM CMD AS OLEDBCommand

Try

CONN = New OLEDBCONNECTION ("provider = ........." & _

"DataSource = city.mdb")

Cmd = new oledbcommand ("SELECT Regions" from

CityRegions where cityname = '"& mname &"' ",

CONN)

MchargedRegionsquares = cmd.executescalar ()

IF mcharedregionsquares is nothing then

MchargedRegionsquares = ""

END IF

Catch ex as oledbexception

Console.Writeln ("Error Occurred: {0}", EXMESSAGE)

Finally

IF not conn is nothing then

CONN.CLOSE ()

END IF

END TRY

END IF

Return MchargedRegionsquares

END GET

End Property

END CLASS

We now come to analyze the above code: (for the above code to use the database connection, we need to display imports system.data.oledb, which is required.)

1. The city class has the name of the city, the construction time of the city, the three private fields of the city's jurisdiction.

2. Construct the function to initialize the city's name and city construction time. These two private fields, and initialize the city's jurisdiction to to indicate that it will be read when the database will be read.

3. The CityName property gains and sets the MCITYNAME field without deeper processing, which is thinner to encapsulate the properties of MCITYNAME.

4. The cityHistory property calculates the city's existence time according to the construction time of the city and the current system time. This is an example of a significant derived character. Note: The cityhistory seemed to be more than the existing fields in the field.

5. The ChargedRegionsQuares property can be lazy initialization. In the above example, the region of the city's jurisdiction is in the database, so we do not ask for it in the constructor, because this can be obtained with the database connection when the customer code needs to use this property. This significantly improves efficiency. 6. We joined the error and exception processing in the code above, which is necessary for a robust-oriented program, I believe everyone is the same.

转载请注明原文地址:https://www.9cbs.com/read-24665.html

New Post(0)