Structures with Enum-like IntelliSense support

M

Michael Feld

Hello,

does anyone know how to produce a data type which offers Enum-like
IntelliSense in VS 2005?
What I am trying to create is a type which is very similar to an enum, i.e.
has a fixed set of values, but provides some more methods. Unfortunately,
you cannot derive from System.Enum to do this.

I noticed that if you type "Dim C as System.Drawing.Color = ", the editor
will display a list of colors, but this are actually ReadOnly Shared
Properties of the Color structure. So there must be a way to map structure
members to enum constants...

Any ideas how to do this?


Thanks in advance!

Michael
 
H

Herfried K. Wagner [MVP]

Michael,

Michael Feld said:
does anyone know how to produce a data type which offers Enum-like
IntelliSense in VS 2005?
[...]
I noticed that if you type "Dim C as System.Drawing.Color = ", the editor
will display a list of colors, but this are actually ReadOnly Shared
Properties of the Color structure. So there must be a way to map structure
members to enum constants...

There is no automatic way to do that.

Creating enumerations of items with a certain arbitrary data type
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=anytypeenums&lang=en>
 
M

Michael Feld

Hello Herfried,

thanks for the quick reply!
There is no automatic way to do that.

So is there any way at all to do this? Or is System.Color and others somehow
"hard-coded" into the VS2005 IntelliSense?
Creating enumerations of items with a certain arbitrary data type
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=anytypeenums&lang=en>

This is what I started with too. It works well, but the IDE support is
missing.

I also discovered System.ComponentModel.TypeConverter.GetStandardValues(),
but this worked only for the Windows Forms designer, not for the code
editor.


Thanks anyway,

Michael
 
H

Herfried K. Wagner [MVP]

Michael Feld said:
So is there any way at all to do this? Or is System.Color and others
somehow "hard-coded" into the VS2005 IntelliSense?

The .NET Framework contains a 'KnownColor' enumeration and color /objects/
which are exposed by the 'Color' structure. Are you talking about support
in the property grid?
 
J

Jay B. Harlow [MVP - Outlook]

Michael,
| Any ideas how to do this?
Yes! You already answered your own question:

| this are actually ReadOnly Shared
| Properties of the Color structure

You need to define your Structure, then you need to define Readonly Shared
Properties or Fields on that structure. Generally I use Readonly Shared
Fields as they are "easier" to define then properties...

Something like:

Public Structure Widget

Private ReadOnly m_value As Integer

Private Sub New(ByVal value As Integer)
m_value = value
End Sub

Public Shared ReadOnly Value1 As New Widget(1)
Public Shared ReadOnly Value2 As New Widget(2)
Public Shared ReadOnly Value3 As New Widget(3)
Public Shared ReadOnly Value4 As New Widget(4)

Public Sub Method1()

Public Function Method2() As Something

...

End Structure

I defined the constructor Private as there are a "fixed set of values" on
the Widget class, namely Value1, Value2, Value3, and Value4.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hello,
|
| does anyone know how to produce a data type which offers Enum-like
| IntelliSense in VS 2005?
| What I am trying to create is a type which is very similar to an enum,
i.e.
| has a fixed set of values, but provides some more methods. Unfortunately,
| you cannot derive from System.Enum to do this.
|
| I noticed that if you type "Dim C as System.Drawing.Color = ", the editor
| will display a list of colors, but this are actually ReadOnly Shared
| Properties of the Color structure. So there must be a way to map structure
| members to enum constants...
|
| Any ideas how to do this?
|
|
| Thanks in advance!
|
| Michael
|
|
 
M

Michael Feld

Hello Jay!

I tried your code, but I just don't get the IntelliSense to work. My
question was in fact all about this feature.

Maybe you can try this and tell me if I am doing something wrong:

If I type
Dim C as new Color =
in the editor, a list of possible values pops up.

But when I type
Dim W as new Widget =
(creating the class you posted before) nothing happens.

