Introduction: when programming some algorithms in C language, you need to store several different types of variables in the same memory unit. That is to say, using the overwrite technology, several variables overwrite each other. These variables occupy a memory structure. In C, these variables are called the "shared body" type structure.
In C ++, a type is called a consortium (also called a shared body). Its keyword is union, which is very similar to the structure struct and can contain any structure data, however, it has another unique feature, that is, all data points to an address.
This means that all data in a consortium references data with the same address in a memory block. When we change the value of any data in the consortium, the values of other data will change accordingly.
This is very effective for data of unknown types. You can use a consortium to load a data, analyze whether the data is valid, or perform bitwise operations on some special types, obtains the value at a specific position.
However, in VB. NET or C #, there is no union keyword that makes us famous for the Consortium. But how can we be famous for the consortium?
This requires structure attributes!
Let's take a look at how to convert the following C ++ consortium code into a VB. NET consortium structure!
- UnionMyunion
- {
- CharB; // a single-byte integer. In C, the char type is used to represent a single-byte integer.
- ShortS; // double-byte integer
- IntI; // four-byte integer
- }
|
The size of this consortium is 4 bytes, each of which is represented as a single-byte, dual-byte, and four-byte integer. Any data changes during running will affect other data.
- Improts System. Runtime. interopservices' introduce the Runtime unmanaged Data Management Service
|
Introduce structure attributes to precisely control the positions of elements in the Structure
- <StructLayout (LayoutKind. Explicit)> _
- StructureMyUnion
- 'Set the Offset Value of the field to 0.
- <FieldOffset (0)>DimBAs Byte'Single-byte integer
- <FieldOffset (0)>DimSAs Short'Dubyte integer
- <FieldOffset (0)>DimIAs Integer'Four-byte integer
- End Structure
|
This is the method for setting the consortium structure in. NET. The setting method in C # In VB. NET is the same. I will not repeat it here.
The following describes the feature application of a consortium. The code below demonstrates the features of a consortium based on the well-known consortium structure as an example:
- DimMUAs NewMyUnion
- MsgBox (String. Format ("{0} {1} {2}", MU. B, MU. s, MU. I) '0 0 0
- MU. s = Int16.MaxValue
- MsgBox (String. Format ("{0} {1} {2}", MU. B, MU. s, MU. I) '2017 255 32767
- MU. B = 12
- MsgBox (String. Format ("{0} {1} {2}", MU. B, MU. s, MU. I) '12 32524 32524
- MU. I = 0
- MsgBox (String. Format ("{0} {1} {2}", MU. B, MU. s, MU. I) '0 0 0
|
The code above can display the changes in the data in the memory more intuitively. When the Union data is changed, other data also changes with the memory.
Of course, there are limits on the use of consortium, that is. NET consortium is only applicable to value types and cannot be applied to reference types and pointers. You cannot set strings or arrays to enter the consortium type. This requires great attention!
Of course, the Consortium struct in. NEt is not only used above. You can use your imagination, for example:
- <StructLayout (LayoutKind. Explicit)> _
- StructureMyUnion2
- <FieldOffset (0)>DimB1As Byte
- <FieldOffset (1)>DimB2As Byte
- <FieldOffset (2)>DimB3As Byte
- <FieldOffset (3)>DimB4As Byte
- <FieldOffset (0)>DimIAs Integer
- <FieldOffset (0)>DimUiAsUInteger
- End Structure
|
This structure can be used to obtain the data of each byte of a four-byte integer with or without the need to write algorithms for analysis.
Test code:
Code:
- DimMUAsMyUnion2
- MsgBox (MU. I & ":" & MU. ui) '0: 0
- MsgBox (String. Format ("{0} {1} {2} {3}", Hex (MU. b1), Hex (MU. b2), Hex (MU. b3), Hex (MU. b4) '0 0 0 0
- MU. b1 = 255: MU. b2 = 255: MU. b3 = 255: MU. b4 = 255
- MsgBox (String. Format ("{0} {1} {2} {3}", Hex (MU. b1), Hex (MU. b2), Hex (MU. b3), Hex (MU. b4) 'ff FF
- MsgBox (MU. I & ":" & MU. ui) '-1: 4294967295
|