digitalmars.D.learn - AA as lvalue
- John C (49/49) Mar 31 2005 Is there any reason why the following code shouldn't compile?
- Regan Heath (3/4) Mar 31 2005 I can't think of any.
- Ben Hinkle (17/23) Mar 31 2005 It might be easiest to change things around a little and use
- Ben Hinkle (1/3) Mar 31 2005 oops- that should be Color[NamedColors] ct = namedColorTable;
- John C (5/8) Mar 31 2005 So far as I can tell, most cases where you needed to add () to access a
- Ben Hinkle (3/5) Mar 31 2005 note you could also switch to using a dynamic array instead of an
- John C (4/10) Mar 31 2005 Exactly the conclusion I came to - I redesigned the class using a dynami...
Is there any reason why the following code shouldn't compile? public class ColorTable { public Color buttonBorder() { return namedColorTable[NamedColors.BUTTONBORDER]; // error here: this.namedColorTable() is not an lvalue } private enum NamedColors { BUTTONBORDER, BUTTONHIGHLIGHT // ... etc ... } private void initNormalColors() { namedColorTable_[NamedColors.BUTTONBORDER] = SystemColors.highlight; // ... etc ... } private Color[NamedColors] namedColorTable() { if (namedColorTable_ is null) initNormalColors(); return namedColorTable_; } private Color[NamedColors] namedColorTable_; } If I change the buttonBorder() property to directly use namedColorTable_ instead of calling through the namedColorTable() property (and do initNormalColors() in the constructor) it compiles. The reason why I haven't done it that way is because I want namedColorTable() to handle Windows visual styles, perhaps like this: private Color[NamedColors] namedColorTable() { if (namedColorTable_ is null) { if (VisualStyleInfo.enabled) { if (VisualStyleInfo.colorScheme == "NormalColor") initNormalColors(); else if (VisualStyleInfo.colorScheme == "HomeStead") initOliveColors(); else if (VisualStyleInfo.colorScheme == "Metallic") initSilverColors(); } else initSystemColors(); } return namedColorTable_; } And namedColorTable_ would get reset to null when the user changes their preferences, meaning the color schemes get reinitialised when namedColorTable() is next called. Granted, people don't change their desktop colors that often, but they do on occasion, and an application should probably sync up with those changes if they happen while it's running. Any ideas? John.
Mar 31 2005
On Thu, 31 Mar 2005 11:54:27 +0100, John C <johnch_atms hotmail.com> wrote:Is there any reason why the following code shouldn't compile?I can't think of any. Regan
Mar 31 2005
"John C" <johnch_atms hotmail.com> wrote in message news:d2gktk$mbk$1 digitaldaemon.com...Is there any reason why the following code shouldn't compile? public class ColorTable { public Color buttonBorder() { return namedColorTable[NamedColors.BUTTONBORDER]; // error here: this.namedColorTable() is not an lvalue }It might be easiest to change things around a little and use namedColorTable_ instead of the property namedColorTable and an lvalue ColorTable. Maybe this is a compiler bug with properties - I don't know. Sometimes I can't tell when the compiler needs () and when it doesn't. What I'm thinking: public Color buttonBorder() { ColorTable ct = namedColorTable;// needs ()? I haven'te tried it return ct[NamedColors.BUTTONBORDER]; // will insert if not present so it needs an lvalue } or public Color buttonBorder() { validateColorTable(); return namedColorTable_[NamedColors.BUTTONBORDER]; }
Mar 31 2005
public Color buttonBorder() { ColorTable ct = namedColorTable;// needs ()? I haven'te tried itoops- that should be Color[NamedColors] ct = namedColorTable;
Mar 31 2005
"Ben Hinkle" <ben.hinkle gmail.com> wrote in message news:d2grh8$t9f$1 digitaldaemon.com...So far as I can tell, most cases where you needed to add () to access a property have been eliminated.public Color buttonBorder() { ColorTable ct = namedColorTable;// needs ()? I haven'te tried itoops- that should be Color[NamedColors] ct = namedColorTable;Thanks, Ben. That did the trick.
Mar 31 2005
note you could also switch to using a dynamic array instead of an associative array and then namedColorTable could be used as an lvalue (plus it would be faster and simpler).oops- that should be Color[NamedColors] ct = namedColorTable;Thanks, Ben. That did the trick.
Mar 31 2005
"Ben Hinkle" <bhinkle mathworks.com> wrote in message news:d2h29r$14ss$1 digitaldaemon.com...Exactly the conclusion I came to - I redesigned the class using a dynamic array, since the key values for the AA were basically indexes anyway.note you could also switch to using a dynamic array instead of an associative array and then namedColorTable could be used as an lvalue (plus it would be faster and simpler).oops- that should be Color[NamedColors] ct = namedColorTable;Thanks, Ben. That did the trick.
Mar 31 2005