Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Why can not get the static variable value ????



Permlink Replies: 1 - Last Post: Mar 10, 2017 10:17 AM Last Post By: Antonio Estevez
et jon

Posts: 2
Registered: 9/6/01
Why can not get the static variable value ????
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 10, 2017 8:37 AM
Unit2.h

#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include

class MyValue
{
public:
MyValue();
~MyValue();
public:
static String m_Name;

};
#endif


Unit2.cpp


#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include

class MyValue
{
public:
MyValue();
~MyValue();
public:
static String m_Name;

};
#endif
//------------------------------------------------------

Unit1.cpp
//--------------------------------------------------------------------------------------
#include
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
#pragma resource ("*.LgXhdpiTb.fmx", _PLAT_ANDROID)

TForm3 *Form3;

MyValue gMyValue;
//---------------------------------------------------------------------------
__fastcall TForm3::TForm3(TComponent* Owner)
: TForm(Owner)
{

}
//---------------------------------------------------------------------------
void __fastcall TForm3::Button1Click(TObject *Sender)
{

String strName,SS,AA;

// MyValue gMyValue; Written in this can be made
strName = gMyValue.m_Name; <-- Can not get the value, but in Windows can, Android not, I feel Static is not the initial? ????

}
//---------------------------------------------------------------------------

Sample
https://drive.google.com/file/d/0B86ugIajjebUQWlBSFE2SFBGbW8/view?usp=sharing

Edited by: et jon on Mar 10, 2017 8:37 AM

Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Why can not get the static variable value ???? [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 10, 2017 10:17 AM   in response to: et jon in response to: et jon
El 10/03/2017 a las 17:38, et jon escribió:

// MyValue gMyValue; Written in this can be made
strName = gMyValue.m_Name; <-- Can not get the value, but in Windows can, Android not, I feel Static is not the initial? ????

}

Problem is the object creation order. The object gMyValue is created before the string MyValue::m_Name:
1) The MyValue constructor assign "123456" to m_Name
2) The String constructor assign "" to m_Name

You can see this adding a breakpoint in both lines:

	String MyValue::m_Name = _D("");   // (1)

and
	m_Name = _D("123456");             // (2)


and then run tha app. You will see that line (2) is executed before line (1)

To solve the problem:

String MyValue::m_Name= _D("123456");
 
MyValue::MyValue()
{
}
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02