if...else if.. & bad casting
-
I recently started a new job and my first project it to do a maintenance release of one of the companies products. Yesterday I found code like this...
if ((CString)presuffixS.GetAt(i) == "%") { // Add ID, TYPE, FORMAT
if ((CString)presuffixS.GetAt(i+1) == "i") {
presuffix = presuffix + szDeviceID;
i++;
}
else if ((CString)presuffixS.GetAt(i+1) == "t") {
presuffix = presuffix + szType;
i++;
}
else if ((CString)presuffixS.GetAt(i+1) == "n") {
presuffix = presuffix + szId;
i++;
}// and so it continues for and other 6 if elses
}
Could it be any less efficient? I also found this technique used several other places too!
Thanks, Robin.
-
I recently started a new job and my first project it to do a maintenance release of one of the companies products. Yesterday I found code like this...
if ((CString)presuffixS.GetAt(i) == "%") { // Add ID, TYPE, FORMAT
if ((CString)presuffixS.GetAt(i+1) == "i") {
presuffix = presuffix + szDeviceID;
i++;
}
else if ((CString)presuffixS.GetAt(i+1) == "t") {
presuffix = presuffix + szType;
i++;
}
else if ((CString)presuffixS.GetAt(i+1) == "n") {
presuffix = presuffix + szId;
i++;
}// and so it continues for and other 6 if elses
}
Could it be any less efficient? I also found this technique used several other places too!
Thanks, Robin.
It could. You could, in addition, convert the CString to a std::sting, then copy everything to a char* which has previously been allocated with malloc, and allocate it in the loop, but don't use free. Before you do that translate everything to Klingon.
If you're having a bad day, stare at your feet and smile.
-
It could. You could, in addition, convert the CString to a std::sting, then copy everything to a char* which has previously been allocated with malloc, and allocate it in the loop, but don't use free. Before you do that translate everything to Klingon.
If you're having a bad day, stare at your feet and smile.
:thumbsup: :)
Thanks, Robin.