Click here to Skip to main content
15,884,975 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi,

I'm trying to figure out how do I display or set the format of my enum to hex on the Editor Tooltip or Intellisense? I'm building a reusable class and I want the hex numbers to show up, instead of the decimal, when referencing the enum within the VB.NET editor.

Here is one of my enums:
VB
Public Enum OutputPort As Byte
    PortA = &H0&
    PortB = &H1&
    PortC = &H2&
    AllPorts = &HFF&
End Enum


For instance, when I type the following in VB.NET OutputPort.a

An autocomplete box comes up, if you hover over the "AllPorts" enum entry, a white info box (Editor tooltip or intellisense) appears that says "OutputPort.AllPorts = 255". Is there a way to get it to display "OutputPort.AllPorts = &HFF&" or equivalent so that the developer gets the hex code instead?

This is just one example of many byte enums that have to be formatted in hex. I'm hoping for some sort of .NET formatting property, instead of commenting each enumerated value.

I want to remember that I have seen this before somewhere, but I'm a bit rusty on the correct terminology, so I have been unable to find useful results in Google.

Thanks,

Marco

[The article providing comprehensive answer to the questions on this page is published: Bitwise Enumeration Editor for PropertyGrid and Visual Studio[^] — SA]
Posted
Updated 19-Aug-14 19:16pm
v5
Comments
Sergey Alexandrovich Kryukov 20-Aug-14 1:20am    
The article I promised is published: Bitwise Enumeration Editor for PropertyGrid and Visual Studio. It should answer nearly all questions we discussed on this page, in addition the the other answers and articles I references.

Besides, I decided to up-vote this question post (not even for the question, but for all the discussion) with my 5. I also credited you in the section "Gredits" of my article for the raising some question which gave me some ideas and inspired doing this work. Thank you!

I hope you will find this work interesting and useful.

—SA

As far as I know you cannot do that in Visual Studio IDE (maybe I am wrong).

Simple solution: make the developers learning hexadecimal numbers, after all it is a useful piece of knowledge.

A workaround: use the string representation of the hexadecimal numbers, and convert them to their byte values before actually sending it to the device.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Aug-14 4:30am    
I agree and expressed similar opinion. But, later on, I came to a good alternative. I implemented a bitwise enumeration editor which can be used in EditorAttribute of any enumeration type. This is the mechanism which allows any PropertyGrid to show this editor. Including the PropertyGrid used in Visual Studio. And, in the editor, two strings are shown: enumeration member (with an alternative to get human-readable representation from resource) and ToolTip of the node shows a long "description" string defined in resource.

I just completed the implementation: such enumeration types, augmented with appropriate attributes, really show those extra strings in Visual Studio. Pretty interesting. Please see my comments to Solution 1 at the end of comment tree.

—SA
bigbro_1985 wrote:
But I think you just answered another problem that I had, or gave me an idea rather. I actually wanted a bitwise enumeration to assign Booleans, right now I made a bitwise structure that converts the 8 bit Boolean value to byte. But I don't like what I did, it doesn't look clean to me. This is what I like about embedded development because you get all your bitwise operators.
Oh, this is another problem I solved a while ago. The solution is really comprehensive. Please see my first article from my short "enumeration" series: Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^].

