Unable to Modify struct Members

Getters and setters -- how properties are accessed -- still function like methods in this regard. That is,

distances.Dist1.SetFeet(1000);

is "equivalent" to

distances.GetDist1().SetFeet(1000);

The "copy" of the structure (value) is made when it is returned from the getter (or passed to the setter). If Dist1 were a member variable this would not be the case and would work "as expected".

Happy coding.


struct are value types - so when you are accessing distances.Dist1.SetFeet you basically are accessing a copy... see for example at MSDN http://msdn.microsoft.com/en-us/library/aa288471%28v=vs.71%29.aspx

[EDIT after comment]
On the other hand, if you do distances.Dist1 = new Distance ().SetFeet (1000); AND change the return of SetFeet from void to Distance it should work. Alternatively make Distance a class.

For a reference on how to build structs in a way that they work as expected see the DateTime struct in the framework - http://msdn.microsoft.com/en-us/library/system.datetime.aspx
[/EDIT after comment]

Tags:

C#

Struct