Raghu
Raghu

Reputation: 190

CStringList in MFC

I was coding with 2 CStringList objects. Each has its own data, for eg one has name and other the phoneno, and both are in sync, i.e, if there is a phoneno there is a name and viceversa.

Now, i have 2 combobox in which i show the names and the respective phonenos. The name combobox is sorted, hence the sync between the two goes for a toss. hence 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 executed this i got the names in the myPhoneComboBox rather than the phonenos.

Now i have 2 qns:

  1. how come i get the name present in namelist when i am accessing the phonelist? isn't it a breach, as i am able to access some other variables data using some other variable.

  2. how to sort the 2nd list.

Upvotes: 0

Views: 4728

Answers (3)

baash05
baash05

Reputation: 4516

That is a crazzy, forgive me, stupid way to find names. It assumes the names are unique. God help me, I've had to deal with these things, name fields should never be viewed as unique, its bloody dangerious. Just ask my dad Baash05 Sr.

I'd assume there's an ID or some data set as the app adds to the combo box. Please use that in your map. My guess is the programmer set the data to either the ID of the name, or a pointer to the object that contained the name. (person object/business object/student object...).

If the code that adds the names didn't add a way to tell the difference between George Forman and any of his kids, then make an argument to the boss, that it's implementation should be changed, because by god it should be!

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

Upvotes: 0

Serge Wautier
Serge Wautier

Reputation: 21888

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

Upvotes: 0

lakshmanaraj
lakshmanaraj

Reputation: 4175

I Hope U are using CStringArray and not CStringList. You need to use FindIndex rather than Find since Find will return OBJECT Pos rather than the Index count.... and to get the element with array use simply [] the operator. If You still want to use CStringList then through Iterator Find the Index Count of the first match of string in one List and Use FindIndex of that IndexCount to get the postition object for the secondlist to use GetAt to the second list.

Upvotes: 1

Related Questions