tags:

views:

595

answers:

3

Hi,

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.

+1  A: 

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.

lakshmanaraj
didn't get u....i don't have the index( even if i have index from myNameComboBox, the stringlist and the combobox elements are not in sync as the combobox elements are sorted...)
Raghu
I am using CStringList not CList or CArray. :(
Raghu
CList.Find does not return a Number value (0,1,2,3,..) , It is an Object. Which Needs to be used for same List.CArray.Find also does the same. Instead CArray.FindIndex returns the Index (0,1,2,...).I hope now you understood.
lakshmanaraj
Then Use CStringArray instead of CStringList
lakshmanaraj
hmm, well the thing is if i change it to CStringArray, many other places will also get impacted.
Raghu
Then you need to write your own findmethod to return index of List and then use FindIndex(IndexNumber) to another List.
lakshmanaraj
hmm, i guess i will have to use map maybe...but then the major thing is how MFC allows one variable to access another variable's data.....
Raghu
It is only a Address Pointer , that's why!
lakshmanaraj
Hmm,ok. thanks for your help.
Raghu
A: 

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

Serge - appTranslator
the thing is the name list is already implemented. I am not allowed to change what is implemented... :(
Raghu
A: 

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] );
}
baash05
Dude, i mentioned names and phoneno's as an example, don't take them seriously. In our project we are using different stuff and they are unique. Please read the question carefully and understand what the questioner meant before answering....
Raghu