Re: CStringArray



You may want to try code like this, defining an helper class called
IONameManager:

<code>
IONameManager ioNames;

// mod = 0, location = 0
ioNames.InsertName( 0, 0, _T("Test") );

// mod = 2, location = 3
ioNames.InsertName( 2, 3, _T("Ciao") );

// Print content of the class
MessageBox( NULL, ioNames.ToString(), _T("IONameManager"), MB_OK);
</code>

The class implementation is like this (it may have bugs, but seems to
work...):

BTW: I'm not sure I understood your specifications correctly...

<code>

// ---------------------------------------------------------------------
// Stores a 2D array of strings, indexed by "ModNum" and "Location".
//
// Use InsertName() to insert/modify a name in the given position.
// Use GetName() to retrieve the name in given position
// (returns an empty string if no name is inserted there).
// ---------------------------------------------------------------------
class IONameManager
{
public:

enum { ModNumMax = 16 }; // modNum in 0-16
enum { LocationMax = 12 }; // location in 0-12


// Build the object
IONameManager()
// Build array of proper size
: m_names( (ModNumMax+1) * (LocationMax+1) )
{
}


//
// The class methods check for valid position indexes.
// On bad indexes, they ASSERT in debug builds, and
// throw exceptions in release builds.
//

// Insert/modify string in given position
void InsertName( int modNum, int location, const CString & name )
{
ATLASSERT( ValidIndexes( modNum, location ) );
if ( ! ValidIndexes(modNum, location ) )
throw std::out_of_range( "Bad addressing." );

m_names.at( GetIndex(modNum, location) ) = name;
}

// Get string in given position
CString GetName( int modNum, int location ) const
{
ATLASSERT( ValidIndexes( modNum, location ) );
if ( ! ValidIndexes(modNum, location ) )
throw std::out_of_range( "Bad addressing." );

return m_names.at( GetIndex(modNum, location) );
}

// Return string representation of the class,
// for debugging purpose
CString ToString() const
{
CString result;

for ( int mod = 0; mod <= ModNumMax; mod++ )
{
for ( int loc = 0; loc <= LocationMax; loc++ )
{
result.Append( _T("[") );
result.Append( GetName(mod, loc) );
result.Append( _T("] ") );
}
result.Append( _T("\n") );
}

return result;
}


//
// IMPLEMENATION
//
private:

typedef std::vector< CString > StringArray;
StringArray m_names;

// Do the given indexes specify a valid position?
bool ValidIndexes( int modNum, int location ) const
{
bool valid = false;

if ( modNum >= 0 && modNum <= ModNumMax )
{
if ( location >= 0 && location <= LocationMax )
valid = true;
}

return valid;
}

// Convert 2D indexes into 1D, to properly access the 1D array
int GetIndex( int modNum, int location ) const
{
return ( location + modNum * (LocationMax+1) );
}
};

</code>

Giovanni



"Hoop" <jcollett@xxxxxxxxxxxx> ha scritto nel messaggio
news:fa135a0a-44f1-443f-8d21-c115ed582a38@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi,
I am doing some maintenance work on an MFC app.
The portion of code I have to deal with uses a number CStringArrays.
I am not that familier with these and have had limited success.
Code wise an array is created,
CStringArray ioNames[64];

Data is added,
void AddIOName(int modNum, CString name)
{
ioNames[modNum].Add(name);
}
and accessed,
CString GetIOName(int modNum, int num)
{
return ioNames[modNum][num];
}

From what I have learned so far I have to make use of either
InsertAt() or SetAt(),

void InsertIOName(int modNum, int location, CString name)
{
// ioNames[modNum].InsertAt( location, name);
ioNames[modNum].SetAt(location, name);
}

I am wondering if anybody knows where there is some good code examples
for this. I have been looking
over the info at MSDN, no much in the way of examples.

What I would like is the proper way to initialze the arrays, not sure
if the original code is correct.
And if I use InsertAt() and SetAt(), would the GetIOName() above work?
I do not think so.

Thanks For any help or feedback.
Jeff


.



Relevant Pages

  • Re: CStringArray
    ... void InsertName(int modNum, int location, const CString & name) ... CString GetName(int modNum, int location) const ...
    (microsoft.public.vc.mfc)
  • Re: Inheritance
    ... CString GetTeammascot(); ... int FindTeam; // Get the index for a particular team given their team name by looping through list ... class glider: public monoplane { ... Given this hierarchy, I can probably come up with actual planes that don't fit it. ...
    (microsoft.public.vc.mfc)
  • Re: Newbie: out of memory issues
    ... Heres the problem code:: Lots of array handling with objects.. ... You should use CString. ... char out2; ... BOOL swe_date_conversion(int year, int month, int day, int hour, int cal, double & ...
    (microsoft.public.vc.mfc)
  • Re: How To Refresh tree
    ... > pane tree control to Right pane list control which contain Tree Item, ... > CString strItemQry; ... > int nItemPriKey = GetItemPrimaryKey; ... > // check for any sub chield under sub chield ...
    (microsoft.public.vc.mfc)
  • Re: Multidimensional Packed Bit Array
    ... the offset and multiplier for each dimension is simply the total size of the ... I'm having troubles with calculating the required size of the storage and ... private int[] bitSize; ... int location = GetLocation; ...
    (microsoft.public.dotnet.languages.csharp)