title: Store data and global variables using the Application object
body: <p>In Xamarin, I am wanting to store data and global variables using the Application object.</p>
  
  <p>I looked at this resource for this code:
  <a href="http://www.helloandroid.com/tutorials/maintaining-global-application-state" rel="nofollow noreferrer">http://www.helloandroid.com/tutorials/maintaining-global-application-state
  </a></p>
  
  <p>Here is my code:</p>
  
  <pre><code>    public class HelloApplication : Application 
      {
          private int GlobalVariable = 1;
  
          public int GetGlobalVariable()
          {
              return GlobalVariable;
          }
  
          public void SetGlobalVariable(int GlobalVariable)
          {
              this.GlobalVariable = GlobalVariable;
          }
      }
  </code></pre>
  
  <p>I am trying to reference the class using this code:</p>
  
  <pre><code>    ((HelloApplication)GetApplication()).setGlobalVariable(10);
      int variable=((HelloApplication)GetApplication()).getGlobalVariable();
  </code></pre>
  
  <p>When I build the application I am getting this error for both the above lines of code:</p>
  
  <blockquote>
    <p>The name 'GetApplication' does not exist in the current context</p>
  </blockquote>
  
  <p>Can I please have some help to get this code working?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is my code:</p>
  
  <pre><code>HelloApplication state = ((HelloApplication) this.ApplicationContext);
  state.SetGlobalVariable(10);
  int globalVariable = state.GetGlobalVariable ();
  
  Toast.MakeText (this, "Global variable " + globalVariable, ToastLength.Short).Show ();
  </code></pre>
  
  <p>This is the error I am getting:</p>
  
  <blockquote>
    <p>UNHANDLED EXCEPTION: System.InvalidCastException: Cannot cast from source type to destination type.</p>
  </blockquote>
  
  <p>Can I please have some help to get this working?</p>