CStringList in MFC

I have coded 2 CStringList objects. Each of them has its own data, for example one has a name and the other phoneno and both are synchronized, that is, if there is phoneno, there is a name and vice versa.

Now I have 2 comboboxes in which I am showing the names and the corresponding phonenos. The name sorption is sorted, so the sync between the two goes for the cast. so for sorting I did the following:


int aComboElementNo = myNameComboBox.GetCount();
if( aComboElementNo >= 1 )
{
    for( int aIndex = 0; aIndex < aComboElementNo; aIndex++ )
    {
        CString aTempStr;
        // Getting the string in the combobox
        myNameComboBox.GetLBText( aIndex, aTempStr );
        // Getting the position where the name is present in the list
        POSITION aPos = aNameList.Find( aTempStr );
       // setting the appropriate phoneno in the 2nd combobox
        myPhoneComboBox.AddString( aPhoneList.GetAt( aPos ) );
    }
}

      


When I ran this, I got the names in myPhoneComboBox, not phonenos.

Now I have 2 qns:

  • How can I get the name present in the namelist when I contact a phonetician? this is not a violation as I may be able to access some other variable data using some other variable.

  • how to sort the second list.

0


source to share


3 answers


I Hope U is using CStringArray, not CStringList. You need to use FindIndex and not Find as Find will return OBJECT Pos and not index. and to get an element with an array use just the [] operator. If you still want to use a CStringList, then through an Iterator find the index of the index of the first match of the string in one list and use the FindIndex of that IndexCount to get the postition object for the second list to use GetAt on the second list.



+1


source


Why do you have 2 separate lists? Why not one CTypedPtrArray of structures containing both name and phone nb?



0


source


This is a crazy, excuse me, stupid way to find names. The names are assumed to be unique. God help me, I had to deal with these things, name fields should never be seen as unique, bloody, dangerous. Just ask my father Baash05 Sr.

I would assume the ID or some data is set when the app adds to the combo box. Use this on your map. I am assuming that the programmer has given the data to either a name identifier or a pointer to an object containing the name. (object object / business object / student object ...).

If the code that adds names hasn't added a way to tell the difference between George Foreman and any of his children, then make an argument to the boss that his implementation needs to be changed because god should be

int aComboElementNo = myNameComboBox.GetCount();
for( int aIndex = 0; aIndex < aComboElementNo; aIndex++ )
{
    int nameLocal = myNameComboBox.GetItemData( aIndex);
    myPhoneComboBox.InsertString(aIndex, aPhoneList[namelocal] );
}

      

0


source







All Articles