15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Classes ❘ 77<br />

This code shows how the color preference is stored in a static variable, which is initialized in the static<br />

constructor. This field is declared as read-only, which means that its value can only be set in a constructor.<br />

You learn about read-only fields in more detail later in this chapter. The code uses a few helpful structs that<br />

Microsoft has supplied as part of the Framework class library: System.DateTime <strong>and</strong> System.Drawing.<br />

Color. DateTime implements a static property — Now, which returns the current time — <strong>and</strong> an instance<br />

property — DayOfWeek, which works out what day of the week a date-time represents. Color (which is<br />

discussed in the online Chapter 48, “Graphics with GDI+”) is used to store colors. It implements various<br />

static properties, such as Red <strong>and</strong> Green as used in this example, which returns commonly used colors. To<br />

use Color, you need to reference the System.Drawing.dll assembly when compiling, <strong>and</strong> you must add a<br />

using statement for the System.Drawing namespace:<br />

using System;<br />

using System.Drawing;<br />

You test the static constructor with this code:<br />

class MainEntryPoint<br />

{<br />

static void Main(string[] args)<br />

{<br />

Console.WriteLine("User-preferences: BackColor is: " +<br />

UserPreferences.BackColor.ToString());<br />

}<br />

}<br />

Compiling <strong>and</strong> running this code results in this output:<br />

User-preferences: BackColor is: Color [Red]<br />

Of course if the code is executed during the weekend, your color preference would be Green.<br />

Calling Constructors from Other Constructors<br />

You may sometimes find yourself in the situation where you have several constructors in a class, perhaps<br />

to accommodate some optional parameters for which the constructors have some code in common. For<br />

example, consider this:<br />

class Car<br />

{<br />

private string description;<br />

private uint nWheels;<br />

public Car(string description, uint nWheels)<br />

{<br />

this.description = description;<br />

this.nWheels = nWheels;<br />

}<br />

public Car(string description)<br />

{<br />

this.description = description;<br />

this.nWheels = 4;<br />

}<br />

// etc.<br />

Both constructors initialize the same fields. It would clearly be neater to place all the code in one place. <strong>C#</strong><br />

has a special syntax known as a constructor initializer to allow this:<br />

class Car<br />

{<br />

private string description;<br />

private uint nWheels;<br />

public Car(string description, uint nWheels)<br />

{<br />

this.description = description;<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!