views:

653

answers:

2

I'm woking on a card game for my Intro to OOP paper. The game itself meets the specifications but I am now playing around with it for my own gratification and learning.

I have a card class that contains and image, its rank and suite. On the GUI I use picture boxes to display the image stored in the respective cards (which are stored in a card array in the deck class) so,

cardPictureBox1.Image = card1.show();
cardPictureBox2.Image = card2.show();
cardPictureBox3.Image = card3.show();
...
etc

Would it be possible to make the card class an inherit the PictureBox control so what is on-screen is "actually" an instance of the card calss (rather than a box that hold the image value of it) which would dramaticall reduce he amount of hoops one would have to leap through to attain the cards other relevant info.

+2  A: 

Would it be possible to make the card class an inherit the PictureBox control so what is on-screen is "actually" an instance of the card calss (rather than a box that hold the image value of it) which would dramaticall reduce he amount of hoops one would have to leap through to attain the cards other relevant info.

Yes, but would you want to? No. It couples your business logic and model very tightly to your GUI layer.

If you're writing a paper on OOP, you should encourage good programming habits and discourage cutting corners. It would be better for you to whip a quick MVC, so that your GUI automagically updates itself whenever you make a change to your model.

Juliet
I think he's writing a paper *for* an Intro to OOP class that he's taking, as opposed to writing a paper to *teach* the subject.
MusiGenesis
+3  A: 

Instead of having a Card class, you can create a UserControl (named Card or ucCard or whatever) that inherits from PictureBox (instead of inheriting from UserControl). The easiest way to do this in C# is to add a UserControl with your desired name, and then in the code change the top line from

public partial class ucCard : UserControl

to

public partial class ucCard : PictureBox

Your ucCard control will then have all the properties of a PictureBox (including Image, where you would store the card's Bitmap). When you build your project, the compiler will barf on a line that references AutoScaleMode - just delete this line and rebuild.

You can then add any additional properties and methods the cards require, like suit and rank and Bitmaps for the front and back of the card (the back could be static so that all cards would share it), and maybe a Flip() method to switch between the front and back images.

As far as OOP goes, the forgotten part of the holy trinity seems to be Encapsulation. In this case, since a card is a visual element in the UI that the user interacts with, it makes perfect sense to encapsulate it as a UserControl. As you've already noticed, this will make the application simpler and your life easier.

MusiGenesis
Thanks both of you! I won't be handing it in with any inheritance in place; I'm just curious about it and what changes it would make to the game (line count, performance, etc) and of course how to do it.
Josh King