Download presentation
Presentation is loading. Please wait.
Published byTheodore McBride Modified over 9 years ago
1
String and General Procedures
2
Answer to the last question of Exam 1 Start Get a number Divide the Number By 2 Is the quotient Equal to 1? Print The Remain to the left of previous remains No Print 1 To the left of Previous remains End Yes Output number If number Equal to 1 or 0 Yes No
3
Statistics of Exam 1 Max: 98 Max: 98 Average: 75.18 Average: 75.18 Median: 75 Median: 75
5
Strings Strings are variables that hold letters, words, sentences, or even numbers Strings are variables that hold letters, words, sentences, or even numbers String holds a bunch of ASCII codes. String holds a bunch of ASCII codes. You can not do mathematical operations on string. You can not do mathematical operations on string. So you need to use Val(string) function to convert strings to numbers. So you need to use Val(string) function to convert strings to numbers.
6
String Variables Declaring Strings Declaring Strings Dim Phrase1 As String Assigning value to a string variable Assigning value to a string variable –Phrase1 = “Hello” Phrase1 H e l l o 72 101 108 111 ASCII code
7
& Adding Two Strings & Phrase1 = TextBox1.Text Phrase2 = TextBox2.Text Sentence = Phrase1 & Phrase2 Adding two strings is called “CONCATENATING”
8
Adding text to a String Variable Dim TheAnswer as String Dim TheAnswer as String TheAnswer = “a plague upon society!” TheAnswer = “a plague upon society!” Sentence= “Boy Bands are ” & “TheAnswer” Sentence= “Boy Bands are ” & “TheAnswer” Sentence= “Boy Bands are ” & TheAnswer Sentence= “Boy Bands are ” & TheAnswer
9
Spacing Spacing: Does not insert spaces for you Spacing: Does not insert spaces for you –“This is” & “not right” This isnot right –“This is ” & “right” This is right Space itself has an ASCII code, which is 32 (10 base), or 00100000 (binary) Space itself has an ASCII code, which is 32 (10 base), or 00100000 (binary)
10
String Relationships String can be compared. String can be compared. StringA = StringB : comparing if two string is identical. StringA = StringB : comparing if two string is identical. StringA <> StringB : different from StringA <> StringB : different from –“Hello” <> “hello” is true. StringA < StringB : if the string A will appear earlier in a dictionary than B. StringA < StringB : if the string A will appear earlier in a dictionary than B. >, = >, =
11
String Functions Functions are built in processes that do something to a variable or piece of information Functions are built in processes that do something to a variable or piece of information Functions have ARGUMENTS, or values that you put into them Functions have ARGUMENTS, or values that you put into them ARGUMENTS go inside parentheses ARGUMENTS go inside parentheses Left(“fanatic”, 3) Left(“fanatic”, 3) Left(fanatic, 3) Left(fanatic, 3)
12
String Functions Len(string) – finds length Len(string) – finds length Left(string, n) takes left most n characters Left(string, n) takes left most n characters Right(string, n) does same from right Right(string, n) does same from right Ucase(string) makes string UPPERCASE Ucase(string) makes string UPPERCASE Trim(string) – takes spaces off ends Trim(string) – takes spaces off ends Mid(string, start, length) – takes a string from middle (great for dividing strings up into pieces) Mid(string, start, length) – takes a string from middle (great for dividing strings up into pieces) InStr(string, substring) searches for substring in string InStr(string, substring) searches for substring in string
13
Mid String DNA=“ACGTGACACTGAC” DNA=“ACGTGACACTGAC” Base1=Mid(DNA, 1, 1) “A” Base1=Mid(DNA, 1, 1) “A” Base2=Mid(DNA, 2, 4) “CGTG” Base2=Mid(DNA, 2, 4) “CGTG”... Base6=Mid(DNA, 6, 2) “AC” Base6=Mid(DNA, 6, 2) “AC”
14
InStr() – In String DNA=“ACGTGACACTGAC” DNA=“ACGTGACACTGAC” Location=InStr(DNA, “CACTG”) Location=InStr(DNA, “CACTG”) Returns location of first character of substring
15
Working with Text Strings Comparison = means “identical to” Comparison = means “identical to” “yes” is not equal to “Yes” “yes” is not equal to “Yes” “yes “ is not equal to “yes” “yes “ is not equal to “yes” Prepare strings for comparison Prepare strings for comparison –answer = Ucase(answer) – makes it all caps –answer = Trim(answer) – takes off spaces Then, compare to all caps! Then, compare to all caps! –if answer = “YES” then
16
Numeric Functions answer = Sqr(varNum) answer = Int(varNum) – truncates (cuts off decimal stuff) and makes integer Int(3.7) 3 Int(-2.34) -2 answer= Round(varNum) – rounds number Round(3.7) 4
17
Procedures String and numeric functions belong to a VB category called “procedure” String and numeric functions belong to a VB category called “procedure” Procedures are pre-defined code fragments. Procedures are pre-defined code fragments. There are three types of procedures. There are three types of procedures. –Event procedure (event handler) –General procedure –Function procedure
18
Event Procedure Event procedure is also called event handler. Event procedure is also called event handler. It is associated with an event of the object. It is associated with an event of the object. The name of a event procedure is specially structured. The name of a event procedure is specially structured. Private Sub ObjectName_EventName() … End Sub Underscore
19
General Procedures Defined by key word Sub Defined by key word Sub Private Sub ProcedureName() Block of code End Sub Not linked with any event Not linked with any event Can be called by other part of code. Can be called by other part of code. –Call ProcedureName()
20
Why using general procedures? Making the code clear and well structured. Making the code clear and well structured. –Rule of coding #1: Don’t write a procedure that is more than 50 lines long. Distinguishing different function blocks. Distinguishing different function blocks. Reusing the repetitive code Reusing the repetitive code
21
Passing arguments to a procedure Analogy Analogy –Inputs for a computer is as : –Arguments for a procedure. Defination Defination Private Sub ProcedureName(arg1 as type, arg2 as type) Block of code End Sub In the code section that calls it In the code section that calls it Call ProcedureName(arg1, arg2)
22
A computer without a output device? A sub procedure is like a computer that only has input device. A sub procedure is like a computer that only has input device. Although you can display result on the screen, other output devices are definitely helpful. Although you can display result on the screen, other output devices are definitely helpful. Therefore we need another kind of procedure --- function procedure. Therefore we need another kind of procedure --- function procedure.
23
Functions procedure Function is a sub procedure with a returned value. Function is a sub procedure with a returned value. Function procedure is used in the same way as a built-in numeric or string function. Function procedure is used in the same way as a built-in numeric or string function. Variable = FunctionName(arguments…) Define a function Define a function Private Function FunctionName(arg1 As type1, arg2 As type2,…) As ReturnType Block of code FunctionName=expression End Function
24
Homework Read book page 97-110 Read book page 97-110 Lots of useful information in the comments section of the book. Lots of useful information in the comments section of the book. Today’s office hours are rescheduled to Wednesday 1:30-3:30 only for this week. Today’s office hours are rescheduled to Wednesday 1:30-3:30 only for this week.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.