|
In a few of our examples now we've had to convert values from being strings to being integer values so that we can perform some math calculation on them. To do this, we've used the CInt() function, which is a quick 'n easy VB.NET-specific method for grabbing a string value and converting it to an integer. In a similar way, we can use CStr() and CBool() to convert to a string, or to a boolean value, and so on. The functions you'll probably use most often include:
Function | Result | Acceptable Inputs |
---|---|---|
CBool | Converts to a Boolean value | A string of True or False, or a numeric input of either zero (which converts to False), or any number other than zero (which converts to True) |
CDate | Converts to a date/time value | A string that is correctly formatted as a date or a time, for example, December 31, 2002, or 7:23:05 PM |
CInt | Converts to an integer | Any numerical value, including ones with decimals. The resulting Integer will be rounded to the nearest whole number. |
CStr | Converts to a string | Pretty much anything! |
When we convert from one type to another, we must take care to convert from and to values that are compatible. To demonstrate this, we've included a simple example for you to play with!
This example will give you a neat page that you can experiment with to better understand how type conversions work.
Create a new page and call it Conversions.aspx.
In the designer, type Original input: onto the page, then drag a TextBox control onto the page, give it an ID of txtValueToConvert. Then drag a Button control onto the page, set its ID property to btnConvert, and its Text property to Convert!
Type the following text, pressing Return after each line:
Convert to Integer produces: Convert to Date/Time produces: Convert to Boolean produces:
At the end of each line of text, drag a Label control onto the page, and name them lblToInt, lblToDateTime, and lblToBoolean respectively. Clear their Text properties and you should have the following:
Double-click the Convert button to switch to Code view, and enter the following code:
Sub btnConvert_Click(sender As Object, e As EventArgs) Try lblToInt.Text = CInt(txtValueToConvert.Text) Catch lblToInt.Text = "Could not convert to Integer" End Try Try lblToDateTime.Text = CDate(txtValueToConvert.Text) Catch lblToDateTime.Text = "Could not convert to Date/Time" End Try Try lblToBoolean.Text = CBool(txtValueToConvert.Text) Catch lblToBoolean.Text = "Could not convert to Boolean" End Try End Sub
Run the page, and try entering different types of values:
When we enter a value in a textbox on a page, we are entering a string of text into the text property of the textbox control. All of our conversions will therefore be converting from a String, to the type of our choice.
You'll notice in the code that we entered that there are a couple of new constructs in our code that we've not yet met. The Try ... Catch ... End Try blocks in our code are there to handle exceptions. If we try to convert to a different type that doesn't meet the acceptable conversion criteria, we will cause an exception to be raised. An exception, as we'll see in Chapter 7, is an unexpected error. The Try ... Catch statements handle this exception, by presenting the user with a simple error message, instead of an ASP.NET error page, allowing execution of the page to continue as normal.
So, there are two things to consider. Firstly, what happened when we typed in 23.5 into our example, as shown above, and secondly, what happens if we can't convert to a specific type? Let's start with the first question.
The string 23.5 is successfully converted to an Integer, and is rounded up to 24 (the nearest whole number)
23.5 is interpreted on my system (which is set to UK locale) to be the 23rd of May, 2002.
23.5 can be interpreted as a number that isn't zero, so it equates to True when converted to boolean
If you're in the US, or use US date and time settings, you would see an error message for the conversion to a date, because 23 isn't a month. In fact, let's look at the answer to the second question – what happens if you enter a value that can't be converted to the type you require?
We've entered text that is obviously a word. It's not a number, so it can't be converted to an Integer, it's not a date or a time, and it doesn't say True or False, so we can't convert it to a Boolean. Notice how we see text that states that there's been an error, but we don't get an error page. This is one of the great features of VB.NET that we'll look at in Chapter 7.
Before we move on, it's worth considering one more thing, which is that we are displaying our results in the Text property of a Label control, so we're actually doing an implicit conversion to type String in our code! This is a clear example of how versatile a String can be.
In addition to these built-in functions, we could also use the CType() function, which takes two arguments. The first is the value you want to convert, and the second is the type to which you want to convert, for example:
Dim IntegerAnswer as Integer IntegerAnswer = 42 Dim StringAnswer as String StringAnswer = CType(IntegerAnswer, String)
This function can take any Dimable type as the second argument, so we could use String, DateTime, Integer, and so on.
|