glenn
glenn

Reputation: 11

Global variables in MonoTouch

I am trying to do something very simple, but it is proving to be very difficult. I have two or more views(XIBs) that need to get access to the same data structure. In this case its a long list. I have seen suggestions to use singletons and appdelegates, but none of that seems to work. I cant figure out how a singleton gets instantiated in a mono type project, so all I see is the class. And I have no clue how to use an appdelegate to do this.
lets not argue the drawbacks of global variables. I understand this. And I dont want to pass the list as a parameter to the constructor of the view. The rationale being that the list is very long. This will just end up creating alot of big lists that are all the same. Global is what I need. Can someone send me a simple example of how to access just one variable from any view. lets say its an int located in main and I have two views, view1 and view2 that need to access it.

Upvotes: 1

Views: 749

Answers (1)

Jason
Jason

Reputation: 89117

I use a static class to achieve something similar to this. It has properties for the different global values I need to track state, etc. I initialize the values when my app starts, then I can get/set them anywhere in my app by referring to the static class.

It's not necessarily the best solution, but it works and is simple to implement.

Upvotes: 1

Related Questions