Using C# Property to convert ‘C’ #define macro

Given these two ‘C’ macros to convert to C#:

#define CFG_FILE(dwChoice) ((dwChoice >> 12) & 0x000F)
#define CFG_ELEMENT(dwChoice) (dwChoice & 0x01FF)

The solution I instituted was to create a class containing the results of the two macros. The macros are placed in set accessor of a property with the result being saved in a private variable (to implement data hiding).

using System;
using System.Collections.Generic;
using System.Text;

namespace TestCfgWord
{
    class clsCfgWord
    {
        private uint uiTableType;
        private uint uiTableElement;
        private uint uiCfgWord;

        public uint TableType
        {
            get
            {
                return uiTableType;
            }

            set
            {
                uiTableType = (value >> 12) & 0x000F;
            }

        }

        public uint TableElement
        {
            get
            {
                return uiTableElement;
            }
            set
            {
                uiTableElement = value & 0x01FF;
            }
        }

        public uint CfgWord
        {
            get
            {
                return uiCfgWord;
            }
            set
            {
                uiCfgWord = value;
                TableType = value;
                TableElement = value;
            }
        }
    }
}

To use the CfgWord class:

clsCfgWord CfgWord = new clsCfgWord();

private void cmdXferToStruct_Click(object sender, EventArgs e)
{
    CfgWord.CfgWord = Convert.ToUInt16(mtxtCfgWord.Text);

    txtTableType.Text = CfgWord.TableType.ToString();
    txtTableElement.Text = CfgWord.TableElement.ToString();
}

As an example given a value of CfgWord value of 16422, the Table Table is 4 while the Table Element is 38.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: