[ Previous ]
[ Index ]
[ Next ]
Properties--that's what it's all about. We all want to implement great properties, so we can get rich and own a lot of properties. Getters and setters, accessors and mutilators, call them what you like, but the functionality is still the same. What's new is the funky syntax used by .NET, though it will grow on you.
Basic properties are very simple and self-explanatory. Here are some examples in VB.NET:
Private mBasicProperty As String
Private mReadOnlyProperty As String
Private mWriteOnlyProperty As String
Public Property BasicProperty() As String
Get
Return mBasicProperty
End Get
Set(ByVal Value As String)
mBasicProperty = Value
End Set
End Property
Public ReadOnly Property ReadOnlyProperty() As String
Get
Return mReadOnlyProperty
End Get
End Property
Public WriteOnly Property WriteOnlyProperty() As String
Set(ByVal Value As String)
mWriteOnlyProperty = Value
End Set
End Property
The VB.NET IDE automatically stubs out the rest of a property for you after you type
the first line of the property. The parameter name in the Set property is always defaulted
to name Value. C# uses the keyword value and the parameter name is not even listed in the
property's declaration as shown below. Also note the syntactic difference between
read-only and write-only properties between VB.NET and C#.
private String mBasicProperty;
private String mReadOnlyProperty;
private String mWriteOnlyProperty;
public String BasicProperty
{
get
{
return mBasicProperty;
}
set
{
mBasicProperty = value;
}
}
public String ReadOnlyProperty
{
get
{
return mReadOnlyProperty;
}
}
public String WriteOnlyProperty
{
set
{
mWriteOnlyProperty = value;
}
}
With VB.NET you can create properties that take parameters when accessed, typically for accessing an array like so:
Private mWidgets(42) As Widget
Public Property Widgets(ByVal i As Integer) As Widget
Get
Return mWidgets(i)
End Get
Set(ByVal value As Widget)
mWidgets(i) = value
End Set
End Property
Note that the example above can throw a System.IndexOutOfRangeException.
Thrown exceptions are not documented within the code as they are in Java.
With VB.NET you can also define parameterized properties that take multiple arguments like this example:
Private mPixels(10, 10) As Pixel
Public Property Pixels(ByVal row As Integer, ByVal column As Integer) As Pixel
Get
Return mPixels(row, column)
End Get
Set(ByVal value As Pixel)
mPixels(row, column) = value
End Set
End Property
C# programmers obviously can't handle the mental load of parameterized properties
like VB programmers can, so Microsoft has valiantly eased their burden by providing
only one "option", the indexer. The indexer is always this:
private Object[] mThese;
public Object this[int i]
{
get
{
return mThese[i];
}
set
{
mThese[i] = value;
}
}
It, or rather this, allows you to expose an array or collection of objects on the
name of the object. This allows you to write code like this:
myObject(123) = yourObject;
If you come from a Microsoft COM background, the line of code above may remind
you of a default property. In fact it is (or rather, this is). A default
property (only one per class) is invoked by default for developers who are too
dumb to use Intellisense. An indexer is the only way to define a default
property in C#.
VB.NET gives you more freedom over your properties. You can make any property default by adding the Default keyword:
Default Public Property Widgets(ByVal i As Integer) As Widget
'My property is in default.
End Property
If you're developing components to be used by other members of your development team, a fun thing to do is to move the Default keyword around to a different property before you check in your code into the version control system. Fun for hours!
There are some basic topics regarding methods that we should briefly cover.
To create static methods, i.e. methods callable without using a class instance, the keywords to use are Shared in VB.NET and static in C# as shown in this VB.NET example:
Public Shared Sub CallMeAnytimeBaby()
'Callable without a class instance.
End Sub
And this C# example:
public static void CallMeAnytimeBaby()
{
// Callable without a class instance.
}
VB.NET provides the same two ways of passing parameters to a method as
did VB "Classic", ByVal and ByRef. I'm not going to go into the details of
these since I think most readers are familiar with them; however, C# introduces
a couple of new keywords that are worth taking a more detailed look at, namely
out and ref.
These are used when passing parameters by reference in C#. Both cause a pointer to
be passed. The difference lies with who has the responsibility for initializing the
object being referred to. If ref is used, then the object should be initialized before
being passed. If out is used, then the object must be initialized within the method and
"returned out" to the caller.
In the following example, the PopulateOuthouse method returns a bool as a status flag
indicating whether the method succeeded. Because our Outhouse Management program can
handle many outhouses, we also need to return the assignedOuthouseID that was populated.
The compiler is helping us out (pun intended) by highlighting the fact that we have not
initialized this parameter before returning from the method:
C# allows you to overload operators. VB.NET does not. An overloaded operator must be
public and static and can be defined
for both structs and classes. The operator keyword
is used as shown below:
public static Vector operator + (Vector a, Vector b)
{
return new Vector( a.x + b.x, a.y + b.y );
}
Note the space between operator and + as
opposed to C++ where it would be
defined as "operator+". Just remember that .NET adds more space in order to provide
the code a breath of fresh air.
[ Previous ]
[ Index ]
[ Next ]