Solved

returning an array of CString

Posted on 2000-04-11
5
333 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now