Converting Integer to POSITION?
-
I hv Integer Value which i need to convert as POSITION to assign starting POSITION for the collection class. CTypedPtrMap m_Contacts; POSITION pos; CString strKey; CContact* pContact; // Collection class which reads information from local file int iIndex = m_screenName.GetCurSel(); // gets cursor position from combo Now i need to assign the cursor position as POSITION for CTypedPtrMap. m_Contacts.GetNextAssoc(pos, strKey, pContact); Please inform me how to solve this?
-
I hv Integer Value which i need to convert as POSITION to assign starting POSITION for the collection class. CTypedPtrMap m_Contacts; POSITION pos; CString strKey; CContact* pContact; // Collection class which reads information from local file int iIndex = m_screenName.GetCurSel(); // gets cursor position from combo Now i need to assign the cursor position as POSITION for CTypedPtrMap. m_Contacts.GetNextAssoc(pos, strKey, pContact); Please inform me how to solve this?
Is the screen name the key into the contacts map? If it is, you can get that using
m_screenName.GetLBText
. If not, you could associate the key with the item in the combo usingm_screenName.SetItemData
. I assume you have a good reason for using a Map, otherwise I would suggest using a List or an Array. MFC's map classes are based on a hash-table, which means that you won't get elements out of them in the order you inserted them, or in any predictable order, so you can't just callGetStartPosition
then callGetNextAssoc
repeatedly. Stability. What an interesting concept. -- Chris Maunder -
I hv Integer Value which i need to convert as POSITION to assign starting POSITION for the collection class. CTypedPtrMap m_Contacts; POSITION pos; CString strKey; CContact* pContact; // Collection class which reads information from local file int iIndex = m_screenName.GetCurSel(); // gets cursor position from combo Now i need to assign the cursor position as POSITION for CTypedPtrMap. m_Contacts.GetNextAssoc(pos, strKey, pContact); Please inform me how to solve this?
A POSITION is like an iterator for a collection - its not synomous with an integer. The CTypedPtrMap will be keyed by what ever you templateized it on - looking at this a string. So you can find your CContact based on a CSring easily, which is the key into the map. How did you insert into the map? Did you insert all the keys into m_screenName, then insert each CContact into the map based on the key? If so, you can do a GetText on the combo to get the text for item iIndex, then use that look up in the map. CString str; str = m_screenName.GetText(iIndex); CContact* p = NULL; BOOL b = m_Contacts.Lookup(str, p); etc If not, the only thing u can do is to advance around the map until u hit the position at iIndex: POSTION pos = m_Contacts.GetStartPosition(); int nElement = 0; while(pos != NULL && nElement++ <= iIndex) { pos = m_Contacts.GetNextAssoc(pos, strKey, pContact); } You get the idea - iterate around the map until you get to element iIndex.