03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

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

Add<strong>in</strong>g Logic to Your User Controls x 297<br />

To add properties or methods to a user control, you add them to the Code Beh<strong>in</strong>d of the control.<br />

The properties you add can take various forms. In its simplest form, a property looks exactly like<br />

the properties you saw <strong>in</strong> Chapter 5. For more advanced scenarios you need to add View State properties<br />

which are able to ma<strong>in</strong>ta<strong>in</strong> their state across postbacks. In the next two exercises you see how<br />

to create both types of properties.<br />

Creat<strong>in</strong>g Your Own Data Types for Properties<br />

To make the banner control more useful, you can add a second image to it that displays a horizontal<br />

banner. You could also add a property to the control that determ<strong>in</strong>es whether to display the vertical<br />

or horizontal image. You could do this by creat<strong>in</strong>g a numeric property of type System.Byte. Then<br />

0 would be vertical <strong>and</strong> 1 would be horizontal, for example. However, this makes it hard to remember<br />

what each number represents. You can make it a bit easier by creat<strong>in</strong>g a Str<strong>in</strong>g property that<br />

accepts the values Horizontal <strong>and</strong> Vertical. However, str<strong>in</strong>gs cannot be checked at compile time,<br />

so you may end up with a spell<strong>in</strong>g mistake, result<strong>in</strong>g <strong>in</strong> an error or <strong>in</strong> the <strong>in</strong>correct banner be<strong>in</strong>g displayed.<br />

The .<strong>NET</strong> Framework supplies a nice way to solve this by enabl<strong>in</strong>g you to create your own<br />

data type <strong>in</strong> the form of an enumeration. With an enumeration, or enum for short, you assign numbers<br />

to human-friendly text str<strong>in</strong>gs. Developers then use this readable text, while under the hood the<br />

numeric value is used. The follow<strong>in</strong>g snippet shows a basic example of an enum (notice how the <strong>VB</strong><br />

example doesn’t use commas, whereas they are required <strong>in</strong> C#):<br />

<strong>VB</strong>.<strong>NET</strong><br />

Public Enum Direction<br />

Horizontal<br />

Vertical<br />

End Enum<br />

C#<br />

public enum Direction<br />

{<br />

Horizontal,<br />

Vertical<br />

}<br />

With these enums, the compiler assigns numeric values to the Horizontal <strong>and</strong> Vertical members<br />

automatically, start<strong>in</strong>g with 0 <strong>and</strong> count<strong>in</strong>g upward. You can also def<strong>in</strong>e numeric values explicitly if<br />

you want:<br />

<strong>VB</strong>.<strong>NET</strong><br />

Public Enum Direction<br />

Horizontal = 0<br />

Vertical = 1<br />

End Enum<br />

C#<br />

public enum Direction<br />

{<br />

Horizontal = 0,<br />

Vertical = 1<br />

}

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

Saved successfully!

Ooh no, something went wrong!