mfc >> static CString...

by RAB » Fri, 24 Mar 2006 03:48:29 GMT

Hello,

I need to create a CString that I can pass from one Class to another
within my application.

I created within the App class header file the following:

static CString MyString; (outside of the class definition)

I filled the CString with a listbox selection in a dialog class and
want to pass it to another dialog class. The second dialog class
recognizes the variable as existing but the value is "0". The variable
is filled appropriately in the first dialog class. The project
compiles fine and there are no run-time crashes.

Any help would be appreciated.

Thanks,
RABMissouri



mfc >> static CString...

by Ajay Kalra » Fri, 24 Mar 2006 04:19:01 GMT


First, you should not use static variables. If you are using it for
whatever reason, you dont really need to pass it as parameter as it is
available to other class as well. In your case, how are you passing it?
Is it a pointer, reference etc.

One of the good ways to use it to pass it by reference if you want
other oject to be able to see the read/write same instance of CString.
Something like:

void SomeMethod(CString& someString);

// someString can be changed in SomeMethod. All changes are reflected
in the string that was passed in

or
void SomeMethod(const CString& someString); // someString cannot
change.

----------
Ajay Kalra
XXXX@XXXXX.COM




mfc >> static CString...

by Heinz Ozwirk » Fri, 24 Mar 2006 18:13:50 GMT




Static variables outside a class are visible only in the translation unit, where its definition is found, i.e. the CPP file that includes the definition. If you define a static variable in two different translation unit, you define two different variables. As you define a static variable the the app class's header file, you get a different variable in each CPP file that includes that header file. If you want a variable, that is visible in multiple translation units, you should define it in one CPP file (the app class's implementation file is not really a perfect place for that, but it will do) and declare it (usingthe extern keyword) in one header file. Include that header file in all CPP files that have to use this variable. But don't forget Ajay's advice and add some further rules

1. Don't use static variables. If you really need a variable that is only visible in a single translation unit, use an anonymous namespace instead.
2. NEVER define static variables in header files.
3. Don't use global variables. If you think you need a global variable, think again. There are only few situations where global variables are really needed.
4. NEVER define variables in header files.

HTH
Heinz


Similar Threads

1. static CString[]....

I want to create a static CString [] array...but it doesn't build correctly.
Is there anything I can do to work around this?

Thanks,
RABMissouri


2. Crash if CString is defined in Static Library using MFC in VStudio

3. CString.FormatMessage and CString.Format difference

CString.FormatMessage and CString.Format has the same parameters and they
seems to have to outcome. However, they coexist !. There must be some subtle
different between they.
Can you tell me the difference ?

Thank a lot


4. CString link error in VC7 with MFC extersion dll which export functions using CString as parameter

5. CString.FormatMessage and CString.Format difference

CString.FormatMessage and CString.Format has the same parameters and they
seems to have to outcome. However, they coexist !. There must be some subtle
different between they.
Can you tell me the difference ?

Thank a lot


6. 'CMap' of 'CString' to 'CString'

7. CString hex value to CString decimal value

Please bare with me, I am quite new to C++.

A CString contains a (large) hexadecimal string value,
this value needs to be converted to an decimal value,
and then put back as a string into an other CString.

Data example: hexadecimal:  93d2f666  =  decimal  2480076390

I have googled and found a lot of solutions (hex string to int)
but none of them returns the correct decimal value, I suspect
that converting  a large hex string to int does not work.

So if someone could point me to the right direction or
could suply a working sample, please do.

TIA

Alex






8. Unable to convert from ATL::CString to vector<CString>::iterator