Newbie, help with function that returns two strings 
Author Message
 Newbie, help with function that returns two strings

Hi, sorry if this seems very stupid, I'm trying to create a function that
returns two CStrings as result of the internal processing, I need to obtain
fName and tName.

CString CPag1::RealName(CString FieldName)
{
 int x=FieldName.Find('.',0);
    int nRightLength=FieldName.GetLength()-x-1;
    CString fName=FieldName.Right(nRightLength);
  int nLeftLength=FieldName.GetLength()-nRightLength-1;
  CString tName=FieldName.Left(nLeftLength);

 return fName;
 return tName;

Quote:
}

Thanks and best regards.


Fri, 31 Jan 2003 03:00:00 GMT  
 Newbie, help with function that returns two strings
Reyes:

No C/C++ function can return two values as you attempt. Pass a non-const
referece parameter to get extra returns. Eg

void GetTwoInts(int& i, int& j)
{
  i=2;
  j=3;
  return;

Quote:
}

The reference parameter is a C++ feature which is nicer to use than
traditional C pointers.

HTH,

David Wilkinson

Quote:

> Hi, sorry if this seems very stupid, I'm trying to create a function that
> returns two CStrings as result of the internal processing, I need to obtain
> fName and tName.

> CString CPag1::RealName(CString FieldName)
> {
>  int x=FieldName.Find('.',0);
>     int nRightLength=FieldName.GetLength()-x-1;
>     CString fName=FieldName.Right(nRightLength);
>   int nLeftLength=FieldName.GetLength()-nRightLength-1;
>   CString tName=FieldName.Left(nLeftLength);

>  return fName;
>  return tName;
> }

> Thanks and best regards.



Fri, 31 Jan 2003 03:00:00 GMT  
 Newbie, help with function that returns two strings
Hi,
your question is not really MFC related, better see comp.lang.c or
comp.lang.c++.
Anyway you should do something like :
void CPag1::RealName(CString FieldName, CString* pfName, CString* ptName)
{
    int x=FieldName.Find('.',0);
    int nRightLength=FieldName.GetLength()-x-1;
    (*pfName) =FieldName.Right(nRightLength);
    int nLeftLength=FieldName.GetLength()-nRightLength-1;
    (*tName)=FieldName.Left(nLeftLength);

Quote:
}

then call it by doing :
CString FieldName=...;
CString fName;CString tName;
MyPag1.RealName(FieldName,&fName,&tName);

    Hope this helps

    Fabrice



Quote:
> Hi, sorry if this seems very stupid, I'm trying to create a function that
> returns two CStrings as result of the internal processing, I need to
obtain
> fName and tName.

> CString CPag1::RealName(CString FieldName)
> {
>  int x=FieldName.Find('.',0);
>     int nRightLength=FieldName.GetLength()-x-1;
>     CString fName=FieldName.Right(nRightLength);
>   int nLeftLength=FieldName.GetLength()-nRightLength-1;
>   CString tName=FieldName.Left(nLeftLength);

>  return fName;
>  return tName;
> }

> Thanks and best regards.



Sat, 01 Feb 2003 06:03:47 GMT  
 Newbie, help with function that returns two strings

Quote:
> Hi, sorry if this seems very stupid, I'm trying to create a function
that
> returns two CStrings as result of the internal processing, I need to
obtain
> fName and tName.

> CString CPag1::RealName(CString FieldName)
> {
>  int x=FieldName.Find('.',0);
>     int nRightLength=FieldName.GetLength()-x-1;
>     CString fName=FieldName.Right(nRightLength);
>   int nLeftLength=FieldName.GetLength()-nRightLength-1;
>   CString tName=FieldName.Left(nLeftLength);

>  return fName;
>  return tName;
> }

> Thanks and best regards.

How about a function that returns an object made up of the 2 strings.

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 01 Feb 2003 10:59:55 GMT  
 Newbie, help with function that returns two strings
...In that case you could use a pair

std::pair< CString, CString > realName(CString& x);

Or...
How about a function that returns an array of two strings?

CString* realName(CString& x);

or

std::vector< CString > realName(CString x);

etc...

Rich

Quote:

> > Hi, sorry if this seems very stupid, I'm trying to create a function
> that
> > returns two CStrings as result of the internal processing, I need to
> obtain
> > fName and tName.

> > CString CPag1::RealName(CString FieldName)
> > {
> >  int x=FieldName.Find('.',0);
> >     int nRightLength=FieldName.GetLength()-x-1;
> >     CString fName=FieldName.Right(nRightLength);
> >   int nLeftLength=FieldName.GetLength()-nRightLength-1;
> >   CString tName=FieldName.Left(nLeftLength);

> >  return fName;
> >  return tName;
> > }

> > Thanks and best regards.

> How about a function that returns an object made up of the 2 strings.

> Sent via Deja.com http://www.deja.com/
> Before you buy.

--
http://members.tripod.co.uk/NASA13


Sat, 01 Feb 2003 12:50:00 GMT  
 Newbie, help with function that returns two strings
You could also create the two strings in the calling function and pass
references
to them with your function:

void CPag1::RealName(CString FieldName, CString &fName, CString &tName)
{
    int x=FieldName.Find('.',0);
    int nRightLength=FieldName.GetLength()-x-1;
    fName=FieldName.Right(nRightLength);
    int nLeftLength=FieldName.GetLength()-nRightLength-1;
    tName=FieldName.Left(nLeftLength);

Quote:
}

> > Hi, sorry if this seems very stupid, I'm trying to create a function
> that
> > returns two CStrings as result of the internal processing, I need to
> obtain
> > fName and tName.

> > CString CPag1::RealName(CString FieldName)
> > {
> >  int x=FieldName.Find('.',0);
> >     int nRightLength=FieldName.GetLength()-x-1;
> >     CString fName=FieldName.Right(nRightLength);
> >   int nLeftLength=FieldName.GetLength()-nRightLength-1;
> >   CString tName=FieldName.Left(nLeftLength);

> >  return fName;
> >  return tName;
> > }

> > Thanks and best regards.

> How about a function that returns an object made up of the 2 strings.

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Sat, 01 Feb 2003 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Newbie, help creating a function that returns two strings

2. passing character strings to functions with return string

3. newbie needs help to strings, strings, strings

4. newbie - two-dimensional field of strings with vectors

5. Newbie Q: return( string ) ?

6. newbie: returning strings (ATL7)

7. Newbie: DLL return string to VB?

8. Newbie: DLL return string? - [off-topic redirect]

9. Pointer to function returning pointer to function returning...

10. newbie question: returning structs from functions

11. Newbie: Want function to return boolean value

12. Functions that return arrays (newbie)

 

 
Powered by phpBB® Forum Software