Re: How to Define a global const class variable in MFC?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



See below...
On Sun, 31 Aug 2008 23:53:00 -0700, Electronic75 <Electronic75@xxxxxxxxxxxxxxxxxxxxxxxxx>
wrote:


hello joe :),
Very good idea, those commands can be saved in a file and at time of
selecting a specific embedded device the corresponding file will be loaded
into a command collection class. I probably change the code to adopt this
approach.
I also need to create many dialog controls for each command set to get
command parameters from user. First I thought to adopt an approach similar to
your approach( I mean dynamically creating each control and destroying them)
but after a few weeks working on it I realized this could make code very very
complex
****
Well, it was complex. The reason I did it was that they sent me, for a test, their
*simplest* controller (about 300 windows, quite manageable). Then when I'd delivered the
product, they tried it on their *most complex* controller (about 5000 windows) and then
discovered it couldn't handle more than two of these. So it was either completely
redesign the code or create a solution that worked with the existing code.
****
for example many instructions need same control but those controls
should be grouped differently for each device or command for example for
command1 parameter1 ,2 and 3 are related and it make sense to place them near
each other but for another command parameter 1 and 7,11 are related and
should be grouped with each other.
****
I never worried about that. The description files said explicitly what controls were in
use. Imagine they were of the form
<Controller
<Inputs>
<Group title "Analog" count "8">
<Numeric range/>
<Boolean active/>
<Numeric value/>
<Numeric rate/>
</Group>
</Inputs>
<Outputs>
<Group title "Analog" count "16">
<Numeric range/>
<Numeric zero_offset/>
<Boolean active/>
<Numeric value/>
<Numeric rate/>
<Options title "Conversion"/>
<option "Fast and sloppy"/>
<option "Slow and accurate"/>
<option "Whatever"/>
</Group>
</Outputs>
</Controller>

So the caption, control type, etc. were obtained from this configuration file and windows
were created to match. A group meant a new tab and the group title was the tab name. Tabs
could nest very deeply (a big controller might have eight levels of tabs!)

No, it wasn't as nice as the XML I'm showing. Parsing the config files was hairy, but
their format could not be changed (it was some kind of industry standard representation).

My internal representation was that each window had a data structure that represented it
and held its value. If a window was visible, it would poll the remote embedded device and
specified intervals for the current value, or you could, if you wanted an instantaneous
value, just click on the control (the polling interval could be once a minute or even less
frequently, user settable). Options could be done as dropdowns or radio button groups.
When a tab was selected, I just raced along its associated data structure and created all
the controls. When it was deselected, I saved whatever values were in the controls so I
could set them the next time around. A control that was not being shown did not need to
poll for its data, and this reduced the network traffic considerably (imagine 60,000 items
all asking for their data once a minute: that's 1ms/request, and this more than saturated
the low-speed proprietary control network, and it was not uncommon to have 60,000
controller values potentially active. But in fact, no more than about 100 were active at
any one time, sometimes as few as eight. This was a tremendous improvement over their old
version!)

Some pages named the same parameter as other pages, so there were summary pages of all
kinds. For example, the analog input page just gave 16 check boxes indicating which
inputs were active. Then there were 16 tabs, and each page had an active boolean which
was the same parameter. All of this was derived from their configuration files.
joe
****

So I adopted a new approach and that is to
create a wizard for filling parameters. I mean according to each command
some parameter pages are added to a wizard and again according to inputs of
user some pages may be added later. this was much easier to code but I admit
that your approach that many related controls be visible to the user at a
time is more user friendly. Of course In my approach user still can go back
and change previous parameters and at the end he will see a summery of all
parameters at one summery page.
thank you :)
Best wishes,



"Joseph M. Newcomer" wrote:

See below...
On Sun, 31 Aug 2008 09:57:00 -0700, Electronic75 <Electronic75@xxxxxxxxxxxxxxxxxxxxxxxxx>
wrote:

Thanks a lot joe and tom for your positive and informative inputs.
I generally do not use global variable much but in this case
I like to define a series of constant commands for program. the program
talks to different embedded devices and user at the beginning selects a
device that is connected to via USB. Now for talking I need a big set of
commands for each device these commands not only include their command codes
but many parameters, priorities, conversion algorithm codes,...,
Now it is a big chunk of information for each command, so I need many files
for different devices e.g. file1 has instruction set of device1, file 2 has
instruction set of device2,...
I have read somewhere that it is an unwritten convention that a C file
should never go beyond 1000 lines except in very rare occasions to ease debug
and future manipulation.
****
I find principles like this rather silly. For example, I have C files that contain one
line. I have C files that contain 8000 lines. File size rarely has any value in dealing
with debugging, and editing a 10,000 line file is not that much harder than editing 1,000
lines.
****
so in my case I tried to store these information in cons variables. you may
ak why I do not use structs for this, well I may do that in light of what
I've learned here but my original problem was to store those const variables
in const classes so I can also benefit from functionality of classes to do
sum calculations on some parameters of these const commands.
****
But const tables that are based on CArray of a struct are not const and can't be.

