Leaders snarfblam Posted April 30, 2005 Leaders Posted April 30, 2005 The following line of code is giving me a compiler error: _Level.StartPoint.X = 15 (Expression if a value and therefor can not be the target of an assignment.) _Level is a struct. StartPoint is a readonly property which returns a struct. X is a read/write property which is an integer. Why would this cause an error instead of assigning the value of 15 to the property .X? Note that the folloing code does work: Dim Start As Rom.MapCoord = _Level.StartPoint Start.X = 15 Quote [sIGPIC]e[/sIGPIC]
Leaders Iceplug Posted May 1, 2005 Leaders Posted May 1, 2005 Well, StartPoint is read only, and you are changing one of its values. Also, one of the more interesting problems encountered when you start working with structure properties: you cannot use them exactly the same as variables. When you say something like : Level.StartPoint.X = 15 I'm sure you want to say "Just set this structure's X property to 15". But what you really have to do in order for this work is to Read (Get) the structure, set its .X property to 15, and then Write (Set) it back... so, you are Reading and Writing with the structure. This does not work even if you have removed the ReadOnly / WriteOnly status and made it a regular property. One possible way is to make other properties which expose the submembers of the structure, but this requires a lot of work if you have many structure instances or many structure members. A workaround would be something like you mentioned in the code: Dim S As yourstruct = Level.StartPoint S.X = 15 'At this point, you haven't changed the value of Level.StartPoint Level.StartPoint = S 'Now, you have. But of course, you can't do that last step if StartPoint is readonly. However, if you use a Class (specifically, StartPoint's structure has become a class), then you can do what you are mentioning... since class communicate via references, not instances. Level.StartPoint.X = 15 So, when you make StartPoint readonly, you are keeping the reference readonly, but the data at the reference point is not readonly, so you can say Level.StartPoint.X = 15 and it will read the reference to StartPoint, and set its .X to 15, which will be reflected from now on in the StartPoint property. Always be careful when dealing with structures. :) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
Leaders snarfblam Posted May 1, 2005 Author Leaders Posted May 1, 2005 (edited) One possible way is to make other properties which expose the submembers of the structure, but this requires a lot of work if you have many structure instances or many structure members. A workaround would be something like you mentioned in the code: Dim S As yourstruct = Level.StartPoint S.X = 15 'At this point, you haven't changed the value of Level.StartPoint Level.StartPoint = S 'Now, you have. But of course, you can't do that last step if StartPoint is readonly. The inner working of the structures is a little more complicated than that of a normal struct. The properties do not directly modify private members of the struct, but rather an array which the struct references, as a means of exposing raw binary data in an object oriented structure. (This means that if I re-retrieve another struct using the same Level.StartPoint property, the change will be reflected, just as you would normally expect with a class.) Anyways... Level.StartPoint is an l-value (it can be a _set). But when I use Level.Startpoint.X, Level.StartPoint becomes an r-value (in this case it can only be a _get). I understand that much, and I understand that normally it does not make sense to modify a property on a normal struct with syntax like I used above; the changes would be made the the struct returned, and then discarded because the struct does not get stored anywhere. Regardless, I don't understand exactly why a property of an r-value struct can not be an l-value. It is quite possible for this property assignment to have an effect on objects other than the struct. It's just a small annoyance. Not a big deal, and I suppose that I should expect syntactical oddities like this when I am using structs in the manner that I am, but it doesn't seem too unreasonable (to me) to anticipate that a struct's property could act on a gc object. P.S. I suppose that the technical reason is that since you do not have the struct's data stored, it can't be passed to the member function or property you are calling; it exists only on the stack. Edited May 1, 2005 by snarfblam Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.