8 Replies - 1210 Views - Last Post: 08 December 2012 - 11:07 AM Rate Topic: -----

#1 aortell24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-December 12

attaching an image to a class object

Posted 07 December 2012 - 05:37 PM

Hello all, I am working on a program for my class it is a card shuffling gui program any way the problem I am having is attaching the card face image to the class object which is card.I am not looking for any one to write my code for me. If some one could just take a look at my code and let me know if you see the problem. I have the images in the resources folder in my project. I just can't get the image to display. Thanks alot.
 namespace QuizTwoOne
{
    class Deck
    {
        public Card[] spades = new Card[13];  
        public Card[] clubs = new Card[13];
        public Card[] diamonds = new Card[13];
        public Card[] hearts = new Card[13];
        private Card[] deck = new Card[52];

        private Random rand = new Random();

        public Deck() 
        {
            for (int i = 0; i < 13; i++)
            {
                spades[i] = new Card(i+1, 's');
                clubs[i] = new Card(i+1, 'c');
                diamonds[i] = new Card(i+1, 'd');
                hearts[i] = new Card(i+1, 'h');
            }
        }

        public Card[] CreateDeck()
        {
            Card[] deck = new Card[52];
            for (int i = 0; i < 13; i++)
            {
                spades[i] = new Card(i + 1, 's');
                clubs[i] = new Card(i + 1, 'c');
                diamonds[i] = new Card(i + 1, 'd');
                hearts[i] = new Card(i + 1, 'h');
            }
          

            deck = Merge(deck, spades);
            deck = Merge(deck, clubs);
            deck = Merge(deck, hearts);
            deck = Merge(deck, diamonds);

            return deck;
            
        }
        public Card[] Shuffle(Card[] deck)
        {
            Card[] retVal = new Card[52];
            
            

            //shuffle spades
            for(int i = spades.Length -1; i > 0; i--)
            {
                int n = rand.Next(i + 1);
                Swap(spades, i, n);
            }

          
            return retVal;
        }

        private void Swap(Card[] numbers, int index1, int index2)
        {
            Card temp = numbers[index1];
            numbers[index1] = numbers[index2];
            numbers[index2] = temp;
        }
        public Card[] Merge(Card[] destination, Card[] addition)
        {
            if(destination == null)
            {
                throw new ArgumentNullException("Destination array is Null");
            }
            if(addition == null)
            {
                throw new ArgumentNullException("Addition Array is Null");
            }
           
            Type firstType = destination.GetType();
            Type secondType = addition.GetType();
            
            if(firstType != secondType)
            {
                throw new InvalidOperationException("Type mismatch");
            }
            Hashtable table = new Hashtable();
            ArrayList items = new ArrayList();
            MergeHelper(destination, table, items);
            MergeHelper(addition, table, items);
            return (Card[])items.ToArray(firstType.GetElementType());


        }
        public void MergeHelper(Card[] array, Hashtable table, ArrayList items)
        {
            for(int i =0; i < array.Length; i++)
            {
                Card item = array[i];
                if(!table.Contains(item))
                {
                    table.Add(item, 1);
                    items.Add(item);
                }
            }
        }
    }
}

namespace QuizTwoOne
{
    public partial class Form1 : Form
    {
        Card card = new Card();
        Card[] deckOfCards = new Card[52];
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            deckOfCards = card.CreateSuiteArray();
          
                

        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            Card[] spades = card.CreateSuiteArray();
            string imageName = card.Value + card.Suites + ".gif";
            for(int i =0; i < spades.Length; i++)
            {
                spades[i].CardImage =(Image)Properties.Resources.ResourceManager.GetObject(imageName); 
                  
            }
            for(int i =0; i < spades.Length; i++)
            {
                textBox1.Text +=Environment.NewLine + spades[i].ToString();
                //textBox1.Text += Environment.NewLine + spades[i].CardImage;
               
            }
           spades[0].CardImage = (Image)Properties.Resources.ResourceManager.GetObject("10c.gif");
           pictureBox2.Image = spades[0].CardImage;
                
        }
    }
}




 namespace QuizTwoOne
{
    namespace QuizTwoOne
{
    class Card 
    {
        private char _suites;
        private int _value;
        private Image _cardImage;
        public Card[] spades = new Card[13];
        public Card[] clubs = new Card[13];
        public Card[] diamonds = new Card[13];
        public Card[] hearts = new Card[13];
        private Card[] deck = new Card[52];
        private string imageName;

        public Card() {}
        public Card( int value, char suite)
        {
            this.Value = value;
            this.Suites = suite;
            imageName = Value + Suites + ".gif";
            CardImage = (Image)Properties.Resources.ResourceManager.GetObject(imageName);
        }

