?
Solved

getting the value from a Cstring Array

Posted on 2003-03-20
4
Medium Priority
?
213 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:oliverUK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 100 total points
ID: 8172382
CString ItimeStringTable[] =
{
    "25MS",  "50MS",  "500MS",
    "200MS", "300MS", "400MS",
    "500MS", "600MS", "700MS",
    "800MS", "900MS", "1S",            
};    
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8172440
Oliver,

Man, how did that compile?

Mayank.
0
 
LVL 12

Assisted Solution

by:Salte
Salte earned 100 total points
ID: 8173807
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
 
LVL 9

Expert Comment

by:tinchos
ID: 9510653
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

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question