サンプルコード



自己再起動

    If Not (0 = Shell(App.Path & "\" & App.EXEName)) Then End


空の配列

C#でいうところの、nullが入った状態のことである。
基本的にVB6では、空っぽの配列を作成することはできない。
しかし、偉大な先人達が、いくつかの方法を見つけている。
  • null?を入れておく方法
VB6にはnullなどないので、それに変わる方法を使う。
いちおうmsdnにも記載されているらしいが、かなり裏技っぽい
   'Variant型の空の配列を作る
   Dim arr() As Variant
   arr = Array()

   '空っぽの判定
   On Error Resume Next
   If (UBound(arr)) Then
       Debug.Print "配列は空っぽ"
   End If
   
   'String型の空の配列を作る
   Dim StringArray() As String
   StringArray = Split("")
   
   'Byte型の空の配列を作る
   Dim ByteArray() As Byte
   ByteArray = ""

   'Object型の空の配列を作る
   Dim ObjectArray() As Object
   Call IsArray(ObjectArray)

  • APIを使う方法
Private Declare Function SafeArrayAllocDescriptor Lib "oleaut32" ( _
        ByVal cDims As Long, ByRef ppsaOut() As Any) As Long
Private Declare Sub GetMem4 Lib "msvbvm60" ( _
        ByVal ptr As Long, ByRef ret As Long)


  • 「常に目的のサイズ+1とし、サイズが1を空とみなす方法」
自分的には、読みやすさも考えて、これが好ましいと考える。
   Dim arr() As Integer
   ReDim arr(0)

   '空っぽの判定(本当は要素数1である)
   If (UBound(arr) = 0) Then
       '...
   End If
   
   '要素数を5に変更(本当は要素数6になる)
   ReDim arr(5)
   
   'ループ処理(cntは0~4の値となる)
   Dim cnt As Integer
   For cnt = 0 To UBound(arr) - 1
       '...
   Next cnt
   
   '末尾に要素を追加
   ReDim Preserve arr(UBound(arr) + 1)
   arr(UBound(arr) - 1) = 0


ビット演算

  • 指定位置のバイトを取り出す
'source:取り出し元 / destination:取り出したデータ
Dim  source as Long, destination as Long
'最下位バイトを取り出す dest = src & 0xff
destination = source And &HFF&
'2バイト目を取り出す dest = (src >> 8) & 0xff
destination = (source / &H100) And &HFF&	
'3バイト目を取り出す dest = (src >> 16) & 0xff
destination = (source / &H10000) And &HFF&	
'最下位バイトを取り出す dest = (src >> 24) & 0xff
destination = (source / &H1000000) And &HFF&


ファイル

Dim fno as Integer, str As String
fno = FreeFile
Open "c:\sample.txt" For Input As #fno
Line Input #fno, str
Close #fno
最終更新:2011年04月06日 13:59