So there has to be either a major difference between the implementation of
"Color" and "Widget", or - as I said earlier - IntelliSense is hard-coded
into the IDE for "Color" (which would somehow surprise me looking at how
many options you can find in System.ComponentModel).


Thanks,
Michael
 
M

Michael Feld

Herfried K. Wagner said:
The .NET Framework contains a 'KnownColor' enumeration and color /objects/
which are exposed by the 'Color' structure. Are you talking about support
in the property grid?

I am not talking about making the custom enum work programatically. This
point is clear. It's also not about the property grid (I got this to work in
an example, anyway). But if you are working in the code editor and you want
to specify a value for an object of the type of this "custom enumeration"
(the structure!), then you would want to have a list of values pop up as
soon as you come to a place where such a value has to be specified.

Another example:
Create a class with

Public Sub Foo(ByVal Bar As Color)
End Sub

Now from any valid place in code, try to call Foo and type

Foo(

and you will get a list of shared read-only properties (!) of "Color" from
which you can select, as if it was an enumeration.
If I try the same with my own structure, I do not get this list (unless I
type "<Classname>.", but this includes methods and other properties too).

So, is it now more clear what I am trying to accomplish?


Michael
 
A

Armin Zingler

Michael Feld said:
Hello Jay!

I tried your code, but I just don't get the IntelliSense to work. My
question was in fact all about this feature.

Maybe you can try this and tell me if I am doing something wrong:

If I type
Dim C as new Color =
in the editor, a list of possible values pops up.

But when I type
Dim W as new Widget =
(creating the class you posted before) nothing happens.

So there has to be either a major difference between the
implementation of "Color" and "Widget", or - as I said earlier -
IntelliSense is hard-coded into the IDE for "Color" (which would
somehow surprise me looking at how many options you can find in
System.ComponentModel).


I think I know what you mean, but in my IDE, there is no item listed after
typing "Dim C as new color =". In addition, 'New' doesn't make sense in this
case because the new object would be overwritten by the object after "="
anyway. Apart from this, if you write "Dim c as color =", no combobox pops
up. It would only popup if you write "dim c as color = color.", and how to
implement this in your own class, Jay has already shown you.

Armin
 
A

Armin Zingler

Armin Zingler said:
I think I know what you mean, but in my IDE, there is no item listed
after typing "Dim C as new color =". In addition, 'New' doesn't make
sense in this case because the new object would be overwritten by
the object after "=" anyway. Apart from this, if you write "Dim c as
color =", no combobox pops up. It would only popup if you write "dim
c as color = color.", and how to implement this in your own class,
Jay has already shown you.

Sorry, I didn't read you are using VB 2005. I was referring to VB 2003. (I
didn't know and read there is a difference) Yes, in VB 2005, i get the popup
combo but I cannot answer why, yet.


Armin
 
J

Jay B. Harlow [MVP - Outlook]

Michael,
I have not tried it, I suspect its the ColorConverter that is attached to
the Color structure via the TypeConverterAttribute. Of course it may be one
of the other custom attributes attached to the Color structure, such as the
EditorAttribute...

I would expect that the ColorConverter.GetStandardValues is the method
returning the values...

However I have only used type converters in relation to the Property Grid in
..NET 1.1, I don't know specifically if their use has been expanded to the
IDE in VS 2005 (.NET 2.0) it would be cool if they were.

If I have time later today, I will play with creating a converter...
 
M

Michael Feld

Hello again,

did you find anything out about the ColorConverter?

I tried to create such a converter for my class too (by looking at the
disassembly for ColorConverter), but it still doesn't have the same
IntelliSense as Color.
I also first thought about this attribute, but the fact the IntelliSense
tooltip reads "Public Shared Property ..." puzzles me... because the
TypeConvertes does not seem to provide this information.
And the ColorEditor appears even more unlikely to me.
After all, the same IntelliSense works for System.Drawing.Brush, and this
class has no attributes at all.

Seems there is some kind of magic going on in VS...


Regards,
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top