- Back to Home »
- VBA »
- [VBA] Data_type
Friday, November 29, 2019
1.Basic
Option Explicit
Public Const AGE_COL As String = "B"
Private Const NAME_COL As String = "B"
'define EUM
Enum EMessages
Welcome = 1
Error = 2
Warning = 3
End Enum
'define Struct
Type SMyType
SomeString As String
SomeLong As Long
End Type
Sub enum_type()
Debug.Print EMessages.Welcome
End Sub
Sub struct_type()
Dim t As SMyType
t.SomeString = "Hello"
t.SomeLong = 10
Debug.Print t.SomeString
Debug.Print t.SomeLong
End Sub
Sub basic_type()
Dim a1 As Integer
Dim a2 As Long
Dim a3 As Double
Dim a4 As String
Dim a5 As Object
Set a5 = CreateObject("Scripting.FileSystemObject")
Dim a6 As Variant
End Sub
Sub array_type()
Dim a7() As Variant 'array
ReDim a7(0) 're-size array loss data
ReDim Preserve a7(3) 're-size array without loss data
'set value
a7(0) = 1
a7(1) = 2
a7(2) = 3
a7(3) = 4
'array lengh
Dim arrayLen As Integer
arrayLen = getArrayLen(a7)
'list up
Dim i As Integer
For i = 0 To arrayLen - 1
Debug.Print a7(i)
Next i
End Sub
Public Function getArrayLen(arr() As Variant) As Integer
Dim isInitialised As Boolean
isInitialised = False
On Error Resume Next
isInitialised = IsNumeric(UBound(arr))
On Error GoTo 0
If isInitialised = True Then
getArrayLen = UBound(arr) - LBound(arr) + 1
Else
getArrayLen = 0
End If
' Debug.Print "getArrayLen: " & getArrayLen
End Function
Sub dictionary_type()
'Declare
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Dim child As Object
Set child = CreateObject("Scripting.Dictionary")
'Set value
child("a") = "aa"
child("b") = "bb"
child("c") = "cc"
dict("mot") = 1
dict("hai") = 2
dict("ba") = 3
Set dict("child") = child
'---Add item (key must not already exist)
'dict.Add Key, Value
'e.g.dict.Add "Apples", 50
'list up
Dim key As Variant
For Each key In dict.Keys
If key = "child" Then
Dim tmpobj As Object
Dim tmpkey As Variant
Set tmpobj = dict(key)
For Each tmpkey In tmpobj.Keys
Debug.Print key, tmpkey, tmpobj(tmpkey)
Next tmpkey
Else
Debug.Print key, dict(key)
End If
Next key
'---Check if key exists
'dict.Exists (key)
'e.g. If dict.Exists("Apples") Then
'
'---Remove Item
'dict.Remove key
'e.g.dict.Remove "Apples"
'
'
'---Remove all items
'dict.RemoveAll
'---Get the number of items
'dict.Count
'
'destructure at the end
Set child = Nothing
Set dict = Nothing
End Sub
Sub QueueTest()
' Dim q As Queue '// System.Collections.Queue
Dim q As Object 'As mscorlib.Queue '// System.Collections.Queue
Dim sDeq As String '// Dequeue結果
'// インスタンス生成
' Set q = New Queue
Set q = CreateObject("System.Collections.Queue")
'// データを4つ追加
Call q.Enqueue("1")
Call q.Enqueue("2")
Call q.Enqueue("3")
Call q.Enqueue("4")
'// 4を出力
Debug.Print q.Count
'// 1つ取り出し+削除("1"を削除)
sDeq = q.Dequeue
'// 取り出した"1"を出力
Debug.Print sDeq
'// 3を出力
Debug.Print q.Count
'// キューに値が含まれる場合
If (q.Contains("3") = True) Then
Debug.Print q.Contains("3")
End If
'// キューに値が含まれない場合
If (q.Contains("AAA") = False) Then
Debug.Print q.Contains("AAA")
End If
'// 1つ取り出し+削除はしない
sDeq = q.Peek()
'// 取り出した"2"を出力
Debug.Print sDeq
'// 3を出力
Debug.Print q.Count
End Sub
※https://vbabeginner.net/net-framework-queue/3. ArrayList
Sub DNET_ArrayListTest1()
Dim aryList As Object '// ArrayListオブジェクト
Dim s '// 戻り値用
Dim obj As Object '// Clone用
Dim i As Long
'// .NET FrameworkのArrayListクラスを利用する
Set aryList = CreateObject("System.Collections.ArrayList")
'// 追加
Call aryList.Add("111")
Call aryList.Add("222")
Call aryList.Add("333")
Call aryList.Add("444")
'// 書き換え
aryList.Item(0) = "AAA"
'// 取得
s = aryList.Item(0)
'// 要素数
s = aryList.Count
For i = 0 To s - 1
Debug.Print "Item ", i, aryList.Item(i)
'Item 0 AAA
'Item 1 222
'Item 2 333
'Item 3 444
Next
'// 反転
Call aryList.Reverse
'// ソート
Call aryList.Sort
'// 指定要素削除
Call aryList.Remove("AAA")
'// 指定インデックス削除
Call aryList.RemoveAt(0)
'// 複写
Set obj = aryList.Clone
End Sub
※https://vbabeginner.net/net-framework-queue/