Re: parallel structure(s) used in sorting CListCtrl?
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Fri, 15 Dec 2006 17:21:48 -0500
Generally keeping parallel structures is a Really Bad Idea, and keeping it in a global
variable is a Disastrous Variation Of A Really Bad Idea.
Note that I think you've misinterpreted this example. The static table is just an example
of something that creates data. But it is an exceptionally poorly-written example (I
sometimes think these are written by summer interns who don't have much experience),
because it is representing a 2D-array as a 1D-array and doing weird offset computations to
figure out where the elements are. If it had been written sensibly, it would not have
done this. But note that it is actually creating an itemdata structure and filling it in.
As a typical example of the bad programming, it uses an LPTSTR to hold pointers to strings
(which you can't tell whether or not they are literals or on the heap), instead of
sensibly using CString values. The problem with LP[C]TSTR is that you have no idea when
you delete the object if the string needs to be deleted, and this example is particularly
bad in that regard since it doesn't seem to raise the issue.
Furthermore, there is absolutely no need to keep the array of ITEMDATA elements! They can
be kept in the LPARAM and the pointers don't need to exist anywhere else.
Overall, it is a particularly awful example, since it gets more things wrong or ignores
serious issues, introduces concepts (such as the additional array) that never needed to
exist, and looks like someone's early attempt to write a decent piece of Windows code.
They didn't understand the control, didn't understand C, didn't understand Windows, and
overall wrote a pretty awful article. The *only* thing the article says that makes sense
is that you should create a custom structure and store it in the LPARAM. Everything
around this idea is so badly done the article is somewhere between confusing and total
crap.
The simplest way to manage this is to store the one-and-only pointer to the information in
the LPARAM field of each element of the list control, and use that for the sorting.
Sorting gives a pointer to the LPARAM, and therefore by casting the LPARAM to an
appropriate pointer you have whatever you need to do the sorting. There is no need to
maintain anything outside the control for this.
Note that you will have to respond to deletion notifications to make sure you delete the
itemdata reference. The advice to clean up in WM_DESTROY is a particularly egregious
example of amateur programming. It doesn't allow for the fact that thousands of elements
might be added and removed in the lifetime of the control. And the fact that you would
have to delete the strings pointed to by the LPTSTR pointers is omitted, because they
oversimplified the problem by using only string literals. In a real control this would
not be true because the data might be coming from fies, databases, remote sensors, etc.
and therefore be on the heap. ARGH!
The scary thing is that according to the information on the page, this was reviewed in
November 2006 and nobody stood up and said "This article is crap! Rewrite it!" Nothing
short of a rewrite will fix it.
Subclass the CListCtrl and handle the reflected =TVN_DELETEITEM notification.
It is hard to imagine more design-blunders-per-article than this one has.
joe
On Fri, 15 Dec 2006 12:26:00 -0800, Jim <Jim@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
In the following article:Joseph M. Newcomer [MVP]
http://support.microsoft.com/kb/250614
...the author uses a global structure for maintaining list view data. Is
there a way to do CListCtrl sorting that isn't so restrictive? Preferably,
I'd like to only use the data in the items themselves along with what's set
with SetItemData().
Thanks.
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Prev by Date: Re: Calling Is_High_Surrogate from Visual C++ 6.0
- Next by Date: Re: CString GetData() removed
- Previous by thread: CString GetData() removed
- Next by thread: Re: parallel structure(s) used in sorting CListCtrl?
- Index(es):
Relevant Pages
|