G
Guest
I've been writing C for 20+ years and am finally making the transition to C#.
I understand generally how struct and enum have changed. The data I want to
represent can be thought of as "rows" so in C I'd use an array of structs -
not an option in C#. I'd like to combine an enum to make the code more
readable. My data looks something like:
string strShortName
int intIndex
string strLongName
The values for strIndex are not sequential. I'd like to be able to build a
single "structure" that contains all of the data. In C I'd have done
something like:
enum { Small = 2, Medium = 5, Large = 15 };
struct myStruct {
char *strShortName;
int intIndex;
char *strLongName;
}
Then I'd have used something like:
struct myStruct mydata[] = {
{ "Small", Small, "Something Small" },
{ "Medium", Medium, "Something Medium" },
{ "Large", Large, "Something Large" },
....
{ NULL, 0, NULL }
};
I'd have finished with a couple of functions to return my values for
intIndex or strLongName when passed a strShortName. Of course, they'd have to
iterate through the array but it's not a problem since it's relatively small.
I know that achieving this functionality in an elegant way in C# must be
possible but lacking a language "reference" (no, the "Teach Yourself" books
don't get it for language details) I'm a bit frustrated.
Thanks!
I understand generally how struct and enum have changed. The data I want to
represent can be thought of as "rows" so in C I'd use an array of structs -
not an option in C#. I'd like to combine an enum to make the code more
readable. My data looks something like:
string strShortName
int intIndex
string strLongName
The values for strIndex are not sequential. I'd like to be able to build a
single "structure" that contains all of the data. In C I'd have done
something like:
enum { Small = 2, Medium = 5, Large = 15 };
struct myStruct {
char *strShortName;
int intIndex;
char *strLongName;
}
Then I'd have used something like:
struct myStruct mydata[] = {
{ "Small", Small, "Something Small" },
{ "Medium", Medium, "Something Medium" },
{ "Large", Large, "Something Large" },
....
{ NULL, 0, NULL }
};
I'd have finished with a couple of functions to return my values for
intIndex or strLongName when passed a strShortName. Of course, they'd have to
iterate through the array but it's not a problem since it's relatively small.
I know that achieving this functionality in an elegant way in C# must be
possible but lacking a language "reference" (no, the "Teach Yourself" books
don't get it for language details) I'm a bit frustrated.
Thanks!