











































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












































































The Visual Basic .NET Coach
functions^ or^ subroutines
Programs are made more readable by breaking large amounts of code into smaller, moreconcise parts.By breaking code into functions and subroutines, code can be written once and reused often.This reduces the size of the application and debugging time.Each time you repeat a calculation, you waste space and increase the likelihood of atypographical error and therefore cause your application to execute improperly.Functions and subroutines operate similarly but have one key difference.A function is used when a value is returned to the calling routine, while a subroutine is usedwhen a desired task is needed, but no value is returned.
The Visual Basic .NET Coach
The Visual Basic .NET Coach
The Visual Basic .NET Coach
Returns the^ String^
that is passed in all uppercase letters. Common Uses:^ UCase
can be used when the desired output is required to be in all uppercase letters. It is also commonly used when you wish to validate data entered by a user against agiven string. Syntax:^ String = UCase(String) Examples:
UCase(“UpPeP AnD lOwErCaSE”)
UCase(“ALL UPPERCASE”)
UCase(“all lowercase”)
UCase(“Input String”)
Return Value Function^ call Previous Way of Coding Validation: If (txtVote.Text =
“Bush”^ Or txtVote.Text = “BUSH”
Or^ _ txtVote.Text = “bush” Then ... Better Way of Coding Validation: If (UCase(txtVote.Text) = “BUSH”) Then ...
The Visual Basic .NET Coach
Returns a^ String^ with the same content, except the leading and trailing spaces are removed. Common Uses:^ Often when data is gathered, additional spaces may exist before the firstnoncharacter or after the last nonblank character.It is good practice to remove these so that data may be presented cleanly. Syntax:^ String = Trim(String) Examples:
“Input String” Trim(“^ Input String
“InputString” ”) Trim(“^ InputString
“InputString” ”) Trim(“InputString
“InputString” ”) Trim(“^ InputString”)
Return Value Function^ call
The Visual Basic .NET Coach
(continued) The following code will initialize two
Strings. One will contain a^ String
that has the leading and trailing spaces removed by the
Trim
function.It is displayed between two vertical bars so that it will be obvious that the spaces have beenremoved.The second^ String^ will be created in a similar manner; however, the spaces will not beremoved.^ Dim^ strTest^ As StringDim^ strWithBlanks As StringDim^ strBorder As StringDim^ strTrimmedOutput As StringDim^ strUnTrimmedOutput
As String strTest =^ " Hello " 'Two spaces before and afterstrBorder =^ "|" strTrimmedOutput =
strBorder & Trim(strTest) & strBorder strUnTrimmedOutput
= strBorder &^ strTest & strBorder MsgBox(strTrimmedOutput)MsgBox(strUnTrimmedOutput)
|Hello|| Hello^ |
The Visual Basic .NET Coach
Returns the number of characters contained in a
String
Common Uses:^ Len is used to determine the size of a
String. Syntax:^ Integer = Len(String) Examples:
Len(“”)
Len(“Hello, my name is Inigo Montoya. Youkilled my father.
Prepare to die.”)
Len(“Iocaine Powder”)
Len(“Inconceivable”)
Return Value Function^ call
The Visual Basic .NET Coach
Returns the first N characters of a
String^ where N is an
Integer
parameter indicating the number of characters to return.If N is greater than the number of characters in the
String, then the^ String
is returned.
No extra spaces are added. Common Uses:^ Often you are only concerned with the first few characters of a
String.^ Left
is a great way to look at only the beginning of a
String. Syntax:^ String = Microsoft.VisualBasic.Left(String,
Integer)
Examples:
“Beginning of String”
Microsoft.VisualBasic.Left(“Beginning of String”, 20)
Microsoft.VisualBasic.Left(“Beginning of String”, 0)
“Be”
Microsoft.VisualBasic.Left(“Beginning of String”, 2)
“Begin”
Microsoft.VisualBasic.Left(“Beginning of String”, 5)
Return Value
Function^ call
The Visual Basic .NET Coach
Returns the last N characters of a
String^ where N is an
Integer
parameter indicating the number of characters to return.If N is greater than the number of characters in the
String, then the^ String
is returned.
No extra spaces are added. Common Uses:^ Often you are only concerned with the last few characters of a
String.
Right^ is a great way to look at only the end of a
String. Syntax:^ String = Microsoft.VisualBasic.Right(String, Integer) Examples:
“Ending of String”
Microsoft.VisualBasic.Right(“Ending of String”, 20)
Microsoft.VisualBasic.Right(“Ending of String”, 0)
“ng”
Microsoft.VisualBasic.Right(“Ending of String”, 2)
“tring”
Microsoft.VisualBasic.Right(“Ending of String”, 5)
Return Value
Function^ call
The Visual Basic .NET Coach
(continued) The following code shows how you might use Right to determine a person’s suffix, as in, Jr.,Sr., or Ph.D.^ Dim^ strPerson^
As^ StringDim strPerson2 As^ StringDim strPerson3 As^ StringstrPerson1 = "Nira^ Herrmann, Ph.D."strPerson2 = "John Cunningham, Sr."strPerson3 = "Bob Bruno, Jr.“'Process Person1If ("Ph.D." = Microsoft.VisualBasic.Right(strPerson1, 5)) ThenMsgBox "Person 1 has a doctorate degree."ElseIf ("Sr." = Microsoft.VisualBasic.Right(strPerson1, 3))
Then
MsgBox "Person 1 has a kid with the same name."ElseIf ("Jr." = Microsoft.VisualBasic.Right(strPerson1, 3))
Then
MsgBox "Person 1 has a father with the same name."EndIf'Person2 and Person3 code could follow
The Visual Basic .NET Coach
Returns the position of the first occurrence of a substring that is searched for in the^ String
passed. Common Uses:^ InStr
can be used to tell us if a
String^ has a certain substring contained within it. It operates much like searching a document for a word. Syntax:^ Long = InStr(String to be
Searched, Search String) Examples:
InStr(“ab ab ab”, “c”)
InStr(“ab ab ab”, “a”)
InStr(“ab ab ab”, “ab”)
InStr(“This^ is a very”, “is”)
Return Value Function^ call
The Visual Basic .NET Coach
is the output?”) Answer: “WHAT IS THE OUTPUT?”
The Visual Basic .NET Coach
The Visual Basic .NET Coach