Jump to content
  • Advertisement
Sign in to follow this  
gellin

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

This topic is 3414 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
No. Const means just that, constant. It cannot be modified from the value it was assigned originally.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Quote:
Original post by gellin
Is there any way to do this?


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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!