MFC: member variable vs resource id of control

1.2k Views Asked by At

I am new to vc++. I want to know which is proper and efficient way of handling controls by using member variable or resource id of controls.

m_Name.SetWindowTextW(_T("xyz"));
or 
SetDlgItemText(IDC_EDIT2,_T("xyz"));
or 
CWnd* cwd =    GetDlgItem(IDC_EDIT2);
cwd->SetWindowTextW(_T("xyz"));
2

There are 2 best solutions below

4
On BEST ANSWER

The efficient way is to create a control member variable. You can do this in the resource editor by right-click on the control and select Add Variable.

Every time you use GetDlgItem with the resource ID then it iterates through all child controls to find the one with the specified ID. This iteration is inefficient when done for every GetDlgItem call.

0
On

You should be concerned about what is more maintainable over the long run instead of efficiency unless you really have a performance bottleneck. It's probably not something to worry about in a SetWindowText() operation.

You have to ask questions like

1) How many times are you going to set the window text? Just once or multiple times? If once, you should probably just use SetDlgItemText().

2) Are you going to do other operations besides just setting the text? If so, then probably make it a member variable. If not, then what is the point of making it a member variable and subclassing the window? To save a millisecond the user will never notice?