Introduction: In the C language programming of some algorithms, several different types of variables need to be stored in the same memory unit. That is, using overlay technology, several variables cover each other. These different variables collectively occupy a memory structure, in the C language, is called the "common" type structure, referred to as the common body, also called a consortium.
In C + + there is a type called a federation (also called a common body), its keyword is union, in use and structure struct very similar, can contain any structure type data, but it has a very unique feature, that is all the data point to an address.
This means you can understand that all data in a consortium refers to the same address in a block of memory, and when we change the value of any one of the data in the consortium, the value of the other data changes.
This is very effective for unknown types of data, you can use a federation to load a data and then analyze whether its data is valid, or you can perform a bitwise operation on some special types to get the numeric value of its particular position.
But in vb.net or C #, there is no union keyword to make us famous, but what can we do to make a name for the consortium?
This requires the use of structural attributes!
Let's take a look at how to convert the following C + + federation code into a vb.net federated structure!
- Union Myunion
- {
- Char b; //Single byte integer, using char type in C language to represent single-byte integers
- Short S; //Double byte integer
- int i; //Four-byte integer
- }
|
The Federation size is 4 bytes, each of which is represented as a single-byte, Double-byte, four-byte integer, and any changes in its data during the run affect other data.
- Improts System.Runtime.InteropServices ' introduces runtime unmanaged data management Services
|
Introducing structural attributes to precisely control the position of elements in a structure
- <structlayout (layoutkind.explicit) > _
- Structure Myunion
- ' Set the offset value of the field, set to 0
- <fieldoffset (0) > Dim b as byte single-byte integer
- <fieldoffset (0) > Dim s as Short ' Double-byte integer
- <fieldoffset (0) > Dim i as integer ' four-byte integer
- End Structure
|
This is where. NET, the method of setting the federated structure in C # is similar to that in vb.net, here is not to repeat.
The following is an example of an application of the properties of a consortium, as in the case of the consortium structure that we have just described, the following code will demonstrate the characteristics of the consortium:
- Dim MU as New myunion
- 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)) ' 255 32767 32767
- MU.B = 12
- MsgBox (String. Format ("{0} {1} {2}", mu.b, Mu.s, mu.i)) ' 32524 32524
- MU.I = 0
- MsgBox (String. Format ("{0} {1} {2}", mu.b, Mu.s, mu.i)) ' 0 0 0
|
The above code can more visually show the changes in memory, and whenever you change federated data, other data changes as memory changes.
Of course, there are limits to the use of consortia, and that is. NET is only applicable to value types, cannot be applied to reference types and pointers, you cannot set a string or an array into a union type, which requires great attention!
Of course. NET's federated structure is not only the use of the above, you can play your imagination, such as can:
- <structlayout (layoutkind.explicit) > _
- Structure MyUnion2
- <fieldoffset (0) > Dim b1 as Byte
- <fieldoffset (1) > Dim b2 as Byte
- <fieldoffset (2) > Dim b3 as Byte
- <fieldoffset (3) > Dim b4 as Byte
- <fieldoffset (0) > Dim i as Integer
- <fieldoffset (0) > Dim UI as UInteger
- End Structure
|
This structure can get a four-byte integer with or without symbols for each byte of data, without having to write an algorithm to parse it.
Test code:
Code:
- Dim MU as MyUnion2
- 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 FF FF
- MsgBox (Mu.i & ": & Mu.ui)" -1:4294967295
|