Jump to content
The simFlight Network Forums

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.

 

Link to comment
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

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • 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.