digitalmars.D - How to initialise a struct??
- Matthew (27/27) Mar 07 2005 struct EnumString
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (3/13) Mar 07 2005 --anders
- Matthew (3/16) Mar 07 2005 *MANY* thanks!
- Stewart Gordon (15/24) Mar 07 2005 Added to
- Derek Parnell (53/80) Mar 07 2005 Is this what you are trying to do???
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (21/22) Mar 07 2005 [linear search snipped]
- Derek Parnell (69/98) Mar 07 2005 Well maybe, but it looks like this enum is designed to be combined with
struct EnumString { int value; char* str; }; /* ///////////////////////////////////////////////////////////////////////////// * Enumerations */ /** Flags that moderate the creation of Databases */ public enum ORJ_FLAG { ORDER_FIELDS = 0x0001 /*!< Arranges the fields in alphabetical order */ , ELIDE_BLANK_RECORDS = 0x0002 /*!< Causes blank records to be ignored */ } public char[] toString(ORJ_FLAG f) { const EnumString strings[2] = { { ORDER_FIELDS, "Arranges the fields in alphabetical order" } , { ELIDE_BLANK_RECORDS, "Causes blank records to be ignored" } }; return ""; } The compiler informs me: openrj.d(83): Error: a struct is not a valid initializer for a EnumString [2] All help gratefully received. :-)
Mar 07 2005
Matthew wrote:The compiler informs me: openrj.d(83): Error: a struct is not a valid initializer for a EnumString [2] All help gratefully received.You need to use [] for arrays in D. You also need to name the enums:const EnumString strings[2] = [ { ORJ_FLAG.ORDER_FIELDS, "Arranges the fields in alphabetical order" } , { ORJ_FLAG.ELIDE_BLANK_RECORDS, "Causes blank records to be ignored" } ];--anders
Mar 07 2005
*MANY* thanks! :-) "Anders F Björklund" <afb algonet.se> wrote in message news:d0hash$23hk$2 digitaldaemon.com...Matthew wrote:The compiler informs me: openrj.d(83): Error: a struct is not a valid initializer for a EnumString [2] All help gratefully received.You need to use [] for arrays in D. You also need to name the enums:const EnumString strings[2] = [ { ORJ_FLAG.ORDER_FIELDS, "Arranges the fields in alphabetical order" } , { ORJ_FLAG.ELIDE_BLANK_RECORDS, "Causes blank records to be ignored" } ];--anders
Mar 07 2005
Anders F Björklund wrote: <snip>You need to use [] for arrays in D. You also need to name the enums:Added to http://www.wikiservice.at/wiki4d/wiki.cgi?ErrorMessages Moreover, why have the struct at all? Why not save a bit of code with this: const char[][OBJ_FLAG.max + 1] strings = [ ORJ_FLAG.ORDER_FIELDS: "Arranges the fields in alphabetical order", ORJ_FLAG.ELIDE_BLANK_RECORDS: "Causes blank records to be ignored" ]; Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.const EnumString strings[2] = [ { ORJ_FLAG.ORDER_FIELDS, "Arranges the fields in alphabetical order" } , { ORJ_FLAG.ELIDE_BLANK_RECORDS, "Causes blank records to be ignored" } ];
Mar 07 2005
On Mon, 7 Mar 2005 21:33:39 +1100, Matthew wrote:struct EnumString { int value; char* str; }; /* ///////////////////////////////////////////////////////////////////////////// * Enumerations */ /** Flags that moderate the creation of Databases */ public enum ORJ_FLAG { ORDER_FIELDS = 0x0001 /*!< Arranges the fields in alphabetical order */ , ELIDE_BLANK_RECORDS = 0x0002 /*!< Causes blank records to be ignored */ } public char[] toString(ORJ_FLAG f) { const EnumString strings[2] = { { ORDER_FIELDS, "Arranges the fields in alphabetical order" } , { ELIDE_BLANK_RECORDS, "Causes blank records to be ignored" } }; return ""; }Is this what you are trying to do??? <code> import std.stdio; /** Flags that moderate the creation of Databases */ public enum ORJ_FLAG { ORDER_FIELDS = 0x0001 /*!< Arranges the fields in alphabetical order */ , ELIDE_BLANK_RECORDS = 0x0002 /*!< Causes blank records to be ignored */ } struct EnumString { ORJ_FLAG value; char[] str; }; /* ///////////////////////////////////////////////////////////////////////////// * Enumerations */ public char[] toString(ORJ_FLAG f) { const EnumString[2] strings = [ { ORJ_FLAG.ORDER_FIELDS, "Arranges the fields in alphabetical order"}, { ORJ_FLAG.ELIDE_BLANK_RECORDS, "Causes blank records to be ignored" } ]; int i; for (i = 0; i < strings.length; i++) { if (strings[i].value == f) return strings[i].str; } return ""; } void main() { ORJ_FLAG x; x = ORJ_FLAG.ORDER_FIELDS; writefln(toString(x)); x = ORJ_FLAG.ELIDE_BLANK_RECORDS; writefln(toString(x)); } <endcode> -- Derek Parnell Melbourne, Australia 7/03/2005 10:12:17 PM
Mar 07 2005
Derek Parnell wrote:Is this what you are trying to do???[linear search snipped] No, probably this: ;-) private char[][ORJ_FLAG] orj_strings; static this() { orj_strings[ORJ_FLAG.ORDER_FIELDS] = "Arranges the fields in alphabetical order"; orj_strings[ORJ_FLAG.ELIDE_BLANK_RECORDS] = "Causes blank records to be ignored"; } public char[] toString(ORJ_FLAG f) { return (f in orj_strings) ? orj_strings[f] : ""; } void main() { writefln("%s", toString(ORJ_FLAG.ORDER_FIELDS)); writefln("%s", toString(ORJ_FLAG.ELIDE_BLANK_RECORDS)); } --anders
Mar 07 2005
On Mon, 07 Mar 2005 12:20:28 +0100, Anders F Björklund wrote:Derek Parnell wrote:Well maybe, but it looks like this enum is designed to be combined with other enums to form a set of flags. For example, how could you do this ... writefln("%s", toString( ORJ_FLAG.ELIDE_BLANK_RECORDS | ORJ_FLAG.ORDER_FIELDS); So maybe this is more like it... import std.stdio; /** Flags that moderate the creation of Databases */ public enum ORJ_FLAG { ORDER_FIELDS = 0x0001 /*!< Arranges the fields in alphabetical order */ , ELIDE_BLANK_RECORDS = 0x0002 /*!< Causes blank records to be ignored */ } struct EnumString { ORJ_FLAG value; char[] str; }; /* ///////////////////////////////////////////////////////////////////////////// * Enumerations */ public char[] toString(ORJ_FLAG f) { const EnumString[] strings = [ { ORJ_FLAG.ORDER_FIELDS, "Arranges the fields in alphabetical order"}, { ORJ_FLAG.ELIDE_BLANK_RECORDS, "Causes blank records to be ignored" } ]; char[] result; uint mask; foreach (EnumString x; strings) { mask = 1; while(mask) { if (x.value == (mask & f)) { if (result.length !=0) result ~= ','; result ~= x.str; } mask <<= 1; } } return result; } void main() { ORJ_FLAG x; x = ORJ_FLAG.ORDER_FIELDS; writefln(toString(x)); x = ORJ_FLAG.ELIDE_BLANK_RECORDS; writefln(toString(x)); x = ORJ_FLAG.ELIDE_BLANK_RECORDS | ORJ_FLAG.ORDER_FIELDS; writefln(toString(x)); } -- Derek Parnell Melbourne, Australia 7/03/2005 10:37:12 PMIs this what you are trying to do???[linear search snipped] No, probably this: ;-) private char[][ORJ_FLAG] orj_strings; static this() { orj_strings[ORJ_FLAG.ORDER_FIELDS] = "Arranges the fields in alphabetical order"; orj_strings[ORJ_FLAG.ELIDE_BLANK_RECORDS] = "Causes blank records to be ignored"; } public char[] toString(ORJ_FLAG f) { return (f in orj_strings) ? orj_strings[f] : ""; } void main() { writefln("%s", toString(ORJ_FLAG.ORDER_FIELDS)); writefln("%s", toString(ORJ_FLAG.ELIDE_BLANK_RECORDS)); } --anders
Mar 07 2005