When selecting the display layout, the definition of all variables in the structure must contain the fieldoffset feature. This feature specifies the distance (in byte position) from the start of the structure.
Imports system.Runtime.InteropServices
Structure test
End structure
The structlayout feature can be combined with FieldOffset features. Union has been adopted by a variety of languages, such as C and C , but VB does not have this language characteristic. Union (UNION) is a combination of two or more elements in the structure overlap in memory, and using different names to indicate the same memory location.
In .NET, the key to Union is to support the display structure layout.
Such as:
Imports system.Runtime.InteropServices
Structure test
End structure
Then the location of these elements is in memory, as shown in the figure:
This allows 4 bytes to be accessed as a whole via the Value field.
'Split
DIM RGB AS TEST
Rgb.value = & h112233 '1122867
Console.write ("red = {0}, green = {1}, blue = {2}", rgb.red, rgb.green, rgb.blue)
Output such as:
'merge
Rgb.Red = 51
Rgb.green = 34
Rgb.blue = 17
Console.write (RGB.Value)
Output such as:
This will solve a lot of conversion work, and faster than using the mathematical operator!