how to use global variables??????????/

Login to reply to this topic.
Mon, 2007-11-19 04:35
Joined: 2007-11-02
Forum posts: 37

Hi,
I need to use some variables in all the classes in the program.How to declare them globally.

Regards!


Mon, 2007-11-19 06:30
Joined: 2005-11-20
Forum posts: 1132
Re: how to use global variables??????????/

You cannot directly declare something to be "global", but the next best thing is probably to declare something as public in your AppUI class because the one AppUI object of your application is "visible" from everywhere.

See also e.g. this earlier thread:
http://www.newlc.com/forum/transmitting-data-between-views


René Brunner

Mon, 2007-11-19 10:50
Joined: 2007-07-24
Forum posts: 55
Re: how to use global variables??????????/

HI,
U just declare the variables in appui class's public part.then u access anywhere in following manner
((CExampleAppUi*)(CCoeEnv::Static()->AppUi()))->variablename;
Thanks
mitu

Mon, 2007-11-19 12:23
Joined: 2004-11-29
Forum posts: 1151
Re: how to use global variables??????????/

You can declare something to be "just global".

You do it just as you usually do in C and C++
Just define it in the global namespace, or as a public static member variable, and make sure everything that uses it has access to a header file declaration of it.

Though, usage of global variables has some overhead (mostly memory I think) and must be carefully considered on symbian platform, and should only be used when they _really_ are needed.
Which more or less only is when porting code that relies on it.

Mon, 2007-11-26 12:23
Joined: 2007-11-02
Forum posts: 37
error in declaring a variable

Hi,
I have the following problem.
have 2 classes smsAppUi, SMSOPERATIONS
I need to know how to declare the variable
const TInt KMaxSMSTextLength=100;

SMSOPERATIONS_H
---------------------------------

#ifndef SMSOPERATIONS_H
#define SMSOPERATIONS_H

#include "smsAppView.h"
#include "smsAppUi.h"

class CsmsAppUi;

const TInt KBfrLength= 20;
const TInt KMaxSMSTextLength=100;

class CSmsOperations : public CActive,public MMsvSessionObserver
{
---------------
-------------------

private:
CsmsAppView* iAppView;
CsmsAppUi* iAppUi;

TBufC< KMaxSMSTextLength> iSmsBody;



};

#endif // SMSOPERATIONS_H

SMSAPPUI_h
-------------------------

#ifndef __SMSAPPUI_h__
#define __SMSAPPUI_h__

// INCLUDES

#include "SmsOperations.h"

// FORWARD DECLARATIONS
class CsmsAppView;
class CSmsOperations;

//const TInt KBfrLength= 20;
extern const TInt KMaxSMSTextLength=100;

class CsmsAppUi : public CAknAppUi
{
-------------------
------------------------------



private: // Data

CsmsAppView* iAppView;
CSmsOperations* iOperns;

TBufC< KMaxSMSTextLength> iSmsBody;


};

#endif // __SMSAPPUI_h__

// End of File

At present it is giving 2 errors


1.error: `const TInt KMaxSMSTextLength' previously defined here
2.error: redefinition of `const TInt KMaxSMSTextLength'

Mon, 2007-11-26 12:35
Joined: 2005-11-20
Forum posts: 1132
Re: how to use global variables??????????/

I don't think that there is any need for that extern const definition in SMSAPPUI_h. Because you include SmsOperations.h, that constant is already defined, and that's all that is needed.

So just delete the line

extern const TInt KMaxSMSTextLength=100;


René Brunner

Mon, 2007-11-26 12:39
Joined: 2007-05-15
Forum posts: 14
Re: how to use global variables??????????/

Remove "const TInt KMaxSMSTextLength=100"from SmsOperations.h or your AppUi

Mon, 2007-11-26 12:58
Joined: 2007-11-02
Forum posts: 37
error in declaring a variable

Then it gives 3 errors

