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#
VB.NET
private string _myString;
private int _myInt = 3;
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 |
Listing 2.7. A basic property and a matching private variable
C#
VB.NET
private string _color;
public string Color
{
get { return _color; }
set { _color = value; }
}
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.
Private _color As String
Public Property Color() As String
Get
Return _color
End Get
Set
_color = value
End Set
End Property
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.
Trace.Warn(MyCar.Color); // get called
MyCar.Color = "Red"; // set called
Listing 2.8. A read-only property
C#
VB.NET
private string _color;
public string Color
{
get { return _color; }
}
Private _color As String
Public ReadOnly Property Color() As String
Get
Return _color
End Get
End Property