. The method of setting joint structure in net and the characteristic application of consortium

Source: Internet
Author: User
Tags format end integer net numeric value variables requires string

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!

  1. Union Myunion
  2. {
  3. Char b; //Single byte integer, using char type in C language to represent single-byte integers
  4. Short S; //Double byte integer
  5. int i; //Four-byte integer
  6. }

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.

  1. Improts System.Runtime.InteropServices ' introduces runtime unmanaged data management Services

Introducing structural attributes to precisely control the position of elements in a structure

  1. <structlayout (layoutkind.explicit) > _
  2. Structure Myunion
  3. ' Set the offset value of the field, set to 0
  4. <fieldoffset (0) > Dim b as byte single-byte integer
  5. <fieldoffset (0) > Dim s as Short ' Double-byte integer
  6. <fieldoffset (0) > Dim i as integer ' four-byte integer
  7. 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:

  1. Dim MU as New myunion
  2. MsgBox (String. Format ("{0} {1} {2}", mu.b, Mu.s, mu.i)) ' 0 0 0
  3. Mu.s = Int16.maxvalue
  4. MsgBox (String. Format ("{0} {1} {2}", mu.b, Mu.s, mu.i)) ' 255 32767 32767
  5. MU.B = 12
  6. MsgBox (String. Format ("{0} {1} {2}", mu.b, Mu.s, mu.i)) ' 32524 32524
  7. MU.I = 0
  8. 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:

  1. <structlayout (layoutkind.explicit) > _
  2. Structure MyUnion2
  3. <fieldoffset (0) > Dim b1 as Byte
  4. <fieldoffset (1) > Dim b2 as Byte
  5. <fieldoffset (2) > Dim b3 as Byte
  6. <fieldoffset (3) > Dim b4 as Byte
  7. <fieldoffset (0) > Dim i as Integer
  8. <fieldoffset (0) > Dim UI as UInteger
  9. 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:

  1. Dim MU as MyUnion2
  2. MsgBox (Mu.i & ": & Mu.ui)" 0:0
  3. MsgBox (String. Format ("{0} {1} {2} {3}", Hex (MU.B1), Hex (MU.B2), Hex (mu.b3), Hex (MU.B4))) ' 0 0 0 0
  4. MU.B1 = 255:MU.B2 = 255:mu.b3 = 255:MU.B4 = 255
  5. MsgBox (String. Format ("{0} {1} {2} {3}", Hex (MU.B1), Hex (MU.B2), Hex (mu.b3), Hex (MU.B4))) ' FF FF FF FF
  6. MsgBox (Mu.i & ": & Mu.ui)" -1:4294967295


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.