CS0004: Introduction to Programming Input and Output
Review A String is… a sequence of characters that is treated as a single item A String literal is surround by… double quotes ( “” ) Typecasting is… converting from one type to another Explicit Typecasting Functions CDbl(parameter) – typecasts to double CInt(parameter) – typecasts to integer CStr(parameter) – typecasts to string Concatenation is… combining multiple strings into one string The concatenation operator is… &&
Review Two options that should be turned on in every program you write… Option Strict On Option Explicit On Some string methods and properties are… Length ToUpper ToLower Trim Substring IndexOf
Formatting Numbers VB provides functions for formatting numbers to familiar forms. Note: These all return strings Specify the number of decimal places FormatNumber(n,r) Example: FormatNumber( , 3) returns 1, Return in currency form FormatCurrency(n,r) Example: FormatCurrency( , 2) returns $1, Return in a percent form FormatPercent(n,r) Example: FormatPercent(0.3452, 2) returns 34.53%
Masked Text Box You can avoid doing data validation in some cases by forcing the user to input data in a specific form using a masked text box. Demo Create control Click on the triangle on the control Select “Set Mask” Custom Masks See course webpage
Date Data Type Visual Basic also provides a date data type Dim theDate As Date Date literals are surrounded by pound signs (#) Dim theDate As Date = #2/7/2011# You can convert dates into strings of different formats much like the number data types FormatDateTime(theDate, DateFormat.LongDate) See the course webpage for different date formats Some date functions Today Returns today’s date DateDiff(DateInterval.Day, date1, date2) Returns the number of days between date1 and date2 as an integer. See course webpage for different date intervals theDate.AddYears(n), theDate.AddMonths(n), and theDate.AddDays(n) Returns the date after adding n years, months, and days (respectively) to theDate. CDate(dateString) Typecasts the string dataString to a date data type
Date Example New Topics: Date date type Date literals Masked text box CDate typecasting Today function FormatDateTime function DateDiff function AddDays function
Input Dialog Box Sometimes we want to get a piece of information once and do not want to waste screen space with a text box and a label. For this we use an input dialog box. InputBox(prompt, title) prompt is the message displayed inside of the dialog box title is the text displayed in the title of the dialog box This function returns the string that is in the text box in the dialog box when the user presses OK. Thus, you want to store the result returned into a variable Dim userInput As String userInput = InputBox(“Please Enter Input”, “Input”)
Output Dialog Box You can also display a dialog box that takes no input, but just displays a message. This is called an output dialog box. MessageBox.Show(prompt, title) prompt and title are the same as in the input dialog box. This displays a dialog with the message and an OK button.
Dialog Boxes Examples New Topics Variable scope OnLoad event procedure Input dialog box Output dialog box
Named Constants Sometimes you want to define a variable that will keep the same value throughout the running of the program. These are called constants. Const CONSTANT_NAME As Type = value Notes: Const makes the constant not able to take a different value after initialization The name of the constant should be all in caps and have underscores instead of camel case for different words You need to initialize the constant for it to have a value