Title: CSCI13003A
1Lecture-12 (7.1-7.9)
- CSCI130-03A
- Instructor Dr. Imad Rahal
2Coding Standards
- Every object used MUST be renamed including the
form (s) using the following rules - Form ?frmFormName
- frmTemperature
- Textbox ? txtTextBoxName
- txtInput
- Label ? lblLabelName
- lblDescribeInput
- Button ? cmdButtonName
- cmdButtonName
- PictureBox ? picPictureBoxName
- picOutput
3Summary
- Must use Option Explicit
- Declare all variables
- No need for Val function
- 2s Complement
- Integer or Long
- Floating-point
- Single or Double
- Boolean
- ASCII Data
- String
4Summary
- Start Subs by declaring variables
- What is a variable?
- Letters and numbers (some signs)
- Initialize
- 0 for numbers
- Empty for String
- False for Boolean
- Variable scope
- In Sub ? local
- Outside ? Global
5Numeric Functions
- Program calculate the roots of a quadratic
equation given a, b and c
- Sqr(X)
- Square root
- Sqr(2)
- Int(X)
- Sqr(2.1234)
- Round(X)
- To the nearest integer
- Sqr(2.45)
- Abs(X)
- Abs(-34)
- Log(X)
- Log(4)
- Enter a real number
- Private Sub Quadraticbutton_Click()
- Dim A As Integer, B As Integer, C As Integer
- Dim R1 As Double, R2 As Double
- A Abox.Text
- B Bbox.Text
- C Cbox.Text
- R1 (-BSqr(B2-4AC))/(2A)
- R2 (-B-Sqr(B2-4AC))/(2A)
- Results.Print Roots areR1R2
- End Sub
6String Functions
- Used for string variables or appear in print
functions - Substring functions
- Left (Str, Count)
- Returns substring from the left side of a string
- Requires a count (goes left)
- Right (Str, Count)
- Returns substring from the right side of a string
- Requires a count (goes right)
- Mid (Str, StartPoint, Count)
- Returns substring from the middle of a string
- Requires a starting point and a count (to the
starting points right)
7String Functions
- InStr(S1,S2)
- Searches for string S2 in S1
- Returns the first position it is found in
- A Instr(St. Johns, .) ?
- Len(S)
- Returns the number of characters in S
- Len(A) ?
- Trim removes spaces from both ends of a string
- Concatenation is done with
- A College of Saint Benedict
- B Saint Johns University
- C AB ?
8String Functions
- College of Saint BenedictSaint Johns University
- No space
- How can we add one?
- To any of the concatenated strings
- On its own
- College of Saint BenedictSaint Johns University?
9String Functions
- Program to input first, middle Initial and last
name - Returns username as first letter of first name,
middle initial and then last name (no spaces
between them) in lower case
Private Sub Usernamebutton_Click() Dim n As
String Dim sp As Integer Dim first As String,
middle As String, last as String Dim username As
String Results.Cls n Namebox.Text sp
InStr(n, ) first Left(n, sp-1) middle
Mid(n, sp1,1) last Right(n, Len(n)-(sp2))
skip middle initial and space username
Left(first,1) middle Last Results.Print
Your username is LCase(username) End Sub
10Input Output
- Spacing
- (semicolon)
- Combines numbers and strings on Prints
- 1 space after a number for readability
- 1 space for the sign (if any)
- Non for strings
- Try where Answer is a number and then String
- Results.Print The answer isAnswer
- , (comma)
- Every picture box is divided into 14-space print
zones - Jumps to the next zone (does not jump 14 spaces
necessarily!) - From its current location (its current 14-space
zone) - Results.Print 5,30,2
11Input Output
- Tab function Tab(X)
- Followed by a (automatically inserted by VB)
- leaves X spaces from start
- Pushes any overlap to new lines
- Results.Print Tab(2) Hi!
- Results.Print Tab(2) Hi! Tab(2) Bi!
- What should the second 2 be to print on the same
line
12Input Output
- FormatCurrency(x,y) --- y is 2 by default
- x is the number to be formatted
- y is the number of digits allowed after the
decimal point (rounding) - Extra 0s are appended
- Adds a sign and commas if needed
- FormatCurrency(1234.234,2) 1,234.23
- FormatCurrency(1234.2,2) ?
- FormatPercent(x,y) --- y is 2 by default
- x is the decimal number to be formatted
- y is the number of digits allowed after the
decimal point (rounding) - Extra 0s are appended
- FormatPercent(0.5235, 1) 52.4
- FormatNumber(x,y) --- y is 2 by default
- Rounds x to the nearest number of digits after
the decimal point specified by y - FormatPercent(0.5235) 0.52
- FormatPercent(0.5235,3) 0.54
13Input Output
- Message Boxes
- For output specially in situations of errors
- Other than textboxes and pictureboxes
- MsgBox prompt, ,title
- MsgBox Sorry but you must enter a positive
number , , Error
14Input Output
- Input Boxes
- Other than textboxes
- Good for small amount of input (form full of
textboxes is not nice) - X Inputbox( prompt, title)
- The input is assigned to variable X when the use
hits OK - A Inputbox(Enter an exam score,Exam 1)