Error concerning a sorted list not being accessible due to its protection level

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LouTheNewb
    New Member
    • Oct 2012
    • 1

    Error concerning a sorted list not being accessible due to its protection level

    I'm very new to c# and programming in general, so excuse my poor knowledge.
    For school we have to create a blackjack game using windows form. I had to store the images of each card into a sorted list so i created a class called cardList and created a constructor which contained the the sorted list called cards. So it looks kinda like this:
    Code:
    public class cardList : Form1
        {
            SortedList cards = new SortedList();
    
            public cardList()
            {
                cards.Add(Image.FromFile("C:\\Users\\Lulu\\Documents\\cards\\club02.png"), 0);
                cards.Add(Image.FromFile("C:\\Users\\Lulu\\Documents\\cards\\club03.png"), 1);
                cards.Add(Image.FromFile("C:\\Users\\Lulu\\Documents\\cards\\club04.png"), 2);
                cards.Add(Image.FromFile("C:\\Users\\Lulu\\Documents\\cards\\club05.png"), 3);
    ...
    // the ellipses represents the other 49 cards 
       }
    }
    Now I'm trying to call one of the cards to put into a picture box using a method called dealDealer in a different class called Form1:
    Code:
    public void dealDealer(int cardsDealt)
            {
                Random number = new Random();
                int randomCard = number.Next(1, 52);
    
                for (int b = 0; b < cardsDealt;)
           
                    if (randomCard == cardNumber) 
                    {
                        cardList c = new cardList();
                        dealer2.Image = c.cards.GetByIndex(cardNumber);
                    }
                    
            }
    There's probably a few other errors, I'm still trying to figure this whole c# thing out. All i need right now is for some one to please explain to me why the error tells me (on the line that contains c.cards.GetByIn dex(cardNumber) ;) that cards is inaccessible due the its protection level.

    Much appreciated
    Last edited by Rabbit; Oct 7 '12, 10:35 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    The default access for members is private. What you need is a public getter method that can be accessed from outside the class to return the correct image.

    While you could just make the member public, it is not a recommended practice.

    Comment

    Working...