Appendix B-1
aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf
Methods and Functions for Working with Dates, Financial Calculations,
Mathematics, and String Operations
Appendix BB
McGraw-Hill © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Appendix B-3
The DateTime Structure
Use the date functions and the methods of the DateTime structure to: Retrieve the system date Break down a date into component parts Test whether the contents of a field are compatible with
the Date data typeConvert other data types to a date
Appendix B-4
Retrieving the System Date and Time
Use the Now property or the Today property to retrieve the system date and time for the computer’s clockToday retrieves only the date
ExampleDim dateAndTimeDate As DatedateAndTimeDate = NowDim todaysDate As Date = Today
Appendix B-5
Converting Values to a Date Format
To store values in a Date data type convert the value to a Date type
The Date.Parse method and the Convert.ToDateTime method convert a value to Date type Throw an exception if unable to create a valid date from
the argument-use a Try block to make sure to have a valid date value and catch the exception
TryaDate = Date.Parse(Me.dateTextBox.Text)CatchMessageBox.Show("Invalid date.")End Try
Appendix B-6
Finding the Difference Between Dates
Use the Subtract method to find the difference between two Date objectsThe result is in the format of days, hours, minutes, and
secondsThe .NET Framework includes the TimeSpan class
that stores the time differences with the properties that are needed
Dates can be set at a specified time in the future
Appendix B-7
Checking for the Day of the Week
Sometimes a program needs to check for the day of the week for a meeting or an appointment, or rates that differ between weekend and week day
If enteredDate.DayOfWeek = DayOfWeek.Saturday Or _enteredDate.DayOfWeek = DayOfWeek.Sunday ThenweekendCheckBox.Checked = TrueElseweekendCheckBox.Checked = FalseEnd If
Appendix B-8
Financial Functions
VB provides functions for many types of financial and accounting calculationsEliminates the need to know and code the actual
formulasFinancial functions return a value that can be assigned
to a variable, property, or controlEach function needs to have the necessary arguments
Appendix B-9
Mathematical Functions
In Visual Studio.NET, the mathematical functions are included as methods in the System.Math classTo use the methods either import System.Math or refer
to each method with the Math namespace
Appendix B-10
Working with Strings
VB provides many methods for working with text strings
Strings in VS are immutable-once a string is created it cannot be changedStrings seem to be modified but actually a new string is
created when a change is neededStringBuilder class can be used when building or
extensively modifying strings
Appendix B-11
Functions for Checking Validity
The VB namespace also contains functions that can be used to check for validity or type
The preferred technique is to use the methods in the .NET Framework
Appendix B-12
Functions for Formatting Output
VB has some formatting functions which are included in the VB namespace
Using the ToString method, rather then the VB functions is the preferred technique for compatibility among .NET languages