1 / 18

There is a String type in VB

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

zwi
Télécharger la présentation

There is a String type in VB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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!@#_-=+)),”

  2. Dim strFirstName As String Dim strLastName As String Dim strFullName As String strFirstName = “Homer” strLastName = “Simpson” strFullName = strFirstName & “ ” & strLastName bntRedDemo.Text = strFullName

  3. Using Subroutines/Methods/Functions The black box view of a function 2 3 2 6 5 8

  4. Here is a different function.. “Cat” 2 1 “Dog” “Ca” “D”

  5. “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”

  6. “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)

  7. strFirstName = "Homer" bntRedDemo.Text = UCase(strFullName) bntRedDemo.Text = UCase(“marge”) strFName = “John” strLName = “Doe” bntRedDemo.Text = UCase(strFName & “ ” & strLName ) Syntax:String = UCase(String)

  8. 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

  9. 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 )

  10. 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:

  11. 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.

  12. 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)

  13. 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”

  14. We can nest functions... bntRedDemo.Text = Str(5) & Str(5) bntRedDemo.Text = Str(5) & Trim(Str(5))

  15. 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)

  16. 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!

  17. 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

  18. 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

More Related