Back to Top

Daily Archives: 2016年3月26日

VB.NETキャスト

VBは暗黙的なデータ型変換が通常行われます。またキャストにはいろいろな方法があります。

Option Strict StatementをONにすることにより、それを制限することができます。

1.[プロジェクト] メニューの [プロパティ] をクリックします

2.[コンパイル] タブで、[Option Strict] ボックスの値をONに設定します。

暗黙的なデータ型変換を拡大変換だけに制限し、遅延バインディングを禁止し、Object 型になる暗黙の型指定を禁止します。

Convertによるキャスト

Dim f As Single = 100.5
Dim i As Integer = Convert.ToInt32(f)

CInt()等によるキャスト

 Dim f As Single = 1.5F
 Dim i As Integer = CInt(f)

CType()によるキャスト

'Single型をInteger型に変換
Dim f As Single = 1.5F
Dim i1 As Integer = CType(f, Integer)

'Long型をInteger型に変換する 
Dim l As Long = 100
Dim i2 As Integer = CType(l, Integer)

'参照変換 
Dim btn As New System.Windows.Forms.Button()
Dim ctl As System.Windows.Forms.Control = CType(btn, System.Windows.Forms.Control)

'変換演算子による変換 
Dim pnt As System.Drawing.Point = New Point(0, 0)
Dim siz As System.Drawing.Size = CType(pnt, System.Drawing.Size)

TryCast()によるキャスト

Dim lbl As New System.Windows.Forms.Label()
Dim ctl As System.Windows.Forms.Control = TryCast(lbl, System.Windows.Forms.Control)

DirectCast()によるキャスト

Dim lbl As New System.Windows.Forms.Label()
Dim ctl As System.Windows.Forms.Control = DirectCast(lbl, System.Windows.Forms.Control)

 

キャストできるか調べる

Dim lbl As New System.Windows.Forms.Label()

If TypeOf lbl Is System.Windows.Forms.Control Then
    Dim ctl1 As System.Windows.Forms.Control = CType(lbl, System.Windows.Forms.Control)
End If


Dim ctl2 As System.Windows.Forms.Control = TryCast(lbl, System.Windows.Forms.Control)
If ctl2 IsNot Nothing Then
    MessageBox.Show("キャストに成功")
End If

 

 

VB.NETストラクチャー(Structure)

構造体のストラクチャーについて説明します。

通常のクラスとの違いは通常のクラスが参照型であるのに対し、構造体が値型であることです。

ストラクチャーを定義するには

<属性> Structure <構造体名>
 <属性> <メンバー名> As <変数型名>
 :
End Structure

ストラクチャー例

Public Structure Person
    Implements IDisposable

    Private m_Name As String
    Private m_Age As Integer

    'Newで初期化された場合用のコンストラクタ
    Sub New(ByVal name As String, ByVal age As Integer)
            Me.m_Name = name
            Me.m_Age = age
    End Sub

    'IDisposableインターフェースの実装デストラクタ
    Public Sub Dispose() Implements System.IDisposable.Dispose
            Console.WriteLine("Disposeが呼び出された。")
    End Sub

    '名前を設定する
    Public Sub SetName(ByVal name As String)
        Me.m_Name = name
    End Sub

    '名前を取得する
    Public Function GetName() As String
        Return (Me.m_Name)
    End Function

    '年齢を設定する
    Public Property Age() As Integer
        Get
            Return m_Age
        End Get
        Set(ByVal value As Integer)
            m_Age = value
        End Set
    End Property

End Structure

通常のクラスの使い方とあまり変わりません。通常では、ストラクチャーを使うことはあまりないと思います。

固定長の配列として扱いたい場合などに使用します。

固定長で宣言する

StructLayoutを使用するためImports System, System.Runtime.InteropServicesを追加します。

必ずしもStructLayoutの設定はいらないのですが、メモリの確保の仕方が32bit,64bitなどの機械(OS)により異なります。
ので以下はバイト単位でメモリを確保したいので指定します。(通常ではバイト単位で連続に確保されない)

Imports System, System.Runtime.InteropServices

<StructLayout(LayoutKind.Sequential, Pack:=1)>
Friend Structure Person
    <VBFixedArray(20 - 1)> Public Name() As Byte
    <VBFixedArray(2 - 1)> Public Age() As Byte
End Structure

LayoutKind列挙体は、アンマネージ コードにエクスポートするときにオブジェクトのレイアウトを制御します。