#Visual Basic
47
Microsoft Visual Basic , , Microsoft. Visual Basic , BASIC, . Visual Basic - - . VB , IntelliSense , .
Visual Basic (RAD) Microsoft Windows. , , - -, , , VB .
Visual Basic 3. Windows 5. VB6, Microsoft Visual Studio 6.0, - .
Visual Basic Dim, , - As . , As Variant. Option Explicit , . , Variant, , , .
Dim strMessage As String ' Dim iProductCount As Integer ' Dim dt70YearsOfVictory As Date ' Dim otherValue ' , Variant
Public Sub Main()
strMessage = ", !"
iProductCount = 125
dt70YearsOfVictory = #5/9/2015 2:00:00 PM# ' 9 2015 14:00:00
otherValue = 12.5 ' Variant, Double.
otherValue = "" ' Variant, String.
End Sub
Visual Basic , , , .
(=) . Let . BASIC , Visual Basic . , Set. : nVar = 10, Let iCount = 20, Set refDouble = objClass. , C, . A = B = C , A, B C . VB B = C A True False, 0 ?1, .
(+), (-), (*), (/) (^). : 2 ^ 3 = 8 (). , ( ) . : 5 \ 2 = 2 (Mod). , . : 5 Mod 2 = 1
(=). : If nVar = 10 Then , , (> <). : If nVar > 10 Then (>= <=). : If nVar >= 10 Then (<>). : If nVar <> 10 Then (Is). , . : If obj1 Is obj2 Then (Like). , . : If strEmail Like "@.*" Then
(And) , . : If (2 * 2 = 4) And (2 * 3 = 6) Then (Or) . : If (2 * 2 = 5) Or (2 * 2 = 4) Then (Not) True, . : If Not(2 * 2 = 5) Then (Xor) E1 Xor E2 True, E1 = True E2 = True, False. (Eqv) , True, . (Imp) False, E1 = True E2 = False, True.
(+) . , . , , , . , (&) . (&) . , str = "10" & 20 1020, 30. (+) , VB 10 10, , . Visual Basic, , , . , GoSub Return, BASIC, .
:
' If <> Then [___]
' If <> Then [___] Else [___]
' If <> Then
[___]
ElseIf <2> Then
[__2_]
ElseIf <N> Then
[__N_]
Else
[___]
End If :
Select Case <_> ' ,
Case <_1> ' , 1
[_1]
Case <_2>, <_3>, <_3> ' . ,
[_2]
Case <_5> To <_6> '
[_3]
Case Is >= <_7> '
[_4]
Case Else ' ,
[_5]
End Select :
'
Dim X As Double
X = InputBox(" ")
Select Case X ',
Case Is < 5, Is >= 20, 12 To 15 '
MsgBox " "
Case Else '
MsgBox " "
End Select :
For <> = <> To <_> [Step <_>]
[_]
[Exit For] '
Next [] : 1 100
For I = 0 To 100 Step 2
Sum = Sum + I
Next :
For Each <> In <>
[_]
[Exit For] '
Next [] :
Dim strItem As Variant
For Each strItem In Array("", "", "")
Print strItem
Next :
Do While <> ' ,
[_]
[Exit Do] '
Loop
Do Until <> ' ,
[_]
[Exit Do]
Loop
While <> ' ,
[_]
Wend :
Open "file.txt" For Input As #1
Do While Not EOF(1) ' True,
Line Input #1, strBuffer '
Debug.Print strBuffer '
Loop
Close #1 :
Do
[_]
[Exit Do]
Loop While <>
Do
[ ]
[Exit Do]
Loop Until <> . .
Sub End Sub, . Visual Basic, , , . , : Private Sub Command1_Click() ' ""
...
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) '
...
End Sub Visual Basic Windows. , WM_MOUSEMOVE Form_MouseMove : , . Windows, . , . Visual Basic . , , .
VB Function End Function. , . , , Exit Sub Exit Function. Visual Basic 6.0 , , , , , . , , , . :
Private Sub Main()
Dim RetX1 As Double, RetX2 As Double
Dim strStatus As String
strStatus = SolveQuadraticEquation(4, 6, 2, RetX1, RetX2) ' 4*X^2 + 6*X - 2 = 0
'
MsgBox ": " & strStatus & vbCrLf & _
"X1 = " & RetX1 & vbCrLf & _
"X2 = " & RetX2, vbInformation
End Sub
' Public Function SolveQuadraticEquation(ByVal A As Double, _
ByVal B As Double, _
ByVal C As Double, _
ByRef X1 As Double, _
ByRef X2 As Double) As String
Dim D As Double
D = (B * B) - (4 * A * C) '
If D >= 0 Then
X1 = (-B - Sqr(D)) / (2 * A) '
X2 = (-B + Sqr(D)) / (2 * A)
SolveQuadraticEquation = " " '
Else
SolveQuadraticEquation = " < 0. "
End If
End Function ByVal A, B C. , . ByRef, , . , , . Visual Basic - , .
Visual Basic . : (Form), (Module) (Class Module).
( *.frm) Visual Basic. , : , , . , , . , , . ( *.bas) , , . ( ) : , , , . , , ; , . ( *.cls) - Visual Basic. . , . , , , , . Class_Initialize Class_Terminate, . - Visual Basic. . VB , , , , . . , , .
. New . CreateObject, . Object, Set. ., . , WithEvents, .
Microsoft Scripting Runtime, :
Public Sub Main()
Dim objFSO As New FileSystemObject ' , New
Dim objWindows As Folder
Set objWindows = objFSO.GetFolder(Environ("SYSTEMROOT")) ' GetFolder
MsgBox " Windows: " & objWindows.Path '
MsgBox " : " & Format(objWindows.DateCreated, "YYYY-MM-DD H:mm:ss")
Dim sTextFilePath As String
Dim objTS As TextStream
sTextFilePath = objFSO.BuildPath(Environ("TEMP"), " .txt") ' String
Set objTS = objFSO.CreateTextFile(sTextFilePath, True)
objTS.Write ", !"
objTS.Close
objFSO.DeleteFile sTextFilePath, True
Set objFSO = Nothing ' Nothing, ,
Set objWindows = Nothing 'VB ,
Set objTS = Nothing
End Sub Visual Basic . , Java C++, Visual Basic .
, , . Visual Basic , , , . Visual Basic , [4]
Visual Basic. , Public, Private Friend, , , , .
. Visual Basic Implements. . , .
, VB . .
' ITransport.cls Public Function GetMaxSpeed() As Long End Function ' CAuto.cls Implements ITransport '
Private Function ITransport_GetMaxSpeed() As Long '
ITransport_GetMaxSpeed = 240
End Function ' CFly.cls Implements ITransport
Private Function ITransport_GetMaxSpeed() As Long
ITransport_GetMaxSpeed = 700
End Function ' Program.bas Option Explicit
Public Sub Main()
Dim T As ITransport ' ITransport
Set T = New cAuto
WhatTransportSpeed T ' , ,
Set T = New cFly
WhatTransportSpeed T
End Sub
' , , ITransport Public Sub WhatTransportSpeed(Transport As ITransport)
MsgBox " : " & Transport.GetMaxSpeed()
End Sub