2010-11-29 106 views
0

如何將CString轉換爲MFC中的枚舉類型(VC++)?在MFC(VC++)中將CString轉換爲枚舉類型?

我有一個方法,它將輸入參數作爲枚舉,但我傳遞Cstring值到它,我可以如何將其轉換爲枚舉。

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

我有一個方法

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

爲什麼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. 
}