visual basic - intrinsic functions

13

Upload: kerry

Post on 20-Jan-2016

82 views

Category:

Documents


9 download

DESCRIPTION

Visual Basic - Intrinsic Functions. Intrinsic Functions. Pre-coded Functions Used to improve developer productivity Broad Range of Activities Math calculations Time/Date functions String formatting Many more. Intrinsic Functions. Functions ALWAYS return a value - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Visual Basic - Intrinsic Functions
Page 2: Visual Basic - Intrinsic Functions

Intrinsic FunctionsPre-coded FunctionsUsed to improve developer productivityBroad Range of Activities

Math calculationsTime/Date functionsString formattingMany more

Page 3: Visual Basic - Intrinsic Functions

Intrinsic FunctionsFunctions ALWAYS return a value

You MUST do something with the value that is returnedRight hand side of an assignment statementPart of an Output Statement

Page 4: Visual Basic - Intrinsic Functions

Examples of Intrinsic FunctionsFunction Description Example Result

CDbl Returns a double if the item can be converted(can handle currency)

CDbl (“23.5”)CDbl (“$32.00”)CDbl (“hello”)

23.532Type Mismatch Error

Val Returns the numbers contained in the parenthesis(stops at 1st non numeric item)

Val (txtInput.text)Val (“$32.00”)Val (“hello”)

Number from textbox0 0

Str Converts a number to a String

Str (123) “123”

IsNumeric

Returns true if the items can be converted to a number

IsNumeric (“23.5”)IsNumeric (txtInput.text)IsNumeric(“hello”)

True

False

Int Returns the integer part of a number

Int (123.456) 123

Page 5: Visual Basic - Intrinsic Functions

Examples of Intrinsic FunctionsFunction Description Example Result

FormatNumber

Formats a number to a specified decimal point(default of 2)

FormatNumber (8765.4534)FormatNumber (8765.4534, 3)

8,765.458,765.453

FormatCurrency

Formats a number with a dollar sign & cents

FormatCurrency (76)FormatCurrency (245879.358)

$76.00$245,879.36

FormatPercent

Formats a number as a percent

FormatPercent (.784)FormatPercent (8.2)

78.40%820.00%

Page 6: Visual Basic - Intrinsic Functions

More Intrinsic FunctionsInputBox

Prompts the user for keyboard inputInput is returned to the program for processing

Syntax:Result = InputBox (prompt, [title])

Example:strInput = InputBox (“Enter Input”, “Input”) strName = InputBox (“What is your name”)

Page 7: Visual Basic - Intrinsic Functions

One More Intrinsic FunctionMsgBox

Displays a message in a dialog boxSyntax

MsgBox (prompt)Example:

MsgBox (“Hello World”)MsgBox (“The Result is “ & Result)

Page 8: Visual Basic - Intrinsic Functions

ExampleCreate an application that adds items to a

sales receipt one at a time using an input text box and a button. Each time the button is pressed, the new item price should be added to a list box control which acts as a receipt. The program should also contain output labels for subtotal, sales tax and total that should be updated when an item is added to the receipt. (Ensure that the prices are numeric and that the output is formatted to currency)

Page 9: Visual Basic - Intrinsic Functions

TOE ChartTASK OBJECT EVENT

Input Price Text Box, Label None

Display Receipt List Box None

Display Subtotal Label, Label None

Display Sales Tax Label, Label None

Display Total Label, Label None

Add Item to Receipt Button Click

Exit Button Click

Page 10: Visual Basic - Intrinsic Functions
Page 11: Visual Basic - Intrinsic Functions

CodeVariables

SubtotalTaxTotal

What Data Types?Where to Declare?

Page 12: Visual Basic - Intrinsic Functions

Add to Receipt ButtonRead in Data from Text BoxConvert to Number

CDblVal

Input into ListBoxFormatted as Currency

Update Subtotal, Tax, Total & Displays

Page 13: Visual Basic - Intrinsic Functions

Convert to Number

Format to Currency

Format to Currency