Click here to Skip to main content
15,888,297 members
Articles / Desktop Programming / MFC
Article

Extending CStringArray

Rate me:
Please Sign up or sign in to vote.
4.82/5 (14 votes)
11 Dec 2007CPOL 95.7K   1.2K   23   8
An extended CStringArray class
  • Download source files - 17 Kb

    Sample Image - meStringArray.gif

    Introduction

    One of the reasons I left C and began to use C++ was CString and even more so CStringArray. These two classes are worth all the other aggravation when going OO. I don't claim that they are perfect. There are things missing in both of those classes and I would like to share this extension of CStringArray that I have made.

    Finding an string in a CStringArray

    One of the things that I think are missing in CStringArray is a Find method. I find myself wanting to get a specific string out of the array and I know part, or the whole, of it. Not the position in the array. So I added this method. It is very straight forward and uses CString::Find to find the string.

    Example:

    meStringArray arr;
    
    // fill the array with strings using the normal Add()
    arr.Add("This is a String");
    arr.Add("The second string");
    arr.Add("the 3:rd string");
    
    // find the third string
       int i=arr.Find("3:rd");
    
       // i is now 2
       CString sBuf;
    
       sBuf=arr.GetAt(i);
    
       // sBuf has now the value of "the 3:rd string"
    

    Array of NULL terminating strings

    Some SDK-functions and some classes still using an array of NULL terminating strings. e.g. CFileDialog uses this kind of array for the filters. GetProfileString("section",NULL,"",buf,sizeof(buf)); also uses it, and there are many more. So every time you need to use this class/function you have to go back to C and do a lot of loops. Well I finally got tired of it and extended CStringArray with two methods; AddBuf() and GetBuf(). The first will take an array of NULL terminating strings and add it to the StringArray, and the second will do the reverse

    Example GetBuf:

    CFileDialog dlg;
    meStringArray filter;
    
    // init filter
    filter.Add("All files");
    filter.Add("*.*");
    filter.Add("Doc files");
    filter.Add("*.doc");
    filter.Add("Text files");
    filter.Add("*.txt");
    
    // Init file dialog
    dlg.m_ofn.lpstrFilter=filter.GetBuf();
    dlg.m_ofn.lpstrInitialDir=initDir;
    dlg.m_ofn.Flags |= OFN_SHAREAWARE;
    int rc=dlg.DoModal();
    if(rc==IDOK)
    {
        m_filepath=dlg.GetPathName();
        UpdateData(FALSE);
    }
    

    Example AddBuf:

    meStringArray arr;
    char buf[1024];
    
    // Get all printer names
    GetProfileString("devices",NULL,"",buf,sizeof(buf));
    
    // add them to the array
    arr.AddBuf(buf);
    
    // Get the second printer name from the array
    CString sPrintName = arr.GetAt(2);
    

    Well that's it! It's not a very large class so there is not so much to say. I hope that someone will find use for this and PLEASE let me know what you think! This is the first class I ever published so please say what you think about it, good, bad.

    // Anders

  • License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    Written By
    Software Developer
    Sweden Sweden
    I had my own company for 18 years. We made AutoCAD applications, mainly for GIS and we also developed our own document management system (for drawings).

    Two years ago a client made me an offer I couldn't refuse! So now I work as a Software Developer for a Marking Company. I make software that controls lasers that we use for marking.

    Comments and Discussions

     
    Generalit should be improved Pin
    ddc_smth26-Jul-05 21:40
    ddc_smth26-Jul-05 21:40 
    GeneralRe: it should be improved Pin
    Anders Eriksson16-Aug-05 22:59
    Anders Eriksson16-Aug-05 22:59 
    AFAIK, it already does!

    It support unicode the same way that CStringArray supports unicode.

    Could you please show some code that doesn't work in Unicode?

    // Anders
    GeneralThanks Pin
    valladis25-Mar-05 2:59
    valladis25-Mar-05 2:59 
    GeneralMemory error Pin
    alfa_aquila9-May-04 22:11
    alfa_aquila9-May-04 22:11 
    GeneralCStringDicho Pin
    Kochise5-May-04 12:07
    Kochise5-May-04 12:07 
    GeneralExtension CStringArray Pin
    7-Jun-01 21:24
    suss7-Jun-01 21:24 
    GeneralRe: Extension CStringArray Pin
    Lighto20-Dec-08 17:57
    Lighto20-Dec-08 17:57 
    GeneralJust a comment Pin
    Chris29-Jan-00 14:34
    Chris29-Jan-00 14:34 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.