0
votes

how to Convert CString to Enum type in MFC(VC++) ?

I have one method which take input parameter as Enum but i am passing Cstring value to it how i can convert it to enum.

CString strFolderType = strFolderType.Right(strFolderType.GetLength()-(fPos+1));
m_strFolderType = strFolderType ;

I have one method as

ProtocolIdentifier(eFolderType iFolderType)

where enum eFolderType
{
    eFTP = 1,
    eCIFS,
    eBOTH
};

now when i am calling like this :

ProtocolIdentifier(m_strFolderType);   

It says cannot convert CString to eFolderType ...

How to resolve this ?

1

1 Answers

1
votes

Why is m_strFolderType a string? It seems like it should be an eFolderType.

There is no automatic way to convert a CString to an enum (which is really an integer). The values eFTP, eCIFS, and eBOTH are not strings and the compiler will not treat them as such.

Passing an integer as a string is ugly. You should pass an eFolderType as the argument. If you must pass a string (perhaps it came from some serialization that returned a string), you will have to do something like this:

eFolderType result = /* whichever should be the default*/ ;
if (m_strFolderType == _T("eFTP")) {
    result = eFTP;
} else if (m_strFolderType == _T("eCIFS")) {
    result = eCIFS;
} else if (m_strFolderType == _T("eBOTH")) {
    result = eBOTH;
} else {
    // Invalid value was passed: either use the default value or
    // treat this as an error, depending on your requirements.
}