1.1.1.Compile Visual Basic source code in command line
|
|
vbc /r:System.DLL /r:System.Windows.Forms.DLL /r:System.Drawing.DLL WinEvents.vb
|
1.1.2.How to compile and run the code in this tutorial
Go to: Start->Programs->Visual Studio.NET->Visual Studio.NET Tools-> Visual Studio.NET Command Prompt, and type:
c:\examples>vbc example1.vb.
1.1.3.Simple Visual Basic program.
|
|
Module Tester
Sub Main() Console.WriteLine("Welcome to Visual Basic!") End Sub ' Main
End Module
|
1.1.4.Writing line of text with multiple statements.
|
|
Module Tester
Sub Main() Console.Write("Welcome to ") Console.WriteLine("Visual Basic!") End Sub ' Main
End Module
|
1.2.1.Every console app starts with Main
|
|
Module HelloWorld Sub Main( ) System.Console.WriteLine("Hello world!") End Sub End Module
|
1.3.1.Read a single character
|
|
Module Module1
Sub Main()
Dim strChar As String Console.Write("Enter a character: ") strChar = Console.ReadKey.KeyChar 'strChar = Console.ReadKey(True).KeyChar Console.WriteLine() Console.WriteLine("You just entered {0}.", strChar)
End Sub
End Module
|
1.3.2.Read a complete line of text
|
|
Module Module1
Sub Main()
Dim strLine As String Console.Write("Enter a line of text: ") strLine = Console.ReadLine Console.WriteLine() Console.WriteLine("You just entered: {0}", strLine) End Sub
End Module
|
Enter a line of text: a line
1.3.3.Read a string from console
|
|
Module Module1 Sub Main() Dim strMessage As String Try strMessage = Console.ReadLine() Console.WriteLine("HELLO " + strMessage) Catch ex As Exception End Try End Sub End Module
|
1.3.4.Read keyboard input
|
|
Class Tester Shared Sub Main() Dim userInput As String
Console.WriteLine("Enter a source temperature.") userInput = Console.ReadLine() Console.WriteLine(userInput) End Sub
End Class
|
Enter a source temperature.
1.3.5.Use Do Loop to read user input
|
|
Module Module1
Sub Main() Dim strInput As String
Do Console.WriteLine("Please enter 'q' to quit...") strInput = Console.ReadLine() Console.WriteLine("You typed " & strInput) Loop While (strInput <> "q") Console.WriteLine("Quitting now.") End Sub
End Module
|
Please enter 'q' to quit...
1.3.6.Convert what you type to value type
|
|
Module Module1
Sub Main() Dim intInput As Integer Console.WriteLine("Enter a temperature...") intInput = Val(Console.ReadLine()) If intInput > 75 Then Console.WriteLine("Too hot!") ElseIf intInput < 55 Then Console.WriteLine("Too cold!") Else Console.WriteLine("Just right!") End If End Sub
End Module
|
1.3.7.Use While to read user input
|
|
Module Module1
Sub Main() Console.WriteLine("Please enter 'q' to quit...") Dim strInput As String = Console.ReadLine()
While (strInput <> "q") Console.WriteLine("You typed " & strInput) Console.WriteLine("Please enter 'q' to quit...") strInput = Console.ReadLine() End While Console.WriteLine("Quitting now.")
End Sub
End Module
|
Please enter 'q' to quit...
1.3.8.Match a pattern from user input
|
|
Public Class PatternMatcher Shared Sub Main() Dim sInput As String Dim sPattern As String Dim sMatch As String
System.Console.Write("Please Enter A Pattern:") sInput = System.Console.ReadLine() sPattern = sInput
System.Console.Write("Please Enter A String To Compare Against:") sInput = System.Console.ReadLine() sMatch = sInput
If sMatch Like sPattern Then System.Console.WriteLine(sMatch & " Matched with " & sPattern) Else System.Console.WriteLine(sMatch & " did not Match with " & sPattern) End If End Sub End Class
|
Please Enter A String To Compare Against:123
1.4.1.Output string to the Console
|
|
Module Module1
Sub Main( ) Console.WriteLine("Hello from Module") End Sub
End Module
|
|
|
Public Class Tester Public Shared Sub Main
Console.WriteLine(String.Format( _ "There are about {0} days in {1} years.", _ 365.25 * 3, 3, 17)) End Sub
End Class
|
There are about 1095.75 days in 3 years.
1.4.3.Write some text based on the specified data type
|
|
Module Module1
Sub Main()
Console.Write(True) Console.Write(25) Console.Write("Some text.") Console.WriteLine() Console.WriteLine() Console.WriteLine(True) Console.WriteLine(25) Console.WriteLine("Some text.") End Sub
End Module
|
1.4.4.Concatenate strings in Console.WriteLine statement
|
|
Module Module1
Sub Main() Dim WebSite As String Dim Publisher As String = "AAAA"
Console.WriteLine("AAAA: " & Publisher)
WebSite = "CCCC." & Publisher & ".DDDD" Console.WriteLine("String one " & WebSite)
End Sub
End Module
|
String one CCCC.AAAA.DDDD
1.4.5.Reference variable index in Console.WriteLine
|
|
Module Module1
Sub Main() Dim A As Double = 1.23456789 Console.WriteLine("{0} {1} {2}", 1, 2, 3)
End Sub
End Module
|
1.4.6.Use Console.WriteLine to display various type variables
|
|
Module Module1
Sub Main() Dim A As Integer = 100 Dim B As Double = 0.123456789 Dim Message As String = "Hello, VB World!"
Console.WriteLine(A) Console.WriteLine("The value of A is " & A) Console.WriteLine(B) Console.WriteLine(B & " plus " & A & " = " & B + A) Console.WriteLine(Message)
End Sub
End Module
|
0.123456789 plus 100 = 100.123456789
1.5.1.Demonstrates scope rules and instance variables
|
|
Public Class Tester ' instance variable can be used anywhere in class Dim Shared value As Integer = 1
' demonstrates class scope and block scope Public Shared Sub Main Dim value As Integer = 5
Console.WriteLine("local variable value in" & _ " FrmScoping_Load is " & value )
MethodA() ' MethodA has automatic local value MethodB() ' MethodB uses instance variable value MethodA() ' MethodA creates new automatic local value MethodB() ' instance variable value retains its value
Console.WriteLine("local variable " & _ "value in FrmScoping_Load is " & value ) End Sub
' automatic local variable value hides instance variable Shared Sub MethodA() Dim value As Integer = 25 ' initialized after each call
Console.WriteLine("local variable " & _ "value in MethodA is " & value & " after entering MethodA" ) value += 1 Console.WriteLine("local variable " & _ "value in MethodA is " & value & " before exiting MethodA" ) End Sub
' uses instance variable value Shared Sub MethodB() Console.WriteLine("instance variable" & _ " value is " & value & " after entering MethodB" ) value *= 10 Console.WriteLine("instance variable " & _ "value is " & value & " before exiting MethodB" ) End Sub
End Class
|
local variable value in FrmScoping_Load is 5
local variable value in MethodA is 25 after entering MethodA
local variable value in MethodA is 26 before exiting MethodA
instance variable value is 1 after entering MethodB
instance variable value is 10 before exiting MethodB
local variable value in MethodA is 25 after entering MethodA
local variable value in MethodA is 26 before exiting MethodA
instance variable value is 10 after entering MethodB
instance variable value is 100 before exiting MethodB
local variable value in FrmScoping_Load is 5
|
|
Option Strict On
Public Class BlockScope Public Shared Sub Main() For outerLoop As Integer = 0 to 10000 For innerLoop As Integer = 0 to 10 Dim blockVar As Integer blockVar += 1 If blockVar Mod 1000 = 0 Then Console.WriteLine(blockVar) End If Next Next End Sub End Class
|
1.5.3.Variable block scope
|
|
public class Test public Shared Sub Main For i As Integer = 1 To 5 Dim j As Integer = 3 If i = j Then Dim M As Integer = i + j Console.WriteLine("M: " & M) Else Dim N As Integer = i * j Console.WriteLine("N: " & N) End If Dim k As Integer = 123 Console.WriteLine("k: " & k) Next i End Sub End class
|
1.5.4.Variable scope in try catch statement
|
|
public class Test public Shared Sub Main
Try Dim i As Integer = CInt("bad value") Catch ex As InvalidCastException Dim txt As String = "InvalidCastException" Console.WriteLine(txt) Catch ex As Exception Dim txt As String = "Exception" Console.WriteLine(txt) End Try
End Sub End class
|
1.5.5.Define variable inside If statement
|
|
public class Test public Shared Sub Main
Dim manager As Boolean = True If manager Then Dim txt As String = "M" : Console.WriteLine(txt) Else _ Dim txt As String = "E" : Console.WriteLine(txt)
End Sub End class
|
|
|
public class Test public Shared Sub Main DisplayHowardsName() DisplayStephsName() End Sub Shared Sub DisplayStephsName() Dim myName As String myName = "A"
Console.WriteLine(myName)
End Sub
Shared Sub DisplayHowardsName() Dim myName As String myName = "B"
Console.WriteLine(myName) End Sub End class
|
1.5.7.Function local variables
|
|
Module Module1
Sub F() Dim Name As String = "www.java2s.com" Dim Price As Double = 17.45 Dim I As Integer = 1001
Console.WriteLine("In F") Console.WriteLine("Name: " & Name) Console.WriteLine("Price: " & Price) Console.WriteLine("I: " & I) End Sub
Sub FF() Dim Name As String = "string" Dim Price As Double = 49.99 Dim I As Integer = 0
Console.WriteLine("In FF") Console.WriteLine("Name: " & Name) Console.WriteLine("Price: " & Price) Console.WriteLine("I: " & I) End Sub
Sub Main() F() Console.WriteLine() FF() End Sub
End Module
|
1.5.8.Local variable shadows global variable with the same name
|
|
Module Module1
Dim Counter As Integer
Sub BigLoop() For Counter = 1000 To 1005 ' Use global Counter Console.Write(Counter & " ") Next End Sub
Sub LittleLoop() Dim Counter As Integer
For Counter = 0 To 5 ' Use local Counter Console.Write(Counter & " ") Next End Sub
Sub Main() Counter = 100
Console.WriteLine("Starting Counter: " & Counter) BigLoop() Console.WriteLine("Counter after BigLoop: " & Counter) LittleLoop() Console.WriteLine("Counter after LittleLoop: " & Counter)
If (Counter > 1000) Then Dim Counter As Integer = 0
Console.WriteLine("Counter in If statement: " & Counter) End If
Console.WriteLine("Ending Counter: " & Counter) End Sub
End Module
|
1000 1001 1002 1003 1004 1005 Counter after BigLoop: 1006
0 1 2 3 4 5 Counter after LittleLoop: 1006
Counter in If statement: 0
1.5.9.Module global variable
|
|
Module Module1
Sub Main() For intLoopIndex As Integer = 0 To 5 System.Console.WriteLine(Tracker()) Next intLoopIndex End Sub
Dim intCount As Integer
Function Tracker() As Integer intCount += 1 Return intCount End Function
End Module
|
1.6.1.class definition with namespace
namespace WinForms public class HelloWorld shared sub Main() System.Console.WriteLine("Hello World") end sub end class end namespace
|
1.6.2.Define your own namespace
|
|
Namespace MyNamespace Public Class Class2
End Class End Namespace
Namespace MyNamespace Public Class Class1
End Class End Namespace
Module mod1 Sub main() Dim objClass2 As MyNamespace.Class2 Dim objClass1 As MyNamespace.Class1 End Sub End Module
|
|
|
Namespace MyApp.Info Module Main Sub Main() Dim objHW As New MyApp.Info.Utilities objHW.DisplayData() End Sub End Module
Public Class Utilities 'Run the application Public Sub DisplayData() Console.WriteLine(Environment.MachineName) Console.WriteLine(Environment.SystemDirectory) Console.WriteLine(Environment.GetLogicalDrives()) Console.WriteLine(Environment.Version.ToString()) End Sub End Class End Namespace
|
1.6.4.Use namespace to remove the conflicts
|
|
Namespace Network Class Address Public IP As String Public DomainName As String
Public Sub New(ByVal IPAddr As String, ByVal Domain As String) IP = IPAddr DomainName = Domain End Sub
Public Sub ShowAddress() Console.WriteLine("IP: " & IP) Console.WriteLine("Domain: " & DomainName) End Sub End Class
End Namespace
Namespace Mailing Class Address Public Street As String Public City As String Public State As String Public Zip As String
Public Sub New(ByVal Street As String, ByVal City As String, ByVal State As String, ByVal Zip As String) Me.Street = Street Me.City = City Me.State = State Me.Zip = Zip End Sub
Public Sub ShowAddress() Console.WriteLine("Street: " & Street) Console.WriteLine("City: " & City) Console.WriteLine("State: " & State) Console.WriteLine("Zip: " & Zip) End Sub End Class
End Namespace
Module Module1 Sub Main() Dim IP As New Network.Address("122.111.222.112", "www.SomeSite.com") Dim address As New Mailing.Address("122 Main", _ "Houston", "Texas", "77469")
IP.ShowAddress() Console.WriteLine() address.ShowAddress() End Sub
End Module
|
|
|
Option Strict On
Public Module Test Public Sub Main() For loopCtr As Integer = 1 to 10 Console.WriteLine(Invocations()) Next End Sub
Private Function Invocations() As Integer Static i As Integer i += 1 Return i End Function End Module
|
|
|
Option Strict On
Public Module CallStaticMethod Public Sub Main() Console.WriteLine(Greeting.SayHello()) End Sub End Module
Public Class Greeting Public Shared Function SayHello() As String Return "And a top of the morning to you!" End Function End Class
|
And a top of the morning to you!
1.8.1.Cause compiler error when Option Strict On
|
|
Module Module1 Sub Main()
Dim AnInt As Integer = 5 Dim ALong As Long = 7
ALong = AnInt 'causes compiler error when Option Strict On 'AnInt = ALong MsgBox(AnInt)
End Sub
End Module
|
1.8.2.Option Explicit Off
|
|
Option Explicit Off
Module Explicit Public Sub Main() For ctr As Integer = 0 to 100 ' Do something result = cntr Next Console.WriteLine("The counter reached " & result & ".") End Sub End Module
|
1.8.3.Turn Explicit off to use variable without declaration
|
|
Option Explicit Off
Module Module1
Sub Main() EmployeeName = "Buddy Jamsa" EmployeePhoneNumber = "555-1212" EmployeeSalary = 45000.0 NumberOfEmployees = 1
Console.WriteLine("Number of employees: " & NumberOfEmployees) Console.WriteLine("Employee name: " & EmployeName) Console.WriteLine("Employee phone number: " & EmployeePhoneNumber) Console.WriteLine("Employee salary: " & EmployeeSalary)
End Sub
End Module
|
Employee phone number: 555-1212
Wyszukiwarka
Podobne podstrony:
kurs vba Data Type 2 kolumnykurs vba Date Time 5 kolumnykurs vba Statements 4 kolumnykurs vba Operator 3 kolumnyEXCEL 2003 Kurs VBABody Language Basicskurs vba Data Type 2kurs vba Statements 4kurs vba Date Time 5kurs vba Operator 3Excel VBA Course Notes 1 Macro BasicsExcel VBA Course Notes 1 Macro BasicsExcel VBA Course Notes 1 Macro BasicsKURS ETYKIChoroba hemolityczna p odu na kurszapotrzebowanie ustroju na skladniki odzywcze 12 01 2009 kurs dla pielegniarek (2)kurswięcej podobnych podstron