locked
Property in App object.

    Question

  • Hello,

    I have added a property of type string in the App() object so that I can have global access. But I'm not sure how to access the App's property from other classes. How to get a reference to the App object?

    public:
    	App();
    	virtual void OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ args) override;
    
    	property String^ StringTest
    		{ String^ get() { return sTest;}
    	          void    set   (String^ value) {sTest=value;}
    		}
    Thanks for any help.

    Sunday, November 17, 2013 5:14 AM

Answers

  • Hi Thanik,

    You could use the property in another class like this:

    defination of the class:

    namespace MyNamespace{
    		public ref class MyClass sealed{
    		public:
    			property int MyData{
    				int get(){
    					return _mydata;
    				}
    				void set(int value){
    					_mydata = value;
    				}
    			}
    		private:
    			int _mydata;
    		};
    	}

    How to use it :

    using namespace App7::MyNamespace;
    
    MainPage::MainPage()
    {
    	InitializeComponent();
    	MyClass^ myclass = ref new MyClass();
    	int destinationdata = myclass->MyData;
    }
    

    That is the procedure.

    I hope it is helpful!

    Regards!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 28, 2013 2:47 AM
    Moderator

All replies


  • This is in VB, but it shouldn't be difficult for you to translate it to C# (there are translators online that you can paste into).

    In a nutshell you create a reference to App, and then you access your properties via that reference in the same way you'd access properties from any other object.

    You need a reference to your App object in your other class(es), then you can use it to access the properties:

    Dim CurrentApp as App = App.Current


    To use your property:

    Dim stringFromAppClass as String = CurrentApp.StringTest

    [or]

    CurrentApp.StringText = "Hello from your other class"



    I'm a self-taught noob amateur. Please take this into account when responding to my posts or when taking advice from me.

    Sunday, November 17, 2013 6:17 AM
  • I'm looking for specific example in C++. Thanks
    Sunday, November 17, 2013 6:39 AM
  • Hi Thanik,

    You could use the property in another class like this:

    defination of the class:

    namespace MyNamespace{
    		public ref class MyClass sealed{
    		public:
    			property int MyData{
    				int get(){
    					return _mydata;
    				}
    				void set(int value){
    					_mydata = value;
    				}
    			}
    		private:
    			int _mydata;
    		};
    	}

    How to use it :

    using namespace App7::MyNamespace;
    
    MainPage::MainPage()
    {
    	InitializeComponent();
    	MyClass^ myclass = ref new MyClass();
    	int destinationdata = myclass->MyData;
    }
    

    That is the procedure.

    I hope it is helpful!

    Regards!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 28, 2013 2:47 AM
    Moderator