This article solves a set of different related problems at once, first of all, the problem is using foreach for enumerations (it's a shame that it is not done in .NET by default), and a pretty tricky problems if members with identical underlying integer values.

And one of the problems is the "bitwise" operations. My solution is two-fold: first I explain how to work "manually", in the section "2 Background: Life before Enumeration Class", subsection: "2.4 Iterating Bit Sets". And later on, I explain the advances brought by my generic class "Enumeration". So, you will have a choice and may decide to use the simple technique, if it's enough for you.

[EDIT]

It's possible that you cannot figure out how to read/assign bits as Boolean. Let's do it: you read my article, and here I'll explain the rest, about Boolean. The idea is to use indexed property:
C#
public enum OptionSet {
    None = 0,
    AlignX = 1 << 0,
    AlignY = 1 << 1, // this is better than 1, 2, 4, 8...
    Transparent = 1 << 2,
    Border = 1 << 3,       
} //enum OptionSet

public class SomeContainerWithEnumerationProperty {

    OptionSet optionSet; // data is here

    public bool this[OptionSet index] {
        get {
            // read bit depending on index
            // and return (bit is set) boolean value
        }
        set {
            // set the bit,
            // depending on index and boolean value
        }
    }

} //class SomeContainerWithEnumerationProperty

//... usage:

SomeContainerWithEnumerationProperty myOptions =
   new SomeContainerWithEnumerationProperty();

myOptions[OptionSet.Border] = true; // set one bit
myOptions[OptionSet.AlignX | OptionSet.AlignX] = true; // set 2 bits at once

//...

myOptions[OptionSet.AlignX] = false; // clear one bit


I hope you got it. Some a bit tricky problem is: in this syntax, you can have only one such property. Can you have more than one? In principle, if property type is always bool, but the types if index different (different enum types or other different types), the compiler can resolve which one to use, but how to write it?

The idea is: put these properties in one or more separate interfaces and make your class implement all these interfaces, with explicit interface implementation syntax. So, one property is "this" of the class itself, as shown above, plus one or more coming from the implementation of interfaces.

—SA
 
Share this answer
 
v4
Comments
[no name] 17-Aug-14 4:43am    
That's really awesome! I honestly didn't know you can do that... This will come in handy in a project I'm working on now and I'm sure in the future!

Thanks SA!
Sergey Alexandrovich Kryukov 17-Aug-14 4:46am    
This is such a pleasure to help someone who captures the idea at once!
You are very welcome.
—SA

P.S.: please see the update to the code: simplified with added usage.
Not a problem at all. You take enumeration underlined integer value by taking a value of a enumeration variable and casting it to byte (in your case). And them you format this byte as hexadecimal, using byte.ToString(string format) with format string "X", or "x" or, say, "X2":
http://msdn.microsoft.com/en-us/library/y11056e9%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
Comments
[no name] 14-Aug-14 2:49am    
Hi SA,

Yes, I understand casting the variable to string and formatting it to be hex. I have used byte.ToString("X2") before and I have gotten that to work just fine.

What I would like is, when I make a reference to the enum somewhere else in my code or in a new project and I hover over "OutputPort.AllPorts" I don't want the editor tooltip text to display "OutputPort.AllPorts = 255", I would like it to display "OutputPort.AllPorts = &HFF&".

Because I'm building a serial communication class and the device I'm communicating accepts commands in HEX, it would match up with manuals and aid debugging.

Thanks,
Sergey Alexandrovich Kryukov 14-Aug-14 2:53am    
What project? Do you mean behavior in IDE, Visual Studio? If so, who cares what it shows? It's not your IDE anyway...
—SA
[no name] 14-Aug-14 3:47am    
LOL, yes. Visual Studio, in the VB.NET editor. I know its not my IDE, but this class is intended for other people using this specific device, which I'm posting up on code project in the next couple of weeks. As soon as I'm done testing/documenting/tidying up.

I would like it if the end developers passed a Hex command to the device from my class in VB.Net or C# (while using the command enum), they can see the HEX value as a pose to the decimal value so that you don't need to go pull out a calculator or do long division to verify the correct command is being sent. See these hex commands are listed in the device's Bluetooth communication manual in HEX and not decimal. I'm sure everyone else will care who uses this because it would be thorough documentation.

I know you can place the following code to add information to the editor tooltip text.

''' <summary>
''' Description goes here
'''
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>


Right now I have tested this, it works, but its a pain, and will be a major pain to do this to each and every enumerated value. And then it still displays the value in the bottom in decimal.

''' <summary>
''' Specifies the Motor Regulation Mode Command values in HEX
''' </summary>
''' <remarks></remarks>
Public Enum MotorRegulationMode As Byte
''' <summary>Disabled = &H0&. Regulation Will Be Disbled: Use this value if the Motor mode value is 0x00 (Coast mode), 0x01 (Motor ON mode) or 0x03 (Motor ON with Break mode).</summary>
Disabled = &H0&

''' <summary>SpeedRegulationMode = &H1&. Speed Regulation: Use this regulation mode if the Motor mode value is 0x05 (Motor ON and Regulated mode) or 0x07 (Motor ON with Break and Regulated mode), and you would like the NXT firmware automatically adjust the speed to the specified motor power set point value no matter the physical overload on the motor.</summary>
SpeedRegulationMode = &H1&

''' <summary>SynchronizationMode = &H2&. Two Motors Synchronization Regulation: Use this regulation mode if the Motor mode value is 0x05 (Motor ON and Regulated mode) or 0x07 (Motor ON with Break and Regulated mode), and you would like the NXT firmware automatically keep the two motors in sync regardless of varying physical loads.</summary>
SynchronizationMode = &H2&
End Enum



What do I do to modify the enum's value displayed on the editor tooltip from decimal to hex? Is it even possible?

I don't know if I'm making sense, sorry I don't know all the correct terminology of what I'm trying to achieve.
Sergey Alexandrovich Kryukov 14-Aug-14 6:49am    
First, I would not solve this problem at all; second, it looks like you are confused fundamentally. The key is "modify the enum's value displayed". It's not "modify". It sounds like 255 should be modified into &HFF&. But it's not "modified". This is the same value. I guess you understand it, it could be just bad wording. It's format should be "modified". But format is not a part of enumeration. This is a part of Visual Studio. Besides, why not "0xFF"? Visual Studio works the same way will all .NET languages.

By the way, you can modify the way the value is edited in PropertyGrid. For this purpose, you need to supply a different editor and prescribe this editor in a type attribute for your enumeration type. And yes, it should be shown in IDE then. This is possible, but even this would be a bad thing. Come on, what is shown in Visual Studio is shown for developers. Do you really think that anyone won't see that &HFF& is 255? From the other hand, isn't that natural to have the same treatment for all enumeration types? Besides, people should work with enumeration member names, not values. Look at all MSDN documentation.

In other words, what you want is unreasonable. I don't think you can modify the tooltip in the IDE.

—SA
CPallini 14-Aug-14 5:29am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900