【问题标题】:Convert CString to Enum type in MFC(VC++)?在 MFC(VC++) 中将 CString 转换为枚举类型?
【发布时间】:2011-05-17 04:05:48
【问题描述】:

如何在 MFC(VC++) 中将 CString 转换为 Enum 类型?

我有一种将输入参数作为 Enum 的方法,但我将 Cstring 值传递给它,我如何将其转换为枚举。

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

我有一种方法

ProtocolIdentifier(eFolderType iFolderType)

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

现在当我这样打电话时:

ProtocolIdentifier(m_strFolderType);   

它说不能将 CString 转换为 eFolderType ...

如何解决这个问题?

【问题讨论】:

    标签: visual-c++ mfc


    【解决方案1】:

    为什么m_strFolderType 是一个字符串?好像应该是eFolderType

    没有将CString 转换为enum(实际上是一个整数)的自动方法。值eFTPeCIFSeBOTH 不是字符串,编译器不会将它们视为字符串。

    将整数作为字符串传递是丑陋的。您应该传递 eFolderType 作为参数。如果你必须传递一个字符串(也许它来自一些返回字符串的序列化),你将不得不这样做:

    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.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-09
      • 2023-02-21
      • 2010-11-02
      • 2010-12-21
      • 1970-01-01
      • 2011-07-13
      • 2015-08-06
      • 2012-09-30
      相关资源
      最近更新 更多