Solved

returning an array of CString

Posted on 2000-04-11
5
335 Views
Last Modified: 2013-11-20
I'm still fairly new to MFC and would like to know how to declare a function that returns a CString array and actually create the function that returns the CString array.  How would I do that???
0
Comment
Question by:tblackwe
5 Comments
 

Expert Comment

by:kakamna
ID: 2704618
You have to use the pointer object of CString You can try

Decalre as
MyCLass{

CString * MyFunction()
CString *str;//where *str points to the //array of CString

}

CString * MYClsss::MyFunction()
{
return str;
}
0
 

Accepted Solution

by:
kakamna earned 50 total points
ID: 2704620
You have to use the pointer object of CString You can try

Decalre as
MyCLass{

CString * MyFunction()
CString *str;//where *str points to the //array of CString

}

CString * MYClsss::MyFunction()
{
return str;
}
0
 

Author Comment

by:tblackwe
ID: 2704678
So if I have a function:

CString* MyFunc()
{
CString [5] values;

values[0] = "one";
values[1] = "two";
values[2] = "three";
values[3] = "four";
values[4] = "five";

CString* rValues = values;
return rValues;
}

If the calling program says:

int main
{
CString* vals = MyFunc();

how do I now access the individual elements of the array?
0
 
LVL 1

Expert Comment

by:MT_MU
ID: 2704969
Just a comment.  In your above example the array values will be allocated on the stack - and will be discarded when you exit MyFunc();

To preserve it you would need to make it

static CString[5] values;

To access an indivual element in your main use

vals[index];
0
 
LVL 4

Expert Comment

by:piano_boxer
ID: 2706872
void GetStrings(CStringArray& rgs)
{
    rgs.Add("One");
    rgs.Add("Two");
    rgs.Add("Three");
}

void Test()
{
    CStringArray rgs;
    GetStrings(rgs);
    for(int i=0;i<rgs.GetSize();i++)
        TRACE("String %d = %s\n", i, rgs[i]);
}
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Complete beginner needs help making a cron job 9 116
Doc'in system (example?) BA 7 109
sumHeights2  challenge 7 103
mapAB Challlenge 35 144
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

776 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