Page 1 of 1

How I use wxArray: a shy attempt to explain...

Posted: Fri Sep 23, 2005 3:46 pm
by GeertVc
Hi,

I had problems using the wxArray class. I got some hints via the C++ programming forum (http://forums.wxwidgets.org/viewtopic.php?t=3483). That was enough to try it out myself.

Here's what I've done:
  • Created a structure:

    Code: Select all

        typedef struct
    	{
                wxString ComponentName;
                wxString ComponentDir;
                wxString SubsystemName;
                wxString RootName;
                size_t   LevelComponentName;
                size_t   LevelComponentDir;
                size_t   LevelSubsystemName;
                size_t   LevelRootName;
            } ComponentDefinition;
  • Used the macro (see documentation) to instantiate an array where I will put pointers to my structure.

    Code: Select all

    WX_DEFINE_ARRAY_PTR( ComponentDefinition *, CDefArray );
    This is in fact a new ADT, with CDefArray as name (just like int, char,...)
  • Created a variable of the type CDefArray:

    Code: Select all

    CDefArray CompDefinitionArray;
    This is all done in the header file, but I guess you can do this also in the cpp file.
  • In my C-file function, I used it as follows:

    Code: Select all

    CompDefinitionArray.Add( new ComponentDefinition );
    ComponentDefinition* test = CompDefinitionArray.Item( Counter );
    
    I first add a new 'instance' of the structure, by giving it as a parameter to the Add function of the wxArray class. In fact, you create memory for the structure at that time and you pass the address to the Add function (just like defined in the prototype).

    Then I created a local pointer pointing to such structure and assigned it the value of one of the pointers located in the wxArray (indicated by a number: Counter).

    At that moment, you can access the piece of memory that has been assigned for the structure which is pointed to by the returned pointer from the wxArray, or now by the pointer variable 'test', if you like.
  • From then onwards, it's not difficult anymore to access the fields of the structure.

    It's done as follows:

    Code: Select all

    test->RootName = TempString.Mid( 0, EndPos );
    The field RootName of the structure ComponentDefinition is a wxString type and I simply assign it a substring.

    This way, you can access all the fields of the structure you defined.

    Retrieving information is equally simple. If you want to see if the content of the array (in fact, all the locations where the pointers in the array are pointing at) is correct, you can use something like this (here, I didn't recall the integers which are part of the structure, only the string fields):

    Code: Select all

        for ( size_t Counter = 0; Counter < NrOfElements; Counter++ )
        {
            ComponentDefinition* test = CompDefinitionArray.Item( Counter );
    	    
            LOGTEXT( _T( test->RootName 
                       + "\\" 
                       + test->SubsystemName
                       + "\\"
                       + test->ComponentDir
                       + "\\"
                       + test->ComponentName
                       + "\n"
                       )
                    );
        }
    
HTH,

Best rgds,

--Geert

Posted: Fri Sep 23, 2005 7:30 pm
by vasek

Code: Select all

        
LOGTEXT( _T( test->RootName 
                   + "\\" 
                   + test->SubsystemName 
                   + "\\" 
                   + test->ComponentDir 
                   + "\\" 
                   + test->ComponentName 
                   + "\n" 
                   ) 
                ); 
wouldn't compile under Unicode, where _T("const string") expands as L"const string" and therefore

Code: Select all

_T(test->RootName + ... )
makes no sense.

Posted: Fri Sep 23, 2005 7:36 pm
by GeertVc
vasek wrote:

Code: Select all

_T(test->RootName + ... )
makes no sense.
Alternative?

Best rgds,

--Geert

Posted: Fri Sep 23, 2005 8:02 pm
by Ryan Norton

Code: Select all

LOGTEXT( _T( test->RootName 
                   + "\\" 
                   + test->SubsystemName 
                   + "\\" 
                   + test->ComponentDir 
                   + "\\" 
                   + test->ComponentName 
                   + "\n" 
                   ) 
                );
---->

Code: Select all

LOGTEXT(  test->RootName 
                   + _T("\\") 
                   + test->SubsystemName 
                   + _T("\\") 
                   + test->ComponentDir 
                   + _T("\\") 
                   + test->ComponentName 
                   + _T("\n") 
                   
                );