Jump to content
The simFlight Network Forums
Jason Fayre

accessing value and valueChanged in a getter

Recommended Posts

Hi Paul,

I'm wondering if you have any tricks for accessing offset values in the getter of a property?

I'm putting all my offset definitions in their own class. I want to have a property for each offset that will allow me to get and set the altitude for example, with a simple read or assignment. The getter/setter would do the proper conversions to return a human readable value.

I think I figured out how to get the value of an offset from a getter using something like altitude.Value. The problem is that I also want access to the ValueChanged property. Can you thik of a way of accessing both Value and ValueChanged from the return of a public property getter?

Hope this makes some kind of sense.

 

Share this post


Link to post
Share on other sites

Hi Jason,

I understand what you want to do. It's not really possible with properties in .NET as properties use the same type for getting and setting.

You could change the property to return a custom type that includes both the Changed flag and the property Value, but you would also need to pass that type to set the value. This would work but it would mean creating an instance of an unnecessary type every time you wanted to set the value. It's extra typing and ugly code.

The only thing I can suggest is to use methods instead. You would create a 'set' and a 'get' method (like Java) for each piece of data. e.g. GetHeading() and SetHeading(). Not quite as readable as properties, but not too bad:

Instead of:
 

myFSUIPCData.Heading = 100;

this.txtHeading = myFSUIPCData.Heading.ToString("F2");

it would be:
 

myFSUIPCData.SetHeading(100);

this.txtHeading = myFSUIPCData.GetHeading().ToString("F2");

If you did that you can then have the Get method return a different type than the one accepted by the Set. You would define a struct that included the changed bool and the value and return that from the Get method. The struct would need to include a generic type for the value so you can define the type of the human-readable value (e.g. string, double, etc).

I can give you some example code for this if you want to go the methods route. Otherwise you'll need to make a separate 'changed' property for each of your values. (e.g. HeadingHasChanged).

Paul

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.