|
|
|
As explained in the tip about the ARGBColor structure (look at the end of this tip for the link), we can define a structure and have its fields that point to the same memory address, but that read a different number of bytes. This makes easier to define a structure that allows us to set a value to a field, and then retrieve the single bytes/words of that value with the other fields. Here's how we can define this structure: |
Click here to copy the following block | <StructLayout(LayoutKind.Explicit)> Structure IntegerTypes <FieldOffset(0)> Dim Long0 As Long <FieldOffset(0)> Dim Integer0 As Integer <FieldOffset(4)> Dim Integer1 As Integer <FieldOffset(0)> Dim Short0 As Short <FieldOffset(2)> Dim Short1 As Short <FieldOffset(4)> Dim Short2 As Short <FieldOffset(6)> Dim Short3 As Short <FieldOffset(0)> Dim Byte0 As Byte <FieldOffset(1)> Dim Byte1 As Byte <FieldOffset(2)> Dim Byte2 As Byte <FieldOffset(3)> Dim Byte3 As Byte <FieldOffset(4)> Dim Byte4 As Byte <FieldOffset(5)> Dim Byte5 As Byte <FieldOffset(6)> Dim Byte6 As Byte <FieldOffset(7)> Dim Byte7 As Byte
Function LowByte(ByVal Value As Long) As Byte Long0 = Value Return Byte0 End Function
Function HighByte(ByVal Value As Long) As Byte Long0 = Value Return Byte1 End Function
Function LowWord(ByVal Value As Long) As Short Long0 = Value Return Short0 End Function
Function HighWord(ByVal Value As Long) As Short Long0 = Value Return Short1 End Function
End Structure |
Here's how you can test the IntegerTypes structure to retrieve the hi/low byte/word of a value, and to do other operations: |
Click here to copy the following block | Dim it As IntegerTypes
it.Short0 = 517 Console.WriteLine(it.Byte0) Console.WriteLine(it.Byte1)
Console.WriteLine(it.LowByte(517)) Console.WriteLine(it.HighByte(517)) Console.WriteLine(it.LowWord(&HFFFF1000)) Console.WriteLine(it.HighWord(&HFFFF1000)) |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|