This doesn't feel right

C

Claire

My main object has been designed for read/write in a property grid.
As I couldn't see a way to make it readonly when I needed to, I created a
2nd skeleton object that exposed the properties of the 1st one as read only.
There's a lot of properties in the objects, and hence a lot of
[Category(""),Description("")] attributes.

Not wanting to duplicate, I added another class containing static strings as
follows

public class strs
{
public static string IDNo = "Point ID number";
public static string ChannelType = "If a measurement point is AC or DC
coupled (as defined in the point's Hardware Settings field) then this field
has no effect";
public static string catTrainName = "Train heirarchy";
............
}

Then I changed my attributes to something similar to the following:
[Category(strs.catHardwareSettings),Description(strs.TriggerRange)]
public SettingsTuple.eTriggerRange TriggerRange
{
get{return Settings.TriggerRange;}
}

Compiler didn't complain at first, as I had to fix other bits of code. It
waited until I'd spent over an hour moving the 100 or so passages of text
(snarl). It complained that it needed const rather than static. So I did did
a search and replace changing static to const and recompiled.

I expected the software to fail when I assigned the SelectedObject property
at runtime but it didn't. What's really bugging me about this, is that the
compiler allowed it, the property grid works and is showing the field
attributes correctly, but I haven't created an instance of the strs class! I
used statics at first because I knew they were always available, but I
didn't expect that consts could be used similarly.
Can someone explain it to me please.

Claire
 
N

Nicholas Paldino [.NET/C# MVP]

Claire,

When you declare a field as const, it doesn't require an instance of the
class to be instantiated. Const is something like a #define, in the sense
that when you compile against it, the compiler will take the value defined
in the assembly at the time of compilation, and then use that. You never
have to create an instance of the strs class (which you should rename, BTW,
because it violates the naming conventions for publically exposed .NET
types).

Instead of creating hard coded wrappers, you should just implement the
ICustomTypeDescriptor interface on an object, and pass that. The property
grid should query the implementation for its type info, which you can
program to return read only property descriptors, as well as all the
attributes (without having to hard code all of it).

Hope this helps.
 
C

Claire

Thank you Nicholas and c# learner
I'll take a look at the ICustomTypeDescription.
(Damn clever these microsoft lot, seem to have thought of almost everything
with this .net stuff :blush:) It's just seems very difficult to find what you
need)

Claire
 
C

C# Learner

Claire said:
Damn clever these microsoft lot, seem to have thought of almost everything
with this .net stuff :blush:)

I got that impression, too. :)

You can really feel the difference with a well-thought-out and
well-designed language like C#, as opposed to another language which might
be, say, a cross-breed of two languages, with the end result of this being
that ideas aren't necessarily consistent throughout the whole language.

Note that I'm not having a dig at any such programming language.
 

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