   [.net] Global access
  Hello,In my current project I have a class that (among other things) handles time. Time is a central part and I need just about every other class to know about the time as well. How would you do in order to achieve this global access to time? I guess I could use a singleton but are there better ideas?Thanks   
  Time in what regard? DateTime is fine enough.Otherwise a plain old static seems suitable.   ;  Yeah a DateTime will do fine. Time is not related to the real time in any way it is just used for my simulator. For example by pressing a button you will proceed to the next day and depending on the time (and date) specific events will occur. That's why I need the current time to be accessible from almost anywhere.Will have another look at the design, perhaps it is bad.   ;Quote:Original post by daboThat's why I need the current time to be accessible from almost anywhere.emphasis mine.When you say "current time" do you mean actual real world time of day or in game time?For real world time, you can use DateTime.Now.Otherwise as Telastyn said, a static property/method should be fine.   ;  You could pass it as an argument to any function that needs it.   ;  In this case because time is everywhere I would use a static.But I would only allow a single class to make changes to time.theTroll   ;Quote:Original post by ChaosEngineQuote:Original post by daboThat's why I need the current time to be accessible from almost anywhere.emphasis mine.When you say "current time" do you mean actual real world time of day or in game time?For real world time, you can use DateTime.Now.Otherwise as Telastyn said, a static property/method should be fine.Sorry if I confuse you, I never care about real world time. When I wrote "current time" I meant current time in the simulator.Quote:Original post by TheTrollIn this case because time is everywhere I would use a static.But I would only allow a single class to make changes to time.theTrollYeah that is how I am using it, the other classes just read the time.   ;Quote:Original post by HodgmanYou could pass it as an argument to any function that needs it.I'd favor this, it doesn't feel right to have all your 'objects' depend on and know about some global time variable. I can imagine an instant replay situation in a racing game where you replay a crash that just happened before continuing the current state. Now you have to store the current time somewhere, replay the crash my rewinding and then incrementing the global time, and then resetting it again to 'now'.YMMV though.   