VB|基础语法 变量定义 函数定义 循环语句 IF判断语句等

文章目录

  • 变量定义
  • Sub过程
  • 函数定义
  • 控制台输入输出
  • switch case语句
  • IF语句
  • FOR循环语句
  • 不等于
  • 逻辑运算符
  • 控制台输入回车不崩溃函数
  • 获取外部库指针内容放到自定义类中
  • 读ini文件

变量定义

'int
Dim 变量名 As Int32 = 0
'string
Dim 变量名 As String = ""
'bool
Dim 变量名 As Boolean = False
'枚举
Dim 变量名 As 枚举名
'数组
Dim array(256) As String
'自定义指针类
Dim 变量名 As 类名 = Nothing
'byte
Dim length As Int32 = 128
Dim buf(length) As Byte

Sub过程

参考:Excel-VBA基础(15):VBA中Sub过程、Function函数介绍
Sub过程是一系列由Sub和End Sub语句所包含起来的程序语句,它们会执行动作却不能返回一个值。Sub过程可有参数。如果一个Sub过程没有参数,则它的Sub语句必须包含一个空的圆括号。

其他过程调用Sub过程时,可以用Call语句,Sub过程的参数部分必须包含在括号中。假如直接调用Sub过程名称,则可以不用括号,参数用逗号分隔。

Private Sub 函数名()
	'dosomething
End Sub

函数定义

Function函数是一系列由Function和End Function语句所包含起来的程序语句。Function函数和Sub过程很类似,但Function函数可以返回一个数值。

Function函数可通过调用过程传递参数。例如,常数、变量或是表达式。如果一个 Function函数没有参数,它的Function语句必须包含一个空的圆括号。函数代码中的一个或多个语句将指定一个值给函数名称来返回值。

Function 函数名(ByRef 参数名 As 参数类型,同前逗号分隔可添加多个变量) As Int32 '好像是返回值
    'dosomething    
End Function

控制台输入输出

Console.WriteLine("输入内容")
Console.Write("输入内容")
Console.WriteLine("Write Path:")
Dim path As String = ""
path = Console.ReadLine()
Console.WriteLine("Path is {0}:",path)
Dim key As Char = Console.ReadKey().KeyChar
Dim Idx As UInt32 = (Convert.ToUInt32(key) - Convert.ToUInt32(Chr(&H30)))

switch case语句

 Select Case Idx
            Case 1
            Console.WriteLine("1")
            Case 2
            Console.WriteLine("2")
            Case 3
            Console.WriteLine("3")
            Case Else
            Console.WriteLine("Case Else")
 End Select

IF语句

Dim rtn As Int32 = 0
If rtn = 0 Then
   Console.WriteLine("rtn = 0 ")
Else
	Console.WriteLine("rtn != 0")
End If

FOR循环语句

Dim i As Int32 = 0
For i = 0 To 10
	Console.WriteLine("i:{0}", i)
	i=i+1
Next

不等于

' 不等于 <> 等于=
If a <> b Then
'dosomething
End If

逻辑运算符

' 按优先级 非(Not)>与(And)>或(Or)>异或(Xor)

控制台输入回车不崩溃函数

 Function GetInputValue()
        Dim Idx As UInt32 = 99
        Dim key As Char = Console.ReadKey().KeyChar
        Dim comp As Int64 = (Convert.ToInt64(key) - Convert.ToInt64(Chr(&H30)))
        If comp > 99 Or comp < 0 Then
            Console.WriteLine()
            Return Idx
        End If
        Idx = comp
        Return Idx
    End Function

获取外部库指针内容放到自定义类中

Public Structure NODEARRAY
	Public node_ As IntPtr
End Structure
'写在类的开头
Private Declare Function 外部库函数名 Lib "someone.dll" (ByVal nodeList As IntPtr, ByRef nodeListSize As UInt32) As Int32

 Public Function 内部库函数名(ByRef nodeList As ArrayList, ByRef nodeListSize As UInt32) As Int32
            '第一遍调用为了获取size
            Dim rtn As Int32 = 外部库函数名(IntPtr.Zero, nodeListSize)
            If rtn = 0 And nodeListSize > 0 Then
                Dim ndList(nodeListSize) As NODEARRAY
                Dim comp As Int32 = nodeListSize
                Dim ndListpt As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(NODEARRAY)) * comp)
                '第二遍调用为了获取内容
                rtn = 外部库函数名(m_node, ndListpt, nodeListSize)
                For i = 0 To nodeListSize - 1
                    Dim ptr As IntPtr = New IntPtr(ndListpt.ToInt64 + i * Marshal.SizeOf(GetType(NODEARRAY)))
                    ndList(i) = Marshal.PtrToStructure(ptr, GetType(NODEARRAY))
                    Dim ndHandle As IntPtr = ndList(i).node_
                    Dim nodeN As 自定义类名 = New 自定义类名(ndHandle)
                    nodeList.Add(nodeN)
                Next
            End If
            Return rtn
        End Function

读ini文件

 ' ini读
    Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

'从IniFile中读数据
Function ReadIniFile(FileName As String, AppName As String, KeyName As String) As String
        Dim ReturnStr As String = ""
        Dim ReturnLng As Integer
        ReadIniFile = vbNullString
        ReturnStr = Space(1024)
        ReturnLng = GetPrivateProfileString(AppName, KeyName, "path is wrong", ReturnStr, 1024, FileName)
        ReadIniFile = ReturnStr.Substring(0, ReturnLng)
End Function
'调用
 Dim filepath As String = Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "configuration.ini"
If (Not System.IO.File.Exists(filepath)) Then
	Return False
End If
Dim content As String  = ReadIniFile(filepath, "CardConfiguration", "int")
      

VB|基础语法 变量定义 函数定义 循环语句 IF判断语句等_第1张图片

你可能感兴趣的:(VB,开发语言)