VBScript09

advertisement
VBScript
Session 9
Dani Vainstein
1
What we learn last session?
VBScript coding conventions.
Code convention usage for
constants, variables, objects and
functions.
Dani Vainstein
2
Subjects for session 9
Conversion functions.
OS Conversions
Asc, Char
Data Types conversions
CBool, CByte, CCur, CDate, CDbl, Char, CInt, Clng,
CSng, CStr
Base Conversions
Hex, Oct
Variables subtypes verification.
IsArray, IsDate, IsEmpty, IsNull, Is Numeric, IsObject
Dani Vainstein
3
Conversion Functions
OS Conversions - Asc
Returns the ANSI character code corresponding to
the first letter in a string.
The string argument is any valid string
expression. If the string contains no characters, a
run-time error occurs.
Dani Vainstein
4
Conversions Functions - Char
Returns the character associated with the
specified ANSI character code.
The charcode argument is a number that
identifies a character.
Numbers from 0 to 31 are the same as standard,
nonprintable ASCII codes.
For example, Chr(10) returns a linefeed
Dani Vainstein
5
Conversion Functions
Data Type Conversions - CBool
Returns an expression that has been converted to a Variant
of subtype Boolean.
If expression is zero, False is returned; otherwise, True is
returned.
If expression can't be interpreted as a numeric value, a runtime error occurs.
Dani Vainstein
6
Conversion Functions
Data Type Conversions - CByte
Returns an expression that has been converted to a Variant of subtype Byte.
In general, you can document your code using the subtype conversion functions
to show that the result of some operation should be expressed as a particular
data type rather than the default data type.
For example, use CByte to force byte arithmetic in cases where currency, singleprecision, double-precision, or integer arithmetic normally would occur.
Use the CByte function to provide internationally aware conversions from any
other data type to a Byte subtype.
For example, different decimal separators are properly recognized depending on
the locale setting of your system, as are different thousand separators.
Dani Vainstein
7
Conversion Functions
Data Type Conversions - CCur
Returns an expression that has been converted to a Variant of
subtype Currency
In general, you can document your code using the subtype
conversion functions to show that the result of some operation
should be expressed as a particular data type rather than the
default data type.
For example, use CCur to force currency arithmetic in cases where
integer arithmetic normally would occur.
You should use the CCur function to provide internationally aware
conversions from any other data type to a Currency subtype.
Dani Vainstein
8
Conversion Functions
Data Type Conversions - CDate
Returns an expression that has been converted to a Variant of subtype
Date.
Use the IsDate function to determine if date can be converted to a date or
time.
CDate recognizes date literals and time literals as well as some numbers
that fall within the range of acceptable dates.
When converting a number to a date, the whole number portion is
converted to a date.
CDate recognizes date formats according to the locale setting of your
system.
The correct order of day, month, and year may not be determined if it is
provided in a format other than one of the recognized date settings.
In addition, a long date format is not recognized if it also contains the dayof-the-week string.
Dani Vainstein
9
Conversion Functions
Data Type Conversions - CDate
Returns an expression that has been converted to a Variant of subtype
Double.
In general, you can document your code using the subtype conversion
functions to show that the result of some operation should be expressed as
a particular data type rather than the default data type.
For example, use CDbl or CSng to force double-precision or singleprecision arithmetic in cases where currency or integer arithmetic normally
would occur.
Use the CDbl function to provide internationally aware conversions from
any other data type to a Double subtype.
For example, different decimal separators and thousands separators are
properly recognized depending on the locale setting of your system.
Dani Vainstein
10
Conversion Functions
Data Type Conversions - CInt
Returns an expression that has been converted to a
Variant of subtype Integer.
In general, you can document your code using the
subtype conversion functions to show that the result of
some operation should be expressed as a particular data
type rather than the default data type.
For example, use CInt or CLng to force integer arithmetic
in cases where currency, single-precision, or doubleprecision arithmetic normally would occur.
Use the CInt function to provide internationally aware
conversions from any other data type to an Integer
subtype.
If expression lies outside the acceptable range for the
Integer subtype, an error occurs.
Dani Vainstein
11
Conversion Functions
Data Type Conversions - CLng
Returns an expression that has been converted to a
Variant of subtype Long.
For example, use CInt or CLng to force integer arithmetic
in cases where currency, single-precision, or doubleprecision arithmetic normally would occur.
Use the CLng function to provide internationally aware
conversions from any other data type to a Long subtype.
For example, different decimal separators are properly
recognized depending on the locale setting of your
system, as are different thousand separators.
If expression lies outside the acceptable range for the
Long subtype, an error occurs.
Dani Vainstein
12
Conversion Functions
Data Type Conversions - CSng
Returns an expression that has been converted to a
Variant of subtype Single.
For example, use CDbl or CSng to force double-precision
or single-precision arithmetic in cases where currency or
integer arithmetic normally would occur.
Use the CSng function to provide internationally aware
conversions from any other data type to a Single
subtype.
For example, different decimal separators are properly
recognized depending on the locale setting of your
system, as are different thousand separators.
If expression lies outside the acceptable range for the
Long subtype, an error occurs.
Dani Vainstein
13
Conversion Functions
Data Type Conversions - CStr
Returns an expression that has been
converted to a Variant of subtype String.
Use the CStr function to provide
internationally aware conversions from any
other data type to a String subtype.
You should use the CStr function instead of
Str to provide internationally aware
conversions from any other data type to a
String subtype.
Dani Vainstein
14
Conversion Functions
Data Type Conversions - CStr
The data in expression determines what is returned
according to the following
If expression is Boolean then CStr returns a string
containing True or False.
If expression is Date then CStr returns a string
containing a date in the short-date format of your system.
If expression is Null then CStr returns a run-time error.
If expression is Empty then CStr returns a zero-length
String ("").
If expression is Error then CStr returns a string
containing the word Error followed by the error number.
If expression is Other Numeric then CStr returns a string
containing the number.
Dani Vainstein
15
Conversion Functions
Base Conversions - Hex
Returns a string representing the hexadecimal value of a number.
If number is not already a whole number, it is rounded to the
nearest whole number before being evaluated.
If number is Null then Hex returns Null.
If number is Empty then Hex returns Zero(0).
If number is any other number then Hex returns Up to 8 hexadecimal
characters.
You can represent hexadecimal numbers directly by preceding
numbers in the proper range with &H.
For example, &H10 represents decimal 16 in hexadecimal notation.
Dani Vainstein
16
Conversion Functions
Base Conversions - Oct
Returns a string representing the octal value of a number.
If number is not already a whole number, it is rounded to the
nearest whole number before being evaluated.
If number is Null then Oct returns Null.
If number is Empty then Oct returns Zero(0).
If number is any other number then Oct returns Up to 11 octal
characters.
You can represent octal numbers directly by preceding numbers in
the proper range with &O.
For example, &O10 is the octal notation for decimal 8.
Dani Vainstein
17
Variables Subtypes
Verification
Because wrong usage of data can cause run-time errors,
VB provides a set of critical data varification functions.
IsArray - IsArray(varname)
Returns a Boolean value indicating whether a variable is an
array.
The varname argument can be any variable.
IsArray returns True if the variable is an array; otherwise,
it returns False.
IsArray is especially useful with variants containing arrays.
Dani Vainstein
18
Variables Subtypes Verification
IsDate Function
Returns a Boolean value indicating whether an expression
can be converted to a date.
The expression argument can be any date expression or
string expression recognizable as a date or time.
IsDate returns True if the expression is a date or can be
converted to a valid date; otherwise, it returns False.
In Microsoft Windows, the range of valid dates is January 1,
100 A.D. through December 31, 9999 A.D.
Ranges vary among operating systems.
Dani Vainstein
19
Variables Subtypes Verification
IsEmpty Function
Returns a Boolean value indicating whether a variable
has been initialized.
However, because IsEmpty is used to determine if
individual variables are initialized, the expression
argument is most often a single variable name.
IsEmpty returns True if the variable is uninitialized, or
is explicitly set to Empty; otherwise, it returns False.
False is always returned if expression contains more
than one variable.
Dani Vainstein
20
Variables Subtypes Verification
IsNull Function
Returns a Boolean value that indicates whether an expression contains no valid data
(Null).
IsNull returns True if expression is Null, that is, it contains no valid data; otherwise,
IsNull returns False.
If expression consists of more than one variable, Null in any constituent variable
causes True to be returned for the entire expression.
The Null value indicates that the variable contains no valid data.
Null is not the same as Empty, which indicates that a variable has not yet been
initialized.
It is also not the same as a zero-length string (""), which is sometimes referred to as
a null string.
Caution Use the IsNull function to determine whether an expression contains a
Null value.
Expressions that you might expect to evaluate to True under some circumstances,
such as If Var = Null and If Var <> Null, are always False.
This is because any expression containing a Null is itself Null, and therefore, False.
Dani Vainstein
21
Variables Subtypes Verification
IsNumeric Function
Returns a Boolean value indicating whether an
expression can be evaluated as a number.
The expression argument can be any expression.
IsNumeric returns True if the entire expression is
recognized as a number; otherwise, it returns False.
IsNumeric returns False if expression is a date
expression.
Dani Vainstein
22
Variables Subtypes Verification
IsObject Function
Returns a Boolean value indicating
whether an expression references a
valid Automation object.
IsObject returns True if expression is
a variable of Object subtype or a userdefined object; otherwise, it returns
False.
Dani Vainstein
23
Lab 9.1
In a company we have 3 divisions : “HR”,”R&D” and
“QA”
Each employee in each division, will asked by his
name and Salary (Input box) title of input boxes the
division name.
The program will loop in a fixed for…next statement
(for divisions).
Employees will be entered in another loop,
# of employees in each division = unknown.
The default salary is 2800 NIS.
Verify if the entered value for salary is a number,
otherwise display a warning message + warning
icon, and downgrade to zero.
Use only two arrays.
Dani Vainstein
24
Lab 9.1
Write a Sub procedure that outputs to the reporter the
percenatge of taxes paid by each employee according to the
follow tax table
Until 2800 NIS – 0%
Between 2801 And 3500 NIS – 10%
Between 3501 And 4500 NIS – 20%
Between 4501 And 6500 NIS – 30%
Between 6501 And 9500 NIS – 40%
Between 9501 And 12000 NIS – 50%
Between 12001 And 15000 NIS – 55%
15001 and above – 60%
The calculation Itself will be calculated in the function
GetTax(curSal).
Tips :
Use the Erase statement to reuse the arrays.
Convert to subtype currency.
Dani Vainstein
Check if input size is > 0 before enter sub.
25
Make sure to visit us
Tutorials
Articles
Proikects
And much more
www.AdvancedQTP.com
26
Download