Declaring global variables in c# windows application
Here's a good scenario illustrating this problem I've mentioned.. You've got a Seat class where a player sits.. Hand is a class that consists of two Cards. This is all pretty basic OO stuff so I'll skip past the details here. The problem is, my main window MainForm has a method for when the Paint event is fired off, to draw a bunch of players, for example. Now, I'm storing the list of players in my own public static class called GlobalVars.
DrawSeat ; The thing is, how is the DrawSeat method in the Seat class supposed to know how to draw this on the MainForm window, which is in no way at all associated with Seat?
CreateGraphics " and pass it into DrawSeat.. DrawSeat gc but this feels like a hack. I could put the drawing code in the MainForm class, but for OO stuff, it feels more correct to make a DrawSeat method in the Seat class. Can anyone recommend any good, solid C books? Saturday, September 9, PM. The question is, is the Seat class a part of the model or a part of the view? If it is part of the model then it should not have a method to perform any rendering.
In this case you could create your own class called, e. PokerDrawer might even delegate the drawing to a SeatDrawer class where you could have different types of 'seat' that render in a different way. One thing I would do is get rid of or rename a class called GlobalVars. From your description this sounds to me like it should be renamed PokerGame or something similar.
PokerGame contains abstractions for player, table seat etc and obviously the rules. Monday, September 11, AM. Ahh ok.. Yes, Seat is purely a model, so is Card and Hand.
None of these have anything to do with the view. As far as C books, I'm going to grab the Jesse Liberty book. It'll serve as a good refresher. Monday, September 11, PM. Wednesday, February 24, AM. Man thanks alot. I have been looking for that code for a complete day. Sunday, March 13, PM. I hope this article would have helped you out with the working process of variables.
This is a guide to Variables in C. Here we discuss what are variables in C , how do we declare variables, how do we initialize variables, and finally what are different types of variables in C with their examples. You may also look at the following articles to learn more —. Submit Next Question.
By signing up, you agree to our Terms of Use and Privacy Policy. Forgot Password? This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy.
By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy. Variables in C By Priya Pedamkar. Popular Course in this category. Course Price View Course. Free Software Development Course. Login details for this Free course will be emailed to you. Email ID. C is an object-oriented programming OOP language and does not support global variables directly.
The solution is to add a static class containing the global variables. Using a global variable violates the OOP concept a bit, but can be very useful in certain circumstances. You should also be careful when using multithreading, e. Make sure, that only one thread has access to the global static variable at a time - or add some kind of lock routine - to avoid conflicts.
The "static" keyword means the variable is part of the type and not an instance of the type! The following sample should illustrate the difference between a class and a static class:. Generic; using System.
0コメント