• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 459
  • Last Modified:

CTreeCtrl question

I'm using a CTreeCtrl populated from DB values to display a hiearchy  (user--reports---updates)

Populating it is working just fine, I select all users, loop through selecting all reports, looping through selecting all updates.

Within each loop I track the parent and add the items accordingly. For each line item, I am using SetItemData(<HTREEITEM>, ID)   -  ID being the recordset ID for the line item.

This all works. Currently however, I'm manually finding the level of each item, and simply hard-coding that "level 1 refers to users"  .. "level 2 refers to reports" .. and "level 3 refers to updates" - and that works as well. I know that an ID on level 2 is the ID of a record from the reports table, and ID on level 3 refers to a record from the updates table etc.

that seems "inappropraite" though - the hard-wiring of that. I have classes modeling each of those tables (lets call them MyUser, MyReport, MyUpdate) and rather than having to rely on "knowing" what each level refers to, I'd like to somehow have each line item in the tree "know" not only the ID, but the "what" the ID is for.

Does that make sense? Can I define some kind of structure which holds a DWORD ID and some kind of string identifier for the type of class I want to instantiate based on the ID of the selection and somehow stuff that into the SetItemData(..) call ?

thanks!
0
PMH4514
Asked:
PMH4514
  • 3
  • 2
1 Solution
 
Jaime OlivaresCommented:
There is a simple aproach, I think:
divide the ID in 2 parts:

8 higher bits: item type (1=user, 2=report, 3=updates, etc)
24 lower bits: ID itself

So, every time you retrieve item's data (32 bits), evaluate the higher 8 bits (itemData>>24) to know the item type, and use only 24 bits (itemData & 0xFFFFFF) to identify the item.
0
 
PMH4514Author Commented:
interesting.. but that would still force me to know that 1==user and 2==report and 3==updates etc..  could I somehow stuff CMyUser as a class name to instantiate, into it? or am I overthinking unnecessarilly?
0
 
Jaime OlivaresCommented:
you can derive all these classes from a single abstract class (like CMyBase), so all can have equivalent functions you can call without knowing exactly the class type, or you can have a helper class (CMyClassManager) that evaluate the item type before calling proper function with a switch/case.
0
 
Jaime OlivaresCommented:
0
 
PMH4514Author Commented:
I actually already use a factory pattern to instantiate these derivations of a common base class elsewhere in the app. I was primarilly looking for a way to stuff into each tree line item the value needed to supply to the factory. I think your first comment will work nicely.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now