Click here to Skip to main content
15,936,362 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
How do I create global variable for dialog class (global object of dialog class), which is accessible to all other classes including other dialog classes?

Thank You.
Posted

I suggest you implement the Singleton Pattern[^].
 
Share this answer
 
Comments
Olivier Levrey 15-Apr-11 7:43am    
My 5. This is definitely better than using global variables.
Niklas L 18-Apr-11 2:05am    
Thanks!
Sergey Alexandrovich Kryukov 15-Apr-11 13:19pm    
Ad-hoc global variable is evil. My 5.
--SA
Niklas L 18-Apr-11 2:05am    
Thank you!
Define it inside the dialog source file and declare it as extern in all the other source files allowed to access it, e.g.

// MyDialog.cpp
int g_counter = 0; // variable definition (and initialization, here)


// MyOtherDialog.cpp
extern int g_counter;
//...
g_counter = 5; // access global variable defined in MyDialog.cpp


:-)
 
Share this answer
 
Comments
Olivier Levrey 15-Apr-11 7:44am    
Technically correct. But you should warn about the dangers of using global variables... My 5 anyway.
CPallini 15-Apr-11 7:58am    
Well, you're right. Anyway there are always some folks giving good design advices here, so that I can refrain from doing it.
BTW Thank you.
Sergey Alexandrovich Kryukov 15-Apr-11 13:20pm    
Agree with Olivier, but as its shows the technique which usually confuse novice developers, my 5.
--SA
CPallini 15-Apr-11 14:22pm    
Well, when I ask for new tyres there's usually someone who advices me buying a new car (you know, safer, more ecological, ...). Of course I value such folks, but... :-)
BTW Thank you.
Sergey Alexandrovich Kryukov 15-Apr-11 15:41pm    
...I only smile at them to have them stop talking about their enthusiasm of boosting the industry in this way. :-)
--SA
If the need is a global variable available to all the dialogs in your app... I'd go for a variable declared in your app class... then you'll be able to get access to it from everywhere...

I'd use getters and setters in order to acces to it...

HTH! :thumbsup:
 
Share this answer
 
v2
Comments
Niklas L 15-Apr-11 9:29am    
Although a working solution, you will pollute your app class with something that isn‘t it‘s concern, adding to build dependencies and compile time.
Joan M 15-Apr-11 9:35am    
True at 100%. According to the needs and depending on how big is the application though... this can be a really easy way to solve the problem. Of course it is not the most elegant one... :rolleyes:
I suggest not to use global variables because they just bring too many problems:
Global Variables Are Bad[^]

Niklas's answer is probably the best choice.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 15-Apr-11 13:22pm    
Some brainwash will never hurt, my 5. :-)
--SA
Olivier Levrey 15-Apr-11 13:36pm    
He he thanks ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900