        public char Suites
        {
            get
            {
                return _suites;
            }
            set
            {
                try
                {
                    
                    _suites = value;
                }
                catch(Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public int Value
        {
            get
            {
                return _value;
            }
            set
            {
                try
                {
                    _value = value;
                }
                catch(Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public Image CardImage
        {
            get
            {
                return _cardImage;
            }
            set
            {
                try
                {
                    _cardImage = value;
                }
                catch(Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }

       
        public Card[] CreateSuiteArray()
        {
            for (int i = 0; i < 13; i++)
            {
                spades[i] = new Card(i + 1, 's');
                clubs[i] = new Card(i + 1, 'c');
                diamonds[i] = new Card(i + 1, 'd');
                hearts[i] = new Card(i + 1, 'h');
            }
            return spades;
        }
        public Card[] CreateDeckOfCards()
        {
            Card[] deck = new Card[52];
            for (int i = 0; i < 13; i++)
            {
                spades[i] = new Card(i + 1, 's');
                clubs[i] = new Card(i + 1, 'c');
                diamonds[i] = new Card(i + 1, 'd');
                hearts[i] = new Card(i + 1, 'h');
            }
            deck = Merge(deck, spades);
            deck = Merge(deck, clubs);
            deck = Merge(deck, hearts);
            deck = Merge(deck, diamonds);

            return deck;

        }
        public Card[] Merge(Card[] destination, Card[] addition)
        {
            if (destination == null)
            {
                throw new ArgumentNullException("Destination array is Null");
            }
            if (addition == null)
            {
                throw new ArgumentNullException("Addition Array is Null");
            }

            Type firstType = destination.GetType();
            Type secondType = addition.GetType();

            if (firstType != secondType)
            {
                throw new InvalidOperationException("Type mismatch");
            }
            Hashtable table = new Hashtable();
            ArrayList items = new ArrayList();
            MergeHelper(destination, table, items);
            MergeHelper(addition, table, items);
            return (Card[])items.ToArray(firstType.GetElementType());


        }
        public void MergeHelper(Card[] array, Hashtable table, ArrayList items)
        {
            for (int i = 0; i < array.Length; i++)
            {
                Card item = array[i];
                if (!table.Contains(item))
                {
                    table.Add(item, 1);
                    items.Add(item);
                }
            }
        }
        public override string ToString()
        {
            return String.Format("This card has a value of {0} and a a suite of {1} and uses {2} as an image!", Value, Suites, CardImage);
        }
    }

namespace QuizTwoOne
{
    public partial class Form1 : Form
    {
        Card card = new Card();
        Card[] deckOfCards = new Card[52];
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            deckOfCards = card.CreateSuiteArray();
          
                

        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            Card[] spades = card.CreateSuiteArray();
            string imageName = card.Value + card.Suites + ".gif";
            for(int i =0; i < spades.Length; i++)
            {
                spades[i].CardImage =(Image)Properties.Resources.ResourceManager.GetObject(imageName); 
                  
            }
            for(int i =0; i < spades.Length; i++)
            {
                textBox1.Text +=Environment.NewLine + spades[i].ToString();
                //textBox1.Text += Environment.NewLine + spades[i].CardImage;
               
            }
           spades[0].CardImage = (Image)Properties.Resources.ResourceManager.GetObject("10c.gif");
           pictureBox2.Image = spades[0].CardImage;
                
        }
    }
}


This post has been edited by tlhIn`toq: 07 December 2012 - 06:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: attaching an image to a class object

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,658
  • Joined: 02-June 10

Re: attaching an image to a class object

Posted 07 December 2012 - 06:28 PM

I think you need to go through your code again after getting some sleep.

You seem to be confusing the idea of a card and a deck of cards. Why does your card class have 4 suit arrays as well as a deck array? You have WAY, WAY overcomplicated the card suite and totally muttled the functionality of a card and a deck of cards.

A card is one thing. I should have just a few properties:

class card
{
  public int value;
  public string suit;
  public image faceArt;
}


There is a tutorial on objects/classes linked in my signature block - maybe it will help.
Was This Post Helpful? 1
  • +
  • -

#3 aortell24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-December 12

Re: attaching an image to a class object

Posted 07 December 2012 - 07:18 PM

Thanks for the reply I didn't mean to post that deck class I moved every thing over to the card class and just haven't deleted it yet.The reason I have an array for each suite is because I need to be able to sort them by suites and the deck is to have a separate shuffled deck of cards with all suites.I thought it would be easy to keep the suites separate because the user will click on the suite and the cards will be sorted by the selected suite.I just can't get the image to display.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,658
  • Joined: 02-June 10

Re: attaching an image to a class object

Posted 08 December 2012 - 06:51 AM

But none of that belongs in the card class.

When you get it cleaned up and separated repost the classes and we'll see what direction to point you in next.
Was This Post Helpful? 1
  • +
  • -

#5 aortell24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-December 12

Re: attaching an image to a class object

Posted 08 December 2012 - 10:29 AM

Here it is cleaned up sorry about that. I just cant figure out why line 39 wont work I even used the exact name of the image instead of the variable name.Thanks guys.
namespace QuizTwoOne
{
    public class Card 
    {
        private char _suites;
        private int _value;
        private Image _cardImage;
       
        public Card( int value, char suite)
        {
            this.Value = value;
            this.Suites = suite;
        }

        public char Suites
        {
            get
            {
                return _suites;
            }
            set
            {
                try
                {
                    
                    _suites = value;
                }
                catch(Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public int Value
        {
            get
            {
                return _value;
            }
            set
            {
                try
                {
                    _value = value;
                }
                catch(Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public Image CardImage
        {
            get
            {
                return _cardImage;
            }
            set
            {
                try
                {
                    _cardImage = value;
                }
                catch(Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }
       
        public override string ToString()
        {
            return String.Format("This card has a value of {0} and a a suite of {1} and uses {2} as an image!", Value, Suites, CardImage);
        }
    }
}
public partial class Form1 : Form
    {
       
        Card[] deckOfCards = new Card[52];
        Card[] spades = new Card[13];
        Card[] clubs = new Card[13];
        Card[] diamonds = new Card[13];
        Card[] hearts = new Card[13];
               
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            
        }

        private void button1_Click(object sender, EventArgs e)
        {
           Card card = new Card(1, 's');
           string imageName = card.Value + card.Suites + ".gif";
                    
           pictureBox2.Image = (Image)Properties.Resources.ResourceManager.GetObject("10c.gif");
                
        }

        public Card[] CreateSuiteArray()
        {
            for (int i = 0; i < 13; i++)
            {
                spades[i] = new Card(i + 1, 's');
                clubs[i] = new Card(i + 1, 'c');
                diamonds[i] = new Card(i + 1, 'd');
                hearts[i] = new Card(i + 1, 'h');
            }
            return spades;
        }
        public Card[] CreateDeckOfCards()
        {
            Card[] deck = new Card[52];
            for (int i = 0; i < 13; i++)
            {
                spades[i] = new Card(i + 1, 's');
                clubs[i] = new Card(i + 1, 'c');
                diamonds[i] = new Card(i + 1, 'd');
                hearts[i] = new Card(i + 1, 'h');
            }
            deck = Merge(deck, spades);
            deck = Merge(deck, clubs);
            deck = Merge(deck, hearts);
            deck = Merge(deck, diamonds);

            return deck;
        }
        public Card[] Merge(Card[] destination, Card[] addition)
        {
            if (destination == null)
            {
                throw new ArgumentNullException("Destination array is Null");
            }
            if (addition == null)
            {
                throw new ArgumentNullException("Addition Array is Null");
            }

            Type firstType = destination.GetType();
            Type secondType = addition.GetType();

            if (firstType != secondType)
            {
                throw new InvalidOperationException("Type mismatch");
            }
            Hashtable table = new Hashtable();
            ArrayList items = new ArrayList();
            MergeHelper(destination, table, items);
            MergeHelper(addition, table, items);
            return (Card[])items.ToArray(firstType.GetElementType());
        }
        public void MergeHelper(Card[] array, Hashtable table, ArrayList items)
        {
            for (int i = 0; i < array.Length; i++)
            {
                Card item = array[i];
                if (!table.Contains(item))
                {
                    table.Add(item, 1);
                    items.Add(item);
                }
            }
        }
    }



View Postaortell24, on 08 December 2012 - 10:27 AM, said:

Here it is cleaned up sorry about that. I just cant figure out why line 39 wont work I even used the exact name of the image instead of the variable name.Thanks guys.

I just cant figure out why line 39 wont work: Line 104 sorry.
Was This Post Helpful? 0
  • +
  • -

#6 aortell24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-December 12

Re: attaching an image to a class object

Posted 08 December 2012 - 10:43 AM

Never mind I figured it out thanks for the help. in line 104 instead of the .resources.resourceManager.getObject("10c.gif");

I used .resources.10c; now I just need to do that dynamically for 52 images.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,658
  • Joined: 02-June 10

Re: attaching an image to a class object

Posted 08 December 2012 - 11:02 AM

Yes, you want the resource name not the file name including extension.

But before you bang out a lot of repetitive code maybe you should re-read that method tht line 104 is in, because its not doing a lot. LInes 101 and 102 have no purpose. You don't seem to really be building an object with anything there. Not sure what you think that's doing.
Was This Post Helpful? 0
  • +
  • -

#8 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: attaching an image to a class object

Posted 08 December 2012 - 11:03 AM

May I propose using enums for suites and values. Using char and int is not strongly typed, and could present trouble.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,658
  • Joined: 02-June 10

Re: attaching an image to a class object

Posted 08 December 2012 - 11:07 AM

View Postlucky3, on 08 December 2012 - 12:03 PM, said:

May I propose using enums for suites and values. Using char and int is not strongly typed, and could present trouble.


In a more real world app there are lots of things like this that would make the program a lot better

Quote

001namespace QuizTwoOne


But as this is school homework I'm sure they can only use the limited amount of stuff they have covered so far. Enums probably don't come until the 3rd year of a college course where they are included by week 3 of any "Learn C# in 30 days" type book.

If we were to pull this all apart we would replace arrays with List<> and so on.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1