Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello all,

I'm still porting hat old application and I can see things that should be improved.

As an example, I'm hardcoding the DoDataExchange function with specific hardcoded resource symbols to make the DDX assignments.

I can create an array using dynamic memory allocation easily, but what I do not know is how to make the assignment between what I've just created and the Resource symbol name.

i.e.:
C++
DDX_Control(pDX, IDC_STATIC_R5, m_cstatR5);
DDX_Control(pDX, IDC_STATIC_R4, m_cstatR4);
DDX_Control(pDX, IDC_STATIC_R3, m_cstatR3);
DDX_Control(pDX, IDC_STATIC_R2, m_cstatR2);
DDX_Control(pDX, IDC_STATIC_R1, m_cstatR1);

Here you can see how I'm making some associations.

I would like to use something like:
C++
CString csName = "";
for (i=0;i<100;i++)
{
  csName.Format("m_cstatR%i",i);
  DDX_Control(pDX, IDC_STATIC_R1+i, csName);
}


Now... how can I create the IDC_STATIC_Rx dynamically without worrying for the existing ones?

I'm worried about what could happen if two resource identifiers would become equal.

I've tried to google for it, but my google fu is not as good as it should... clearly as my VC++ skills which are a little bit rusty...

Thank you in advance!
Posted

The IDC_STATIC_R1+i part looks okay. Just make sure that those symbols are assigned a consecutive range of numbers, either by using the resource editor or by simply editing the resource.h file.

The third argument of DDX_Control is supposed to be a reference to CWnd. You cannot replace that by a string. The easiest way to comply to the API is in my opinion to create an array with your control member variables for those controls and feed the array elements to the DDX_Control call in your loop.
 
Share this answer
 
v2
A "static" solution :) :
C++
{
  enum { maxCount = ... };
  
  static CWnd& arCtls[maxCount] = {
    m_ctlStatic1, m_ctlStatic2, ...
  };
  static UINT  arCtlIds[maxCount] = {
    IDC_STATIC1, IDC_STATIC2, ...
  };

  for (int i(0); i < maxCount; i++) {
    DDX_Control(pDX, arCtlIds[i], arCtls[i]);
  }
}

Of course, the arrays and the enum could be declared as members of your dialog...
 
Share this answer
 
v2
Comments
nv3 18-Dec-14 9:45am    
arCtls in your code would be an array of references, which is not possible in C++ (see http://www.codeproject.com/Questions/203511/Why-array-of-references-not-allowed-in-C). Instead, I would create an array of CWnd in the class definition and refer to those in the for loop.
Eugen Podsypalnikov 18-Dec-14 9:49am    
Thank you :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900