Professional Documents
Culture Documents
Variables, Constants and Calculations
Variables, Constants and Calculations
Variables, Constants and Calculations
Variables, Constants
and Calculations
Programming In
Visual Basic .NET
Data - Variables and Constants
Variable
– Memory locations that hold data that can be changed
during project execution
– Example: customer’s name
• Named Constant
– Memory locations that hold data that cannot be
changed during project execution
– Example: sales tax rate
• Decimal - D • Long - L
• Double - R • Short - S
• Integer - I • Single - F
Method Convert
To
Integer.Parse Integer
Decimal.Parse Decimal
.ToString String
quantityInteger = Integer.Parse(quantityTextBox.Text)
priceDecimal = Decimal.Parse(priceTextBox.Text)
wholeNumberInteger = Integer.Parse(digitString)
resultLabel.Text = resultDecimal.ToString( )
countTextBox.Text = countInteger.ToString( )
idString = idInteger.ToString( )
Operator Operation
+ Addition
– Subtraction
* Multiplication
/ Division
\ Integer Division
Mod Modulus – Remainder of
division
^ Exponentiation
3- 19 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
Order of Operations
C or c Currency
F or f Fixed-point
N or n Number
D or d Digits
P or p Percent
Try
statements that may cause error
Catch [VariableName As ExceptionType]
statements for action when an exception occurs
[Finally
statements that always execute before exit of Try block]
End Try
Try
quantityInteger = Integer.Parse(quantityTextBox.Text)
quantityLabel.Text = quantityInteger.ToString( )
Catch
messageLabel.Text = "Error in input data."
End Try
Try
quantityInteger = Integer.Parse(quantityTextBox.Text)
quantityLabel.Text = quantityInteger.ToString( )
Catch theException As FormatException
messageLabel.Text="Error in input data."
End Try
Try
' Statements that may cause errors.
Catch theException As FormatException
' Statements for nonnumeric data.
Catch theException As ArithmeticException
' Statements for calculation problem.
Catch theException As Exception
' Statements for any other exception.
End Try
• TextMessage string
– String literal or variable that displays message
• Title Bar text
– String that appears in title bar of message box
• MessageBox Buttons
– OK, OKCancel, RetryCancel, YesNo, YesNoCancel,
AbortRetryIgnore
• MessageBox Icons
– Asterisk, Error, Exclamation, Hand, Information, None,
Question, Stop, Warning
• Calculating an Average
averageDiscountedSaleDecimal = discountedPriceSumDecimal / saleCountInteger