[.net] Set a string property to a constant at design time by using the property grid

Started by
7 comments, last by ernow 14 years, 8 months ago
Say I have this const string declared somewhere: public const string SomeText = "blah blah blah"; In designer mode, I would like to assign the value of this string to a field in the property grid by using the variable SomeText. Is there any way to do this?
Advertisement
No. Const means just that, constant. It cannot be modified from the value it was assigned originally.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

I'm not trying to change the value of a const. I want to assign the "blah blah blah" to a field in the property grid just by typing in "SomeText" as the value (by using reflection or whatever means necessary).
Quote:Original post by gellin
Is there any way to do this?


No. Why would you want to do it like this anyway?!?

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

I have an xml file that is used to codegen some constants. I want to be able to assign a property on a control to this constant so I don't ever have to go into code and manually assign it myself.
Using reflection it's perfectly possible. It will probably look something like this (assuming 'foo' is the instance of the containing class):

Type fooType = foo.GetType();PropertyInfo desiredProperty = fooType.GetProperty("TheDesiredProperty");string constName = desiredProperty.GetValue(foo, BindingFlags.Public | BindingFlags.Instance).ToString();object desiredValue = fooType.GetField(constName, BindingFlags.Public | BindingFlags.Constant).GetValue(foo);desiredProperty.SetValue(foo, desiredValue);


Didn't test it though
Quote:Original post by Rynus_Rein
Using reflection it's perfectly possible.

That doesn't get it into the Properties window though or enable updating the field.

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

I suppose that you know that while in the properties window, your app is not running so you can't run any code, so I suppose reflexion won't help you there.
I think your best chance to "export" your string to the properties window would be to use an Attribute, but I've got no clues witch one would work.
Anyway, you'll have to recompile your code every time you change that value.

Where do you want to put that property? In the properties window of the code file that contains the "public const string SomeText = "blah blah blah";" or in the properties window of the xml file? Maybe it's some kind of content processor (custom tool or pre/post build event) that you want... you might want to look into those. I know XNA use content processors for it's assets, you can define you own processors and use the property grid to configure how your asset will be exported.

Good luck.
You could create a UI Type Editor if this is what you want.

This topic is closed to new replies.

Advertisement