dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: VB7
Expertise: Intermediate
Oct 28, 2002

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Using a union to retrieve the RGB components of a color

In VB.NET you can create what in C is called a union, i.e. a particular structure where you can access the same memory with different names. Here's how you declare such a structure by using the StructLayout attribute, and specifying that you want to define the structure layout explicitly, namely how the fields will be saved in memory within the type.

<StructLayout(LayoutKind.Explicit)> Structure ARGBColor
  <FieldOffset(0)> Dim Red As Byte
  <FieldOffset(1)> Dim Green As Byte
  <FieldOffset(2)> Dim Blue As Byte
  <FieldOffset(3)> Dim Alpha As Byte
  <FieldOffset(0)> Dim Value As Integer
End Structure
If you set the Value field, you're actually setting all the 4 bytes of the structure in a single step. Otherwise you can get and set the single bytes of the structure with the other fields. Here's how you can set the Value field of a variable of type ARGBColor to an hexadecimal value that represents a color, and then read the other fields to retrieve its RGB components:

' Split a color into its components.
Dim rgb As ARGBColor
rgb.Value = &H112233         ' This is equal to 1122867.
Console.WriteLine(String.Format("Red={0}, Green={1}, Blue={2}", rgb.Red, _
    rgb.Green, rgb.Blue))
    ' => Red=51, Green=34, Blue=17
--------------------------
Note: this code is taken from Francesco Balena's Programming Microsoft Visual Basic .NET book (MS Press 2002)
Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date