200 likes | 488 Views
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
E N D
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 Dim strFullName As String strFirstName = “Homer” strLastName = “Simpson” strFullName = strFirstName & “ ” & strLastName bntRedDemo.Text = strFullName
Using Subroutines/Methods/Functions The black box view of a function 2 3 2 6 5 8
Here is a different function.. “Cat” 2 1 “Dog” “Ca” “D”
“Cat” 2 • 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 Truncate “Ca”
“Cat” • 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. UCase “CAT” strFirstName = "Homer" bntRedDemo.Text = UCase(strFullName)
strFirstName = "Homer" bntRedDemo.Text = UCase(strFullName) bntRedDemo.Text = UCase(“marge”) strFName = “John” strLName = “Doe” bntRedDemo.Text = UCase(strFName & “ ” & strLName ) Syntax:String = UCase(String)
A classic use of the UCase function is robust data entry… 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
There a complimentary function to UCase called LCase. It works exactly like you would expect … Syntax:String = LCase(String) strFName = “John” strLName = “Doe” bntRedDemo.Text = LCase(strFName & “ ” & strLName )
There a function call Val, which converts strings to numbers. Syntax:Numeric Value = Val(String) strAge = “5” strAgeDiff = 100 – Val(strAge) 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:
There a complementary function call Str, which converts numbers to strings. Syntax:String = Str(Numeric Value) bntRedDemo.Text = Str(25) bntRedDemo.Text = (25).ToString These are logically equivalent (almost!) 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.
There is a function called Trim, which removes trailing and leading space from text. strA = “ Male” If(strA = “Male”) Then bntRedDemo.Text = “You choose male.” End If This is False! This is True If(Trim(strA) = “Male”) Then Syntax:String = Trim(String)
We can nest functions... strA = “ Male” If( Trim(UCase(strA) ) = “MALE”) Then bntRedDemo.Text = “You choose male.” End If Trim(Ucase(strA)) Trim(Ucase(“ Male”)) 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)
There is a function called Mid, which returns a subsection of a string… 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. Syntax:String = Mid(String, integer_type, integer_type ) Optional!
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… DimstrW, strLastLetterAs String strW = “Books” strLastLetter = Mid(strW,len(strW),1) If( (UCase(strLastLetter)) = “S”) Then bntRedDemo.Text = “The word is probably plural” End If