Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. a question regarding CStringList

a question regarding CStringList

Scheduled Pinned Locked Moved C / C++ / MFC
question
19 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    namaskaaram
    wrote on last edited by
    #1

    i have a CStringList instance and i dynamically create CString objects and add them to the CStringList Object , now here is my question: when i want to destroy the CStringList object i first have to 'delete' all the CString objects thati have inserted dynamically!for this i need to get the pointer to the CString....but using the RemoveTail() of the CStringList object i am able to get back only the LPCTSTR of the CString objects ,SO HOW do i GET THE CSTRING object pointer from the CStringList to delete the dynamically created CString objects?????:confused::confused::confused::confused: -- modified at 2:34 Tuesday 14th February, 2006

    S T C 3 Replies Last reply
    0
    • N namaskaaram

      i have a CStringList instance and i dynamically create CString objects and add them to the CStringList Object , now here is my question: when i want to destroy the CStringList object i first have to 'delete' all the CString objects thati have inserted dynamically!for this i need to get the pointer to the CString....but using the RemoveTail() of the CStringList object i am able to get back only the LPCTSTR of the CString objects ,SO HOW do i GET THE CSTRING object pointer from the CStringList to delete the dynamically created CString objects?????:confused::confused::confused::confused: -- modified at 2:34 Tuesday 14th February, 2006

      S Offline
      S Offline
      Subramaniam s V
      wrote on last edited by
      #2

      If your aim is to delete elements in the CStringList one by one, why not you 'find' that particular element and then use 'RemoveAt' function?

      N 1 Reply Last reply
      0
      • S Subramaniam s V

        If your aim is to delete elements in the CStringList one by one, why not you 'find' that particular element and then use 'RemoveAt' function?

        N Offline
        N Offline
        namaskaaram
        wrote on last edited by
        #3

        :((that too returns LPCTSTR not CString*! -- modified at 3:01 Tuesday 14th February, 2006

        S 1 Reply Last reply
        0
        • N namaskaaram

          :((that too returns LPCTSTR not CString*! -- modified at 3:01 Tuesday 14th February, 2006

          S Offline
          S Offline
          Subramaniam s V
          wrote on last edited by
          #4

          Same thing????? I am not sure if I am right. Correct me if I am wrong. Just a suggestion from my side. See, 'Find' method returns the position of a particular string value passed to it. Once you get the position you can use the same in 'RemoveAt' functions which returns a void. Willn't this help you?

          N 1 Reply Last reply
          0
          • S Subramaniam s V

            Same thing????? I am not sure if I am right. Correct me if I am wrong. Just a suggestion from my side. See, 'Find' method returns the position of a particular string value passed to it. Once you get the position you can use the same in 'RemoveAt' functions which returns a void. Willn't this help you?

            N Offline
            N Offline
            namaskaaram
            wrote on last edited by
            #5

            yes,but this returns teh LPCTSTR value not the pointer to the CString! -- modified at 3:18 Tuesday 14th February, 2006

            S 1 Reply Last reply
            0
            • N namaskaaram

              yes,but this returns teh LPCTSTR value not the pointer to the CString! -- modified at 3:18 Tuesday 14th February, 2006

              S Offline
              S Offline
              Subramaniam s V
              wrote on last edited by
              #6

              Sorry !!! May I know which return value(LPCTSTR) are you talking about. Because none of the functions that I mentioned 'Find' and 'RemoveAt' returns LPCTSTR. So I am confused as what return value you are talking about.

              N 1 Reply Last reply
              0
              • N namaskaaram

                i have a CStringList instance and i dynamically create CString objects and add them to the CStringList Object , now here is my question: when i want to destroy the CStringList object i first have to 'delete' all the CString objects thati have inserted dynamically!for this i need to get the pointer to the CString....but using the RemoveTail() of the CStringList object i am able to get back only the LPCTSTR of the CString objects ,SO HOW do i GET THE CSTRING object pointer from the CStringList to delete the dynamically created CString objects?????:confused::confused::confused::confused: -- modified at 2:34 Tuesday 14th February, 2006

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #7

                why don't you just do this ? (looking at the MSDN[^]...)

                CStringList myList = /*...*/;

                //...

                CObject* pObj = NULL;
                for (int i = 0; i < this->GetCount(); i++) {
                //because GetAt() returns a CObject*&
                pObj = myList.GetAt(i);
                myList.RemoveAt(i);
                delete pObj;
                }


                TOXCCT >>> GEII power
                [toxcct][VisualCalc 2.20][VCalc 3.0 soon...] -- modified at 3:39 Tuesday 14th February, 2006

                C N 2 Replies Last reply
                0
                • N namaskaaram

                  i have a CStringList instance and i dynamically create CString objects and add them to the CStringList Object , now here is my question: when i want to destroy the CStringList object i first have to 'delete' all the CString objects thati have inserted dynamically!for this i need to get the pointer to the CString....but using the RemoveTail() of the CStringList object i am able to get back only the LPCTSTR of the CString objects ,SO HOW do i GET THE CSTRING object pointer from the CStringList to delete the dynamically created CString objects?????:confused::confused::confused::confused: -- modified at 2:34 Tuesday 14th February, 2006

                  C Offline
                  C Offline
                  Cedric Moonen
                  wrote on last edited by
                  #8

                  Why don't you use RemoveAt and GetAt ? First, get the object at a specific position with GetAt, then remove it from the list with RemoveAt, then delete the object. POSITION Pos = ....; // Some position CObject* pToDelete = YourStringList.GetAt(Pos); YourStringList.RemoveAt(Pos); delete pToDelete; By the way, I think that the STL containers are much easier to use for particular cases like that. The direct access of the iterator is something very powerfull (no nooooo, I don't want to reopen the debate from yesterday about MFC and STL containers ;P ).

                  T N 2 Replies Last reply
                  0
                  • T toxcct

                    why don't you just do this ? (looking at the MSDN[^]...)

                    CStringList myList = /*...*/;

                    //...

                    CObject* pObj = NULL;
                    for (int i = 0; i < this->GetCount(); i++) {
                    //because GetAt() returns a CObject*&
                    pObj = myList.GetAt(i);
                    myList.RemoveAt(i);
                    delete pObj;
                    }


                    TOXCCT >>> GEII power
                    [toxcct][VisualCalc 2.20][VCalc 3.0 soon...] -- modified at 3:39 Tuesday 14th February, 2006

                    C Offline
                    C Offline
                    Cedric Moonen
                    wrote on last edited by
                    #9

                    I think (but I'm not sure because I don't use MFC containers), that you will have problems if you delete your object before removing it from the list. See my other post.

                    1 Reply Last reply
                    0
                    • C Cedric Moonen

                      Why don't you use RemoveAt and GetAt ? First, get the object at a specific position with GetAt, then remove it from the list with RemoveAt, then delete the object. POSITION Pos = ....; // Some position CObject* pToDelete = YourStringList.GetAt(Pos); YourStringList.RemoveAt(Pos); delete pToDelete; By the way, I think that the STL containers are much easier to use for particular cases like that. The direct access of the iterator is something very powerfull (no nooooo, I don't want to reopen the debate from yesterday about MFC and STL containers ;P ).

                      T Offline
                      T Offline
                      toxcct
                      wrote on last edited by
                      #10

                      yes, you were right... look at the example provided here[^]...


                      TOXCCT >>> GEII power
                      [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

                      C 1 Reply Last reply
                      0
                      • T toxcct

                        yes, you were right... look at the example provided here[^]...


                        TOXCCT >>> GEII power
                        [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

                        C Offline
                        C Offline
                        Cedric Moonen
                        wrote on last edited by
                        #11

                        BTW, I like the way you post code :). How are you doing that ? You change the color manualy for everything or do you use something special to edit code ?

                        T 1 Reply Last reply
                        0
                        • C Cedric Moonen

                          BTW, I like the way you post code :). How are you doing that ? You change the color manualy for everything or do you use something special to edit code ?

                          T Offline
                          T Offline
                          toxcct
                          wrote on last edited by
                          #12

                          hum, actually, manually... (for the moment)... but i plan to make a little program which gets the text you post and automatically adds the CP CSS tags for the colors :

                          <SPAN class=cpp-keyword> keyword </SPAN>
                          <SPAN class=cpp-comment> //comment </SPAN>
                          <SPAN class=cpp-preprocessor> #preprocessor </SPAN>
                          <SPAN class=cpp-string> "string" </SPAN>
                          <SPAN class=cpp-literal> 123 </SPAN>


                          TOXCCT >>> GEII power
                          [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

                          O 1 Reply Last reply
                          0
                          • T toxcct

                            why don't you just do this ? (looking at the MSDN[^]...)

                            CStringList myList = /*...*/;

                            //...

                            CObject* pObj = NULL;
                            for (int i = 0; i < this->GetCount(); i++) {
                            //because GetAt() returns a CObject*&
                            pObj = myList.GetAt(i);
                            myList.RemoveAt(i);
                            delete pObj;
                            }


                            TOXCCT >>> GEII power
                            [toxcct][VisualCalc 2.20][VCalc 3.0 soon...] -- modified at 3:39 Tuesday 14th February, 2006

                            N Offline
                            N Offline
                            namaskaaram
                            wrote on last edited by
                            #13

                            hello toxcct... this is what i found in MSDN class CStringList : public CObject Remarks The CStringList class supports lists of CString objects. All comparisons are done by value, meaning that the characters in the string are compared instead of the addresses of the strings. The member functions of CStringList are similar to the member functions of class CObList. Because of this similarity, you can use the CObList reference documentation for member function specifics. Wherever you see a CObject pointer as a return value, substitute a CString (not a CString pointer). Wherever you see a CObject pointer as a function parameter, substitute an LPCTSTR. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_cstringlist.asp[^] so now what?:((

                            T 1 Reply Last reply
                            0
                            • S Subramaniam s V

                              Sorry !!! May I know which return value(LPCTSTR) are you talking about. Because none of the functions that I mentioned 'Find' and 'RemoveAt' returns LPCTSTR. So I am confused as what return value you are talking about.

                              N Offline
                              N Offline
                              namaskaaram
                              wrote on last edited by
                              #14

                              see,itz not abt deleting from the list(yes that is also one requirement)...but what i need to know is how to "delete" teh allocated memory!(by just removing from teh list, the string only get removed from teh list but it does not free the memory associated with teh CString object that i had dynamically created!...that is why i need teh address of teh CString object!...) any ideas?

                              1 Reply Last reply
                              0
                              • N namaskaaram

                                hello toxcct... this is what i found in MSDN class CStringList : public CObject Remarks The CStringList class supports lists of CString objects. All comparisons are done by value, meaning that the characters in the string are compared instead of the addresses of the strings. The member functions of CStringList are similar to the member functions of class CObList. Because of this similarity, you can use the CObList reference documentation for member function specifics. Wherever you see a CObject pointer as a return value, substitute a CString (not a CString pointer). Wherever you see a CObject pointer as a function parameter, substitute an LPCTSTR. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_cstringlist.asp[^] so now what?:((

                                T Offline
                                T Offline
                                toxcct
                                wrote on last edited by
                                #15

                                this is for danta handling into the list... but if you use the C++ delete keyword, you have to give it the address to the memory block to delete


                                TOXCCT >>> GEII power
                                [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

                                1 Reply Last reply
                                0
                                • C Cedric Moonen

                                  Why don't you use RemoveAt and GetAt ? First, get the object at a specific position with GetAt, then remove it from the list with RemoveAt, then delete the object. POSITION Pos = ....; // Some position CObject* pToDelete = YourStringList.GetAt(Pos); YourStringList.RemoveAt(Pos); delete pToDelete; By the way, I think that the STL containers are much easier to use for particular cases like that. The direct access of the iterator is something very powerfull (no nooooo, I don't want to reopen the debate from yesterday about MFC and STL containers ;P ).

                                  N Offline
                                  N Offline
                                  namaskaaram
                                  wrote on last edited by
                                  #16

                                  hello cedric! actually that what i did!...he is the small snippet!....i get an error! CObject* pObject1; pObject1 = myList1->GetAt(myList1->FindIndex( 0 ));//ERROR!!!!! myList1->RemoveAt(myList1->FindIndex( 0 )); delete pObject1;

                                  error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class CString' (or there is no acceptable conversion)

                                  what do i do?any ideas?:sigh:

                                  C 1 Reply Last reply
                                  0
                                  • T toxcct

                                    hum, actually, manually... (for the moment)... but i plan to make a little program which gets the text you post and automatically adds the CP CSS tags for the colors :

                                    <SPAN class=cpp-keyword> keyword </SPAN>
                                    <SPAN class=cpp-comment> //comment </SPAN>
                                    <SPAN class=cpp-preprocessor> #preprocessor </SPAN>
                                    <SPAN class=cpp-string> "string" </SPAN>
                                    <SPAN class=cpp-literal> 123 </SPAN>


                                    TOXCCT >>> GEII power
                                    [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

                                    O Offline
                                    O Offline
                                    Owner drawn
                                    wrote on last edited by
                                    #17

                                    toxcct wrote:

                                    but i plan to make a little program which gets the text you post and automatically adds the CP CSS tags for the colors :

                                    Sure, sure!


                                    Owner drawn Jesus Loves

                                    1 Reply Last reply
                                    0
                                    • N namaskaaram

                                      hello cedric! actually that what i did!...he is the small snippet!....i get an error! CObject* pObject1; pObject1 = myList1->GetAt(myList1->FindIndex( 0 ));//ERROR!!!!! myList1->RemoveAt(myList1->FindIndex( 0 )); delete pObject1;

                                      error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class CString' (or there is no acceptable conversion)

                                      what do i do?any ideas?:sigh:

                                      C Offline
                                      C Offline
                                      Cedric Moonen
                                      wrote on last edited by
                                      #18

                                      What is the type of myList1 ?? From the MSDN:

                                      CObject*& GetAt(
                                      POSITION position
                                      );

                                      Are you sure the error comes at this line ?

                                      N 1 Reply Last reply
                                      0
                                      • C Cedric Moonen

                                        What is the type of myList1 ?? From the MSDN:

                                        CObject*& GetAt(
                                        POSITION position
                                        );

                                        Are you sure the error comes at this line ?

                                        N Offline
                                        N Offline
                                        namaskaaram
                                        wrote on last edited by
                                        #19

                                        yeah!....u see as long as i typecast it to LPCTSTR and that too to an LPCTSTR i dont get an error..... in teh MSDN it says that for CStringList teh returned parameter will alwayz be replaced from CString* to LPCTSTR..... so is there a way of getting CString* from LPCTSTR?.....i mean after all LPCTSTR is a pointer inside the CString object!

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups