getting the value from a Cstring Array

My Code:-

CString ItimeStringTable[] =
{
     25MS,  50MS,  500MS,
     200MS,     300MS, 400MS,
     500MS,     600MS, 700MS,
     800MS, 900MS,     1S,            
};    

CString ArrayElement = ItimeStringTable[1];
TRACE("Element value is %s\n",ArrayElement);

Debug Output :-

Element value is 

How do I get the value in variable "ArrayElement" to be 50MS?

Thanks
oliverUKAsked:
Who is Participating?
 
AlexFMConnect With a Mentor Commented:
CString ItimeStringTable[] =
{
    "25MS",  "50MS",  "500MS",
    "200MS", "300MS", "400MS",
    "500MS", "600MS", "700MS",
    "800MS", "900MS", "1S",            
};    
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Oliver,

Man, how did that compile?

Mayank.
0
 
SalteConnect With a Mentor Commented:
CString is a class and using aggregate for an array with class element doesn't really work very well. I believe the compiler will never allow it.

If you just want simple strings such as "50MS" etc I will suggest you just use a const char * table:

const char * timeStringTable[] = { ... };

In this case getting timeStringTable[1] will get you the string "50MS".

If you want to use CString and and array of them you can't do the above, you must do something like:

CString timeStringTable[...number of strings here...];

and then loop and insert the strings into the table. The best way to do that is to make the aforementioned const char * table so the code is something like this:


const char * timeStringTable_init[] = { .... };

CString timeStringTable[sizeof(timeStringTable_init)/sizeof(const char *)];

for (int i = sizeof(timeStringTable_init)/sizeof(const char *); --i >= 0; )
   timeStringTable[i] = timeStringTable_init[i];

After that loop is done to initialize your CString table you can use it, before that point you have to use the _init table and only use the const char * variants.

Alf
0
 
tinchosCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split points between AlexFM & Salte

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
All Courses

From novice to tech pro — start learning today.