Fieldoffset features of VB.NET feature

xiaoxiao2021-03-06  53

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

Dim Red as Byte

DIM GREEN AS BYTE

DIM Blue as Byte

DIM Alpha as Byte

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

Dim Red as Byte

DIM GREEN AS BYTE

DIM Blue as Byte

DIM Alpha as Byte

DIM VALUE AS INTEGER

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!

转载请注明原文地址:https://www.9cbs.com/read-115181.html

New Post(0)