Maximizing.ASP.dot.NET.Real.World.ObjectOriented.Development [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Maximizing.ASP.dot.NET.Real.World.ObjectOriented.Development [Electronic resources] - نسخه متنی

Jeffrey Putz

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید






Properties and Private Variables


Your classes will likely need to manipulate some kind of data. We can pass this data in through the constructors or other methods and get data back out by those methods. For classes that don't expose a lot of functionality, using return values from methods and parameters is fine, but for more complex classes, we'll need a place to store data.

Private members are members that fall into class scope, and the class can access them internally. They are not exposed as a part of the instantiated object. The simplest private members are variables used by other members in the class. They're straightforward to define, and you may optionally assign a value to them:

Listing 2.6. Declaring private variables

C#


private string _myString;
private int _myInt = 3;

VB.NET


Private _myString As String
Private _myInt As Integer = 3


Naming conventions for class members vary widely depending on other languages you've used, who you work with, your experience, your teammates' experience, and so on. There is no "right" way, but here are some suggestions you might want to consider. Most shops agree that private variables should begin with an underscore and use camel-case (first letter lower-case, each word thereafter capitalized). Use of Hungarian notation, where you prefix variables with something that indicates their type (i.e., strMyString or intMyInt), is discouraged because most development environments, and Visual Studio in particular, let you know what type a variable is when you mouse over it, though this often makes it harder to read the code in samples online or in books. Don't let developer snobbery dictate what you use, unless of course it's a convention in your shop.

For more information on Microsoft's suggested naming conventions, check out: http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconnamingguidelines.asp

Although you can set the values for private variables right there in the class declaration, you shouldn't. Instead, assign the values in your constructors. This keeps all of your assignments in one place, so you won't have to search your code. Not only that, but if you have several different constructors, you also may want to assign different values depending on which constructor is called.

A private variable might be used in one method and then later be used by another method during the lifetime of an object. The calling code outside of the class can't access this variable as a member of the object instance. For example, if the previous example of _myString was used internally by a class that we've instantiated into an object called MyObject, we couldn't access it with MyObject._myString.

Properties really go hand-in-hand with using private variables. A property is the public face of data for an instantiated object. Going back to our car object, the Color property was something we could get and set from the object. Properties have a special structure that "get" and "set" their values, and we store those values in matching private variables. If our Color property is a string, we'll need to declare the private variable as well as the property used by code that instantiates the class into an object. Listing 2.7 shows this structure.

Listing 2.7. A basic property and a matching private variable

C#


private string _color;
public string Color
{
get { return _color; }
set { _color = value; }
}

VB.NET


Private _color As String
Public Property Color() As String
Get
Return _color
End Get
Set
_color = value
End Set
End Property

The special value keyword is used in the property structure to represent the value being assigned to the property. The code in get is called when the property is referenced, and the code in set is called when a new value is assigned to the property.


Trace.Warn(MyCar.Color); // get called
MyCar.Color = "Red"; // set called

This is not the place to write your code to manipulate data. Properties are intended only to hold your data, not to manipulate it. You'll manipulate data in methods.

Properties can be read-only. To implement this in C#, you drop the set method, and in VB, you need to add the ReadOnly keyword, as shown in Listing 2.8.

Listing 2.8. A read-only property

C#


private string _color;
public string Color
{
get { return _color; }
}

VB.NET


Private _color As String
Public ReadOnly Property Color() As String
Get
Return _color
End Get
End Property


/ 146