Upload
zwi
View
31
Download
0
Embed Size (px)
DESCRIPTION
There is a String type in VB. String A String variable can hold strings of arbitrary length. Dim strFirstName As String strFirstName = “Homer” strFirstName = “”. All punctuation marks, and digits are included. “124!@#_-=+)),”. Dim strFirstName As String Dim strLastName As String - PowerPoint PPT Presentation
Citation preview
There is a String type in VBThere is a String type in VB
Data Type Prefix
String str
Dim strFirstName As String
strFirstName = “Homer”
strFirstName = “”
String
A String variable can hold strings of arbitrary length
All punctuation marks, and digits are included. “124!@#_-=+)),”
Dim strFirstName As String Dim strLastName As StringDim strFullName As String
strFirstName = “Homer”strLastName = “Simpson”
strFullName = strFirstName & “ ” & strLastName
bntRedDemo.Text = strFullName
Using Subroutines/Methods/FunctionsUsing Subroutines/Methods/Functions
The black box view of a function
2 3 6 2
5 8
Here is a different function..
“Cat” 2 “Dog” 1
“Ca” “D”
What do we need, to specify a function?
• A name• A parameter list. A list of the “things” the functions expects, in this case, a string, followed by an integer.• A return type. In this case a string
“Cat” 2
“Ca”
Truncate
Visual Basic has built in functions (and later we can write our own)
Each function has:
• A name• A parameter list• A return type
In this case, the name is UCase, the parameter list is a single string and the return type is a single string.
“Cat”
“CAT”
UCase
strFirstName = "Homer"
bntRedDemo.Text = UCase(strFullName)
Function call Return Value
UCase(“Input String”) “INPUT STRING”
UCase(“all lowercase”) “ALL LOWERCASE”
UCase(“ALL UPPERCASE”) “ALL UPPERCASE”
UCase(“UpPeP AnD lOwErCaSE”) “UPPER AND LOWERCASE”
strFirstName = "Homer"
bntRedDemo.Text = UCase(strFullName)
bntRedDemo.Text = UCase(“marge”)
strFName = “John”
strLName = “Doe”
bntRedDemo.Text = UCase(strFName & “ ” & strLName )
Syntax: String = UCase(String)Syntax: String = UCase(String)
strA = ….
If (strA = “Male” Or strA = “male” Or strA = “MALE” ) Then bntRedDemo.Text = “You choose male.”End If
strA = ….
If (Ucase(strA) = “MALE”) Then bntRedDemo.Text = “You choose male.”End If
A classic use of the UCase function is robust data entry…
strFName = “John”
strLName = “Doe”
bntRedDemo.Text = LCase(strFName & “ ” & strLName )
Function call Return Value
LCase(“Input String”) “input string”
LCase(“all lowercase”) “all lowercase”
LCase(“ALL UPPERCASE”) “all uppercase”
LCase(“UpPeP AnD lOwErCaSE”) “upper and lowercase”
There a complimentary function to UCase called LCase. It works exactly like you would expect …
Syntax: String = LCase(String)Syntax: String = LCase(String)
strAge = “5”
strAgeDiff = 100 – Val(strAge)
There a function call Val, which converts strings to numbers.
Syntax: Numeric Value = Val(String)Syntax: Numeric Value = Val(String)
Function call Return Value
Val(“199.11”) 199.11
Val(“ 199.11 “) 199.11
Val(“ 1 99.1 1”) 199.11
Val(“ 199 “) 199
Val(“$199.11”) 0
Val(“1,199.11”) 1
Val(“ “) 0
Val(“123abc”) 123
Val(“abc123”) 0
Function Name: Val
Function Description: Returns a numeric representation of the String value passed to it.
Val will convert a String to a numeric until it reaches a character that is not a numeric value, a decimal point, or a white-space character.
Once an unrecognizable character is read, conversion stops at that point.
Syntax: Numeric Value = Val(String)
Examples:
bntRedDemo.Text = Str(25)
bntRedDemo.Text = (25).ToString
There a complementary function call Str, which converts numbers to strings.
Syntax: String = Str(Numeric Value)Syntax: String = Str(Numeric Value)
Function Name: Str
Function Description: Returns a String representation of the numeric value passed to it.
By default it will place a single space in front of the first numeric character.
These are logically equivalent (almost!)
These are logically equivalent (almost!)
There is a function called Trim, which removes trailing and leading space from text.
Function call Return Value
Trim(“ InputString”) “InputString”
Trim(“InputString ”) “InputString”
Trim(“ InputString ”) “InputString”
Trim(“ Input String ”) “Input String”
strA = “ Male”
If (strA = “Male”) Then bntRedDemo.Text = “You choose male.”End If
If (Trim(strA) = “Male”) Then
This is False!This is False!
Syntax: String = Trim(String)Syntax: String = Trim(String)
This is TrueThis is True
We can nest functions...
strA = “ Male”
If ( Trim(UCase(strA) ) = “MALE”) Then bntRedDemo.Text = “You choose male.”End If
Trim(Ucase(“ Male”))
Trim(Ucase(strA))
Trim(“ MALE”)
“MALE”
We can nest functions...
bntRedDemo.Text = Str(5) & Str(5)
bntRedDemo.Text = Str(5) & Trim(Str(5))
There is a function called Len, which returns the number of characters contained in a String
strA = …
If ( Len(strA) > 10 ) Then bntRedDemo.Text = “You have a long name!”
Else If ( Len(strA) <= 1 ) ‘The DMV requires at least 2 letters bntRedDemo.Text = “This is not a legal name!”
End If
Syntax: Integer = Len(String)Syntax: Integer = Len(String)
There is a function called Mid, which returns a subsection of a string…
Function call Return Value
Mid(“This is the String”, 6, 2) “is”
Mid(“This is the String”, 9, 3) “the”
Dim shtP As Short = 4
Mid(“This is the String”, 13, shtP ) “Stri”
Mid(“This is the String”, 8) “ the String”
Syntax: String = Mid(String, integer_type, integer_type )Syntax: String = Mid(String, integer_type, integer_type )
Optional!
Returns a specific number of characters of a String allowing the developer to indicate where to start and how many characters to return.
The first parameter is the source String.
The second is an Integer indicating the starting position to copy from.
The third parameter is optional and indicates the number of characters to copy.
If the third parameter is left out, all characters from the starting position are returned.
By using functions we can do lots of cool things… Suppose I want to get just the First letter in someone's name…
Dim strFName, strLName , strMName, strFullName As String
strFName = “Homer”strLName = “Simpson”strMName = “Jay”
strFullName = strFName & " " & Mid(strMName, 1, 1) & " " & strLName
bntRedDemo.Text = strFullName
By using functions we can do lots of cool things… Suppose I want to get just the Last letter in a word…
Dim strW, strLastLetter As String
strW = “Books”
strLastLetter = Mid(strW,len(strW),1)
If ( (UCase(strLastLetter)) = “S”) Then bntRedDemo.Text = “The word is probably plural”End If