Website Design United States, Website Design California, Website Designing United States, Website Designing California

Bitwise: Storing and Retrieving values in a variable - Visual Basic .NET

This example simply shows how to OR values together, then how to AND them back out, using the Bitwise operators.

This allows you to store multiple values (such as properties) into a single variable, then retrieve those values later.

In VB.Net the AND and OR functions are used for both LOGICAL and BITWISE comparisons.

Option Strict On

Module modBinary

'Make up a Enum of Binary values (count in Binary)
Private Enum Bin_Enum
ebValue1 = 1
ebValue2 = 2
ebValue3 = 4
ebValue4 = 8
ebValue5 = 16
ebValue6 = 32
ebValue7 = 64
ebValue8 = 128
End Enum

Sub Main()
'Create an instance of the Enum to use
Dim eTemp As Bin_Enum

'Assign some values to the Enum
'OR these values together, so one variable now contains
'multiple values
eTemp = Bin_Enum.ebValue1 Or Bin_Enum.ebValue4 Or Bin_Enum.ebValue7

'This is just a simple way to output results if
'the eTemp variable contains a certain value from the Enum list.
'This code can be easily modified to include other values.
If CBool(eTemp And Bin_Enum.ebValue1) Then Debug.Print("Value 1")
If CBool(eTemp And Bin_Enum.ebValue2) Then Debug.Print("Value 2")
If CBool(eTemp And Bin_Enum.ebValue3) Then Debug.Print("Value 4")
If CBool(eTemp And Bin_Enum.ebValue4) Then Debug.Print("Value 8")
If CBool(eTemp And Bin_Enum.ebValue5) Then Debug.Print("Value 16")
If CBool(eTemp And Bin_Enum.ebValue6) Then Debug.Print("Value 32")
If CBool(eTemp And Bin_Enum.ebValue7) Then Debug.Print("Value 64")
If CBool(eTemp And Bin_Enum.ebValue8) Then Debug.Print("Value 128")
End Sub

End Module

WEB DESIGN INDIA
42 B Malviya Nagar , New Delhi-110017

Skype: manmeetsi
Email: support.webdesignindia@gmail.com
Tel: 91-011-40502005, 9810067295

 















 


© 2008-2009 dotnet4all.com