LEVERAGING THE BUILT-IN FUNCTIONS AND .NET CLASSES


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

You've already learned how to work with a number of built-in Visual C++ functions. One good example has been the use of the MessageBox::Show method, which provides a means for displaying information in its own window.

You could, of course, create your own custom MessageBox::Show method by creating a form and adding various labels and buttons to it, followed by the programming logic required to tie everything together. However, unless the MessageBox::Show function fails to provide you with a particular piece of functionality, there is really no reason to duplicate the code that Visual C++ provides.

Date-Related Classes

The .NET Framework provides numerous classes that assist you in working with the date and time. For example, the DateTime class's Now() method can retrieve the current system date and time, as demonstrated here:

 MessageBox::Show( DateTime::Now.ToString() ); 

Hint 

The DateTime class represents a moment in time. Its Now property retrieves the current date and time from the computer.

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

image from book
Figure 8.10: 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 here:

 DateTime dteDate; String^ strDate = gcnew String( "12/01/05" ); dteDate = Convert::ToDateTime( strDate ); MessageBox::Show( dteDate.ToString() ); 

Hint 

The Convert class allows you to convert or change a data type from one type to another. Its ToDateTime method allows you to convert numerous data types to the Date data type.

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:

 DateTime dteCurrentDate = DateTime::Now; DateTime dteBirthDate; TimeSpan dteDateDiff; dteBirthDate = Convert::ToDateTime( "03/16/1965" ); dteDateDiff = dteCurrentDate.Subtract( dteBirthDate ); MessageBox::Show(  String::Concat( "I was born ",  dteDateDiff.Days, " days ago." ) ); 

Hint 

The TimeSpan object represents an interval of time.

The first three statements in this example declare objects that store the current date, a hypothetical birth date, and an object to hold the difference between the two. The dteCurrentDate object is initialized with the current date, which is retrieved from DateTime::Now. The dteBirthDate object uses the Convert::ToDateTime method to convert the birth date. The difference is then assigned to dteDateDiff by calling dteCurrentDate.Subtract( dteBirthDate). The result is shown using MessageBox::Show.

String Manipulation Functions

Strings are a commonly used data type in Visual C++ applications. Applications need to work with string values in various ways, including the following:

  • Searching for one string within another

  • Parsing string contents to extract part of a string

  • Formatting the contents of a string

The .NET Framework provides Visual C++ with abundant methods for manipulating strings. For example, using the ToString method, you can convert a value to a String data type, as demonstrated here:

 Double dblValue = 123.5; strVal = dblValue.ToString(); 

In this example, the Double object's ToString method converts the number 123.5 to a string, which is then assigned to a variable named strVal. The following list provides a sampling of different types of methods and properties provided by the String object:

  • Trim. A method that removes leading and trailing blank spaces from a value

  • ToUpper. A method that retrieves an all-uppercase version of a string

  • ToLower. A method that retrieves an all-lowercase version of a string

  • Length. A property that returns the number of characters that make up a string

  • Substring. A method that returns a value indicating the starting location of one string within another

  • Contains. A method that determines whether one string is contained within another

  • Concat. A method that creates a new string from multiple strings

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

 String^ strSentence = gcnew String( "The quick brown fox" ); strSentence = String::Concat( strSentence, " jumped over the", " lazy dog!" ); 

In this example, a new string is declared that contains the phrase The quick brown fox. The string is then appended using the String::Concat method. The first parameter is the string itself, strSentence. This ensures that the current contents are not erased. Next, two sentences are added together along with strSentence. The result is a completely new sentence, which is assigned to strSentence.




Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
ISBN: 735615381
EAN: N/A
Year: 2005
Pages: 131

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