Last active
July 3, 2019 03:06
-
-
Save qtkite/84018864f381996b4317e77e14b291a6 to your computer and use it in GitHub Desktop.
FlagSys is a simple class you can integrate into any VB.NET project to make use of flags using bitwise operators.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Just a small class I wrote that allows simple integration of the use of flags | |
''' <summary> | |
''' Flag sys is a class for any program that allows integration of a flag based system | |
''' </summary> | |
Public Class FlagSys | |
Dim iFlag As Integer = 0 | |
''' <summary> | |
''' Constructor that automatically assigns a flag if a value is passed | |
''' </summary> | |
Sub New(Optional flag As Integer = 0) | |
AddFlag(flag) | |
End Sub | |
''' <summary> | |
''' Adds a flag into the our global flag object | |
''' </summary> | |
Sub AddFlag(flag As Integer) | |
iFlag = iFlag Or flag | |
End Sub | |
''' <summary> | |
''' Removes a flag from our global flag object | |
''' </summary> | |
Sub RemoveFlag(flag As Integer) | |
iFlag = iFlag And Not flag | |
End Sub | |
''' <summary> | |
''' References if a flag exist in our iFlags container | |
''' </summary> | |
Function CheckFlag(flag As Integer) As Boolean | |
Return (iFlag And flag) | |
End Function | |
''' <summary> | |
''' Unsets all our flags | |
''' </summary> | |
Sub RemoveAllFlags() | |
iFlag = 0 | |
End Sub | |
End Class | |
' Example Code: | |
Public Class Main | |
Enum ExampleFlags As Integer | |
Flag_1 = 1 | |
Flag_2 | |
Flag_3 | |
End Enum | |
''' <summary> | |
''' Event function as the main form loads | |
''' </summary> | |
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load | |
' Test that we added a flag and it shows up | |
Dim FlagTest As New FlagSys(ExampleFlags.Flag_1) | |
If (FlagTest.CheckFlag(ExampleFlags.Flag_1)) Then | |
MessageBox.Show("Contains Flag_1") | |
End If | |
If (FlagTest.CheckFlag(ExampleFlags.Flag_2)) Then | |
MessageBox.Show("This shouldn't show up") | |
End If | |
' Test that the flag is removed | |
FlagTest.RemoveFlag(ExampleFlags.Flag_1) | |
If (FlagTest.CheckFlag(ExampleFlags.Flag_1)) Then | |
MessageBox.Show("This shouldn't show up either") | |
End If | |
End Sub | |
End Class | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment