03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Data Types <strong>and</strong> Variables x 151<br />

SelectedValue of a DropDownList. To get a str<strong>in</strong>g representation of an Object, you can call its<br />

ToStr<strong>in</strong>g() method. Every object <strong>in</strong> the .<strong>NET</strong> world supports this method, although the exact<br />

behavior may differ from object to object. For now, it’s important to underst<strong>and</strong> that ToStr<strong>in</strong>g is a<br />

method — or an operation — on an object, like a Str<strong>in</strong>g or a Double <strong>and</strong> even the parent Object<br />

itself. You learn more about methods <strong>and</strong> objects later <strong>in</strong> this chapter when object-oriented programm<strong>in</strong>g<br />

is discussed.<br />

Us<strong>in</strong>g ToStr<strong>in</strong>g() is easy, as the follow<strong>in</strong>g example that outputs today’s date <strong>and</strong> time on a Label<br />

control demonstrates:<br />

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

Label1.Text = System.DateTime.Now.ToStr<strong>in</strong>g()<br />

C#<br />

Label1.Text = System.DateTime.Now.ToStr<strong>in</strong>g();<br />

Another way to convert data types is by us<strong>in</strong>g the Convert class.<br />

NOTE Classes are an important concept <strong>in</strong> .<strong>NET</strong>, so they are discussed <strong>in</strong> their<br />

own section later <strong>in</strong> this chapter. For now it’s important to underst<strong>and</strong> that a<br />

class is like a bluepr<strong>in</strong>t for objects that are used <strong>in</strong> .<strong>NET</strong>. You can create your<br />

own classes, but you will also use many of the st<strong>and</strong>ard classes that are part of<br />

the .<strong>NET</strong> Framework.<br />

The Convert class conta<strong>in</strong>s functionality to convert a number of data types <strong>in</strong>to another type. The<br />

follow<strong>in</strong>g is a simple example of convert<strong>in</strong>g a Str<strong>in</strong>g conta<strong>in</strong><strong>in</strong>g a value that looks like a boolean<br />

<strong>in</strong>to a true Boolean type:<br />

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

Dim myBoolean1 As Boolean = Convert.ToBoolean("True") ' Results <strong>in</strong> True<br />

Dim myBoolean2 As Boolean = Convert.ToBoolean("False") ' Results <strong>in</strong> False<br />

C#<br />

bool myBoolean1 = Convert.ToBoolean("True");<br />

bool myBoolean2 = Convert.ToBoolean("False");<br />

// Results <strong>in</strong> true<br />

// Results <strong>in</strong> false<br />

Besides the ToBoolean method, Convert offers you a host of other conversion methods, <strong>in</strong>clud<strong>in</strong>g<br />

ToInt32 (for <strong>in</strong>teger types), ToDateTime (for dates), <strong>and</strong> ToStr<strong>in</strong>g.<br />

Another way to convert one type <strong>in</strong>to another is by us<strong>in</strong>g cast<strong>in</strong>g. With cast<strong>in</strong>g you actually force<br />

one type <strong>in</strong>to another, which is different from convert<strong>in</strong>g, <strong>in</strong> which the underly<strong>in</strong>g value of a data<br />

type is transformed <strong>in</strong>to a new value.<br />

Cast<strong>in</strong>g only works for compatible types. You can’t, for example, cast a DateTime <strong>in</strong>to an Integer.<br />

You can, however, cast similar types, like a Double to an Integer or a Str<strong>in</strong>g to an Object.<br />

The reverse of the latter example isn’t always true. Earlier I said that every data type <strong>in</strong> the .<strong>NET</strong><br />

Framework is based on the Object data type, mean<strong>in</strong>g that, for example, a Str<strong>in</strong>g is an Object.

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

Saved successfully!

Ooh no, something went wrong!