Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.
Singleton pattern for using variable anywhere in the code just like global variable
This code will create issues when used in a DLL as it uses writable static data. Though WSDs' are supported from EKA2 onwards but gcce compiler provided with public sdk's has known defects that prevent reliable support for this. Only Arm compilers (device target) are known to provide a reliable support. However if you intend to use this code in a dll make sure that you add EPOCALLOWDLLDATA statment to your mmp file otherwise the code will not link for the device target.
In any programming language, there are various ways to save data as we also have in symbian. We can also create our user define data type in the form of structure, union and array etc, but the main problem occur when we need to some data from in all the classes let suppose, the first screen of the application need user name and password and if user input the correct values in the boxes might we need the same values in further classed, might be for authentication etc.
The problem is small if we have two classes then we can easily handle it by just passing the argument from one class to another, but what happen if there are more 50 classes in the project and when ever you create the instance you need to supply the argument, whether they might be need or not at that moment and it will reduce the performance of your application.
The easiest way to achieve this to keep your member variable/ data into AppUi class as public or you can also define public method for get or set the value for the data member. This is the right way to achieve this, but if you project is in developing stage and you could not figure out the number of variable required at that time in future the size of AppUi class will increase. So I always prefer to create separate singleton class for it.
class CGlobalData : public CBase //make a singleton class
static CGlobalData* NewL();
static CGlobalData* NewLC();
void SetUserName(const TDesC& aName);
void SetPassword(const TDesC& aPassword);
static CGlobalData* iGlobalData = NULL;
if(iGlobalData == NULL)
iGlobalData = new (ELeave) CGlobalData();
return iGlobalData ;
if(iGlobalData == NULL)
iGlobalData = CGlobalData::NewLC();
CleanupStack::Pop(); // self;
void CGlobalData::SetUserName(const TDesC& aName)
void CGlobalData::SetPassword(const TDesC& aPassword)
TBool CGlobalData :: InstanceExist()
Now the instance of the class is created only once and you can create it anywhere in the code. When ever you will try to create an instance you will the get the pointer of already created instance.
e.g. First time call for the class CGlobalData.
CGlobalData* globalData = CGlobalData::NewL();
The above lines called first time so instance of CGlobalData class will be created.
Second time call:
static CGlobalData* gd = CGlobalData::NewL();
In this case the NewL function will return the pointer of already created CGlobalData object so name and password variable will contains UserOne and PasswordOne respectively.