Click to See Complete Forum and Search --> : global variables


nadz
June 13th, 2006, 11:10 AM
Hi,
Is there any way to define global variable in vc.net whose value can be acessed by all functions.
I am so pissed. The forms commincate only in one direction, .cpp files cannot even call functions defined in form. Is there any easy way to deal with this problem.
Help me out.
Cheers all.
nadz

ThermoSight
June 13th, 2006, 01:27 PM
Nadz,

I think there may be a way. I just tried it and it worked just fine.

In one file, I declare an int variable, 'globular', OUTSIDE of ANY CLASS or NAMESPACE.

.......
.......
.......
#using <System.Messaging.dll>
using namespace System::Messaging;

int globular = 3;<=====++ file "CryptoQueue.cpp"

namespace Crypto
{
.......
.......
.......

Then, in some other file light-years away functionally, I declare the variable as an external and use it .....

void Form1::encodeText(String *pString){ <<=====++ file "Form1a.cpp"

extern globular; <<====++ DECLARING THE GLOBAL VAR

int junk = globular; << ====++ USING THE GLOBAL VAR

if (pCrypto->memoryEncryption(textBox->Text)){



However, regarding global variables, there may be a better way to go. Perhaps you can give the vars you wish to share namespace scope, or class scope with the access set to "public:", and use an Object/Form1 * pointer as we dicussed in an earlier thread.


There's a bit of a learning curve associated with .NET, but once ya get the hang of it, yer gonna LOVE it. Many, I'm sure, think of .NET as Windows programming on training wheels, but for those of us new to Windows programming, it's a minor miracle. I am amazed at all the power available to me for so little effort and money.

Microsoft has done all the heavy lifting. All we need to do as wannabe Windows programmers is snap the pieces into place.

Best wishes,

bill

nadz
June 14th, 2006, 05:11 AM
Hi bill,
Thank you very much for your reply. The problem I am having with extern is that the compiler generates an error when I try to define extern.
cannot declare a global or static managed type object or a __gc pointer.
what to do. Any good ebook or tutorial to learn the form navigation and global varibles.
Thanking you.
nadz

ThermoSight
June 14th, 2006, 01:50 PM
Man! That's weird!

The example I gave re var "globular" works for me (Visual Studio 3, C++).

You must declare the variable outside of any namespace. In my example, I placed it near the top of the page, after the "using" statements.

Then I place the "extern" statement inside each function that will access the variable. That works for VS3, C++. Perhaps you could post your code or an example in which it fails to compile.

However, it's important to note that my example was an int which is neither a managed type object nor a __gc pointer.

A reminder .... a global variable is not necessarily the best way to proceed. There may be preferable alternatives.




re .NET tutorials, I have a bunch of'em, my two favorites being
"Visual C++ .NET - Step by Step" - Microsoft Press; Templeman & Olsen, and
"Visual C++ .NET - How To Program " - Pearson/PrenticeHall; Deitel, Deitel, Liperi, Yaeger.

Those two are expensive, but well worth the money. They give very good insight into most aspects of .NET. In my opinion, the Microsoft Press book is the better of the two for beginners.

The Deitel book is better for those with some experience. I love it because it made quite complex projects easy to understand through useful examples. It is now the first book I turn to when a problem arises.

I am also currently heavily involved with "Visual C++ .NET 2003 - Kick Start" - Sams; Kate Gregory.

But I should also point out that the HELP section of the IDE is an excellent reference source. It's not a very good tutorial (although it kinda tries to be), but it's a good reference.

If you still can't get the global variable thingy to work, post your code or an example of the code that isn't working ... perhaps we can MAKE it work.

Best wishes,

bill

nadz
June 16th, 2006, 03:30 AM
Hi Bill,
Thanks once again for replying. You are right, there is no problem using int, double etc. as global variables, but I need a mangaed String* type.
If I use some other method later it is impossible to use or convert into desireable type as I tried hard to do that. I know global variable is a bad choice but what when I am unable to access the functions as discussed in other thread.
I have VC++.net Step by Step by Templeman but its too basic and has a one page on concept of global varibales and that of int. I had finished the book before starting to work.
Please please help me out. The first thread topic is very important to me,
if you can write a piece of code for me or should I post you my code.
Thanking you.
nadz

goldenadam
June 19th, 2006, 05:42 PM
In order to have a global variable of a managed type, the easiest thing to do, is to declare an instantiation of a template that microsoft provides...

Example:
#include <vcclr.h> // Smart pointer to managed objects

/////////////////////////////////////////////////////////////////////////////
// GLOBALS variables
/////////////////////////////////////////////////////////////////////////////

gcroot< CounterCreationDataCollection *> gCCDC; gcroot< String *> strInstance;
gcroot< String *> strCategory;

Then in your main, or in some init function in your DLL, you have to create the object... just like you would any managed object:

strInstance = new String( S"hello" );

This template is basically a smart pointer that wraps a managed object, and marks it as to not be garbage collected, since it is at the root of your program, and never out of scope.

ThermoSight
June 22nd, 2006, 06:21 PM
Neat!

Hey, thanks so much for the tip, 'adam. I'll give that a try t'day.

bill