LEVERAGING THE POWER OF VISUAL BASIC S BUILT-IN FUNCTIONS AND .NET OBJECTS


LEVERAGING THE POWER OF VISUAL BASIC'S BUILT-IN FUNCTIONS AND .NET OBJECTS

Although the ability to create your own procedures is a very powerful programming tool, allowing you to perform all sorts of tasks, it is not always necessary for you to develop all of the programming logic required to perform a given task. Instead, you can often take advantage of Visual Basic's built-in collection of functions as well as properties and methods provided by .NET objects. By leveraging the functionality provided by these functions and objects, you can save a lot of development time.

You've already learned how to work with a number of built-in Visual Basic functions. One good example has been the use of the InputBox() function as a means of collecting input from the player during game play. This function allows you to display a text string and to collect and return text input provided by the player, as demonstrated in the following example.

 Dim strUserReply As String = "" strUserReply = InputBox("Please enter your first name.", _   "Data Collection example") 

The pop-up window produced by this example is shown in Figure 8.10.

image from book
Figure 8.10: Using the Visual Basic's InputBox() function to display a pop-up dialog that collects user input.

You could, of course, create your own custom InputBox()-like function by adding a new form to a Visual Basic project and then adding a Label, a TextBox, and two Button controls to it, followed by the programming logic required to tie everything together. However, unless the InputBox() function fails to provide you with a particular piece of functionality that you absolutely must have when prompting the user for input, there is no reason to take the extra time to create your own function.

Date-Related Objects

The .NET Framework provides numerous objects that assist you in working with the date and time. For example, the DateTime object's Now() method can be used to retrieve the current system date and time, as demonstrated below.

image from book
DEFINITION

The DateTimeObject is used to represent a moment in time. It's Now property retrieves the current date and time from the computer.

image from book

 MessageBox.Show(DateTime.Now()) 

When executed, this statement produces the pop-up window shown in Figure 8.11.

image from book
Figure 8.11: Using the DateTime.Now() function to display the current date and time.

You can also use the Convert object's ToDateTime method to convert a string value to a Date data type as demonstrated below.

 Dim strDate As String Dim dteDate As Date strDate = "12/01/05" dteDate = Convert.ToDateTime(strDate) MessageBox.Show(dteDate) 

image from book
DEFINITION

The Convert object provides the ability to convert or change a data type from one type to another. It's ToDateTime method provides the ability to convert numerous data types to a Date data type.

image from book

The following example demonstrates how to use the TimeSpan object's Subtract method to determine the number of days that have elapsed from the specified date to the current date. In the case of this example the specified date is November 11, 1964.

 Dim dteCurrentDate As Date = DateTime.Now Dim dteBirthDate As Date = #11/20/1964# Dim dteDateDiff As TimeSpan dteDateDiff = dteCurrentDate.Subtract(dteBirthDate) MessageBox.Show("I was born " & dteDateDiff.Days & " days ago.") 

The first two statements in this example declare variables that store the current date and a date representing somebody's birthday. The third statement declares a variable named dteDateDiff as an instance of the TimeSpan object. The next statement uses the TimeSpan object's Subract method. The Subtract method is used to determine the amount of time that has passed between the two time periods. The value assigned to dteDateDiff is then displayed in the final statement which used the TimeSpan object's Days property to specify the unit of measurement to be used to represent the time span between the two time frames.

image from book
DEFINITION

The TimeSpan object is used to represent an interval of time.

image from book

Figure 8.12 shows the output produced by the previous example when it is executed.

image from book
Figure 8.12: Using the TimeSpan object's Subtract method to count the number of days between two dates.

Before moving on, let's take a look at one last example of how to programmatically work with date and time values using the DateTime object's ToString method. The ToString method provides the ability to convert a date\time value to a specified string format. For example, using the DateTime object's ToString method you can create your own Visual Basic digital clock, as shown in Figure 8.13.

image from book
Figure 8.13: Using date-related functions to create a digital clock.

The code associated with the digital clock example is shown below and is available for download from this book's companion Web site.

 Public Class Form1     Private Sub Form1_Load(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles MyBase.Load         txtDate.Text = DateTime.Now.ToString("D") 'Long Date pattern     End Sub     Private Sub Timer1_Tick(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles Timer1.Tick          lblTime.Text = DateTime.Now.ToString("T")         If DateTime.Now.ToString("T") = #12:00:00 AM# Then             txtDate.Text = DateTime.Now.ToString ("D")         End If     End Sub End Class 

In this example, the current date is displayed when the Form1_Load event procedure executes. The current date is collected using the DateTime object's Now method and then formatted using the object's ToString method. The ToString method is passed an argument of "D", specifying that the date be displayed in a long date format as shown in Figure 8.13.

The Timer_Tick event procedure updates the display of the current time every second by passing the ToString method an argument of "T", specifying a long time format. The Timer control that drives this procedure was enabled at design time, and its Interval property was set equal to 1000 so that the procedure starts executing the moment the application is loaded.

The Timer_Tick event procedure is also responsible for updating the display of the current date, which it changes automatically at 1 second past midnight.

String Manipulation Functions

Strings are a commonly used data type in Visual Basic applications. Applications need to work with string values in a number of different ways, including:

  • Searching for one string within another

  • Parsing string contents to extract a port of the string

  • Formatting the contents of a string

The .NET Framework provides Visual Basic with an abundance of different String object methods for manipulating strings. For example, using the ToString method, you can convert a value to a String data type, as demonstrated below.

 Dim intValue As Integer = 12345 Dim strSample As String = intValue.ToString() 

In this example, the String object's ToString method is used to convert the number 12345 to a string, which is then assigned to a variable named strSample. The following list provides a sampling of different types of methods and properties provided by the String object.

  • Trim. A method used to remove leading and trailing blank spaces from a value

  • ToUpper. A method that retrieves an all upper case version of a string

  • ToLower. A method that retrieves an all lower case version of a string

  • Length. A property used to return the number of characters that make up a string

  • Substring. A method used to return a value indicating the starting location of one string within another

The following example demonstrates how to use each of these String manipulation functions.

 Dim strDataString As String = "    Once upon a time...    " Dim strTestString As String = "" Dim intLength As Integer = 0 'Assigns "Once upon a time..." strTestString = strDataString.Trim 'Assigns "   ONCE UPON A TIME...    " strTestString = strDataString.ToUpper 'Assigns "   once upon a time...    " strTestString = strDataString.ToLower 'Assigns 25 intLength = strDataString.Length 'Displays "Once upon" strTestString = strDataString.Substring(3, 9) strDataString.Trim 

Note that the value that is assigned or displayed by each statement in the above example is shown as a comment that precedes each statement.




Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
ISBN: 1592008143
EAN: 2147483647
Year: 2006
Pages: 126

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net