I can't even parse much of the last description.

I do exactly this for an embedded system; the company has files that describe each
command, each option, etc. for each class of embedded device, and I simply read these in
and create descriptions (a set of nested tabbed dialogs, in fact, that contain scrollable
sets of controls which include edit controls, combo boxes, radio buttons, etc.). It is
done as an MDI application, where each "document" is an embedded controller.

At no point did I need a global variable. The commands are part of the CDocument-derived
class. Compared to the complexity of the user interface, the cost of the data structures
is irrelevant (a given controller may require 5,000 controls; I dynamically create them
when the page has focus and destroy them when the page loses focus, so at no time does a
controller require more than perhaps 100 windows at any given instant). So it isn't clear
how you use a global variable here, since you would need to have one variable per
controller type.

At no point can these be const, because const is a compile-time declaration that says
"this can never change", which means you cannot ever load them from a file.
joe
****
joe
****

thank you all :)


"Tom Serface" wrote:

Just an opinion...

I would not make this a global variable. Instead I would probably add it to
the main app class or perhaps, a class that contains other variables like
this that is, itself, hosted in the main app class. That way you could
access it with code like:

theApp.m_InfoArray.Add()

Which lets you know where it is hosted and also you could easily clean it up
when exiting the application instance. This is also very easy to debug.

I try very hard not to use global variables these days and I've found it
makes debugging and cleaning up much easier.

Tom

"Electronic75" <Electronic75@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:420537CC-30E0-4A6A-B24B-A54AC08B8AC2@xxxxxxxxxxxxxxxx
Hello :), I'm trying to define a global const class varibles in a MFC
application. This class holds an array of variables that I like to assign
at
the beginning and I will use this variable and its stored const values in
my
program in different classes. Now what I'm doing is to add a blank cpp
file
to my project and then I define this class variable there.


#include "MyInfoArrayClass.h"

const CMyInfoArrayClass g_Info;
g_Info.Add(data1);
g_Info.Add(data2);
...


but compiler dose not see the definition and intellisense shows the type
of
g_Info as int and returns SYNTAX error and redefinition of basic type.
Even
when I place class definition in a header file and include header file in
the
source file compiler returns same errors.
I have done this with a char type or int type, I mean I have defined a
global char or int variable, and I had no problem but when I want to
define a
class type variable I encounter trouble.
Where I made a mistake?
thanks :)


Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • PC8477 Demo Program
    ... The PC8477 Demo Program is designed to allow access to all software commands and registers of National Semiconductor's PC8477 Advanced Floppy Disk Controller. ... The left center indicates the number of bytes transferred during the last command issued. ...
    (comp.sys.ibm.ps2.hardware)
  • HSG60 SCSI write abort on unit, not disk
    ... I have a HSG60 controller with mirror sets on it. ... I have also included the description of the Instance and the SCSI ... SCSI Command Opcode: 42. ... Sense Data Qualifiers: 64. ...
    (Tru64-UNIX-Managers)
  • [PATCH 3/9] MFD: Add chip handler for the ST-Ericsson CG2900.
    ... This patch adds a chip handler for the ST-Ericsson CG2900 Connectivity ... * for Bluetooth commands in the ST-Ericsson connectivity controller. ... * true if the command will generate an interrupt. ...
    (Linux-Kernel)
  • [PATCH 2/6] This patch adds support for the ST-Ericsson CG2900
    ... * for Bluetooth commands in the ST-Ericsson connectivity controller. ... * true if the command will generate an interrupt. ... * Parses a FM command packet and updates the FM mode state machine. ...
    (Linux-Kernel)
  • Re: newbie question: Can I modify MFC source code?
    ... When the command finally got to my window (which was the active ... it then looked up the command in its own table of controls and set the value in the ... may ak why I do not use structs for this, well I may do that in light of what I've learned here but my original problem was to store those const variables in const classes so I can also benefit from functionality of classes to do sum calculations on some parameters of these const commands. ... I try very hard not to use global variables these days and I've found it makes debugging and cleaning up much easier. ...
    (microsoft.public.vc.mfc)