1.error: `KMaxSMSTextLength' was not declared in this scope
2.error: ISO C++ forbids declaration of `iSmsBody' with no type
3.error: template argument 1 is invalid sms/inc smsAppUi.h

Mon, 2007-11-26 13:16
Joined: 2007-11-02
Forum posts: 37
error in declaring a variable

Then it gives 3 errors

1.error: `KMaxSMSTextLength' was not declared in this scope
2.error: ISO C++ forbids declaration of `iSmsBody' with no type
3.error: template argument 1 is invalid sms/inc smsAppUi.h

Mon, 2007-11-26 14:01
Joined: 2007-09-20
Forum posts: 95
Re: how to use global variables??????????/

Hi,

Define the variable in .cpp file as global and refer to it in other files as extern.

i.e; in .cpp file

const TInt KMaxSMSTextLength=100;
#include ""
.....
....

and in .h file refer it as
#ifndef __SMSAPPUI_h__
#define __SMSAPPUI_h__

// INCLUDES

#include "SmsOperations.h"

// FORWARD DECLARATIONS
class CsmsAppView;
class CSmsOperations;

//const TInt KBfrLength= 20;
extern const TInt KMaxSMSTextLength=100;

class CsmsAppUi : public CAknAppUi
{
-------------------
------------------------------



private: // Data

CsmsAppView* iAppView;
CSmsOperations* iOperns;

TBufC< KMaxSMSTextLength> iSmsBody;


};

#endif // __SMSAPPUI_h__


Chao,
Raghav

Mon, 2007-11-26 14:10
Joined: 2005-11-20
Forum posts: 1132
Re: how to use global variables??????????/

Hey, come on, since when do I need to mess around with "extern" for declaring a simple, harmless constant in one .h and then using it in a second .h?

Chinu, are you sure you just only deleted the second declaration of KMaxSMSTextLength, changed nothing else and did not produce any typo? If yes, I don't understand the new errors, frankly.


René Brunner

Mon, 2007-11-26 14:53
Joined: 2007-11-02
Forum posts: 37
error in declaring a variable

Then it gives 3 errors

1.error: `KMaxSMSTextLength' was not declared in this scope
2.error: ISO C++ forbids declaration of `iSmsBody' with no type
3.error: template argument 1 is invalid sms/inc smsAppUi.h

Sorry this message was automatically printed.Dont know how...I have not yet viewed any of the posts.
Is there something wrong with my forum user ID?

Mon, 2007-11-26 15:31
Joined: 2007-11-02
Forum posts: 37
Re: how to use global variables??????????/

yes, I deleted from only one of the classes.

Actually I had tried with this before making a post.

-------------------------------------------------------------------------------
NOW
-------------------------------------------------------------------------------
I tried with somewhat different approach:----

smsappui.h
----------------------

#ifndef __SMSAPPUI_h__
#define __SMSAPPUI_h__
GLREF_C const TInt KMaxSMSTextLength;
----
---

others remaining the same..

The errors related to declaration of TInt KMaxSMSTextLength were solved, but

I had following errors:---
error: ISO C++ forbids declaration of `iSmsBody' with no type
error: non-constant `KMaxSMSTextLength' cannot be used as template argument

of course I dont know anything about
GLREF_D
GLDEF_D
LOCAL_D
GLREF_C
GLDEF_C
LOCAL_C

Now what to do?

Mon, 2007-11-26 15:54
Joined: 2004-05-24
Forum posts: 981
Re: how to use global variables??????????/

The solution for your problem is as alh said:
"You do it just as you usually do in C and C++".....


pirosl

Wed, 2007-11-28 07:29
Joined: 2007-08-07
Forum posts: 25
Re: how to use global variables??????????/

Hi Chinu

You can just use the global variable as you use it in C and C++.

you need to add this line in the mmp file of your "EPOCALLOWDLLDATA" this enable you to have aritable static data.
make sure you dont have more than 64kb of static data

  • Login to reply to this topic.