1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
'Die Haupt-Klasse
Public MustInherit Class Bitmask
Protected m_Value As Long = &H0
#Region " Members "
Property Value() As Long
Get
Return m_Value
End Get
Set(ByVal value As Long)
m_Value = value
End Set
End Property
#End Region
Public Sub SetOn(ByVal position As Long)
m_Value = position Or m_Value
End Sub
Public Sub SetOff(ByVal position As Long)
m_Value = m_Value And Not position
End Sub
Public Function IsOn(ByVal position As Long) As Boolean
Return (m_Value And position) = position
End Function
Public Function GetLabel() As String
Try
'Maximal Option herausfinden
Dim nHighestPosition As Long
nHighestPosition = GetHighestPosition()
'Alle String's miteinander verketten
Dim sTempLabel As String = ""
For I As Long = 0 To nHighestPosition
If IsOn(CLng(2 ^ I)) = True Then
If sTempLabel <> "" Then sTempLabel &= ", "
sTempLabel &= GetPositionLabel(CLng(2 ^ I))
End If
Next
Return sTempLabel
Catch ex As Exception
'ToDo Fehlerbehandlung
Return ""
End Try
End Function
Public Function GetHighestPosition() As Long
Try
For I As Long = 32 To 0 Step -1
If GetPositionLabel(CLng(2 ^ I)) <> "" Then
Return I
End If
Next
Catch ex As Exception
'ToDo Fehlerbehandlung
Return 0
End Try
End Function
Protected MustOverride Function GetPositionLabel(ByVal position As Long) As String
End Class
'-----------------------------------
'Beispiel Klasse:
Public Class SampleAutoBitmask
Inherits Bitmask
Public Const Radio As Long = &H1
Public Const Navigationsgeraet As Long = &H2
Public Const Hintertueren As Long = &H4
Public Const Kofferraum As Long = &H8
Public Const Sechsgang As Long = &H10
'weitere... (Werte währen &H20, &H40, &H80, &H100, &H200, ...)
Protected Overrides Function GetPositionLabel(ByVal position As Long) As String
Try
Select Case position
Case Radio : Return "Radio"
Case Navigationsgeraet : Return "Navigationsgerät"
Case Hintertueren : Return "Hintertüren"
Case Kofferraum : Return "Kofferraum"
Case Sechsgang : Return "Sechsgang"
End Select
Return ""
Catch ex As Exception
'ToDo Fehlerbehandlung
Return ""
End Try
End Function
End Class
'-----------------------------------
'Das Beispiel:
Dim oSampleBitmask As SampleAutoBitmask
oSampleBitmask = New SampleAutoBitmask
'Einen Wert oder mehrere auf '1' setzen (mit dem logischen Operator 'Or' verknüpft)
oSampleBitmask.SetOn(SampleAutoBitmask.Radio Or SampleAutoBitmask.Hintertueren)
'Einen Wert oder mehrere auf '0' setzen (mit dem logischen Operator 'Or' verknüpft)
oSampleBitmask.SetOff(SampleAutoBitmask.Radio)
'Abfragen ob ein Wert auf '1' steht
Dim bWertOn As Boolean
bWertOn = oSampleBitmask.IsOn(SampleAutoBitmask.Radio)
'Alle Label anzeigen die auf '1' stehen
MsgBox(oSampleBitmask.GetLabel())
|