1

I'm pretty sure this is a duplicate, but I've been unable to find a fix for this after a few hours of searching/trying.

I'm not an advanced programmer, but I have a decent amount of C++ experience. I'm trying to learn C# and having trouble with very basic syntax, especially for just accessing other classes. I've been looking for simple examples for a while, and overwhelmingly, everything I find seems to use one HUGE class, wherein the main method is used, so those examples haven't been very helpful.

I want to develop a solution with multiple .cs files (one class in each), and another .cs file containing the main method that I'll use for testing. My solution is named DIVAT. I have a Dealer.cs file with the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DIVAT
{
    public class Dealer
    {
        public List<Tuple<int, string>> deck;

        Dealer()
        { // default constructor
            Console.Out.WriteLine("Default constructor called. (Dealer class)");

            string [] suitValue = {"c", "d", "h", "s"};

            for(int i = 2; i <= 14; i++){
                for(int j = 0; j <= 3; j++){
                    deck.Add(new Tuple<int, string>(i, suitValue[j]));
                }            
            }
        }

        ~Dealer() 
        {// destructor
            Console.Out.WriteLine("Destrcutor called. (Dealer class)");
        }

        Tuple<int, string> Dealer.getCard(int cardNum)
        {// getter
            return deck[cardNum];
        }
    }
}

Now I'm just trying to test this in another file, Program.cs. I'm hitting 2 bugs and can't figure out why. I am having a lot of trouble just trying to initialize my Dealer class. Also, I just want to test a getter function in my Dealer class.

I use to have a lot more static and private keywords throughout, but took those out as I was hitting bugs.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DIVAT
{
    class Program
    {
        static void Main(string[] args)
        {
            Dealer dealer = new Dealer();
            // inaccessible due to it's protection level...

            for (int i = 0; i <= 52; i++) {                
                Console.Out.WriteLine(dealer.getCard(i));
                // does not contain a definition for getCard...
            }


        }
    }
}

Sorry for such the basic questions, but I've been scouring the internet and trying different ways to fix this and have been unsuccessful. I feel like once I get past these few bugs, I should be able to convert a lot of my other code relatively painlessly.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
ch-pub
  • 1,664
  • 6
  • 29
  • 52
  • 2
    As a Side note, you should use a Property for `deck` (e.g. `public List> deck { get; private set; }`. I also added an access modifier to set as `private` because it appears that in this case, you probably don't want any class outside of dealer altering the deck :) – Erik Philips May 11 '14 at 00:57
  • 1
    To extend my comment about converting your [field to a property](http://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property-in-c), if you every need to add code to the event a method *gets* or *set* a value, it simply can't be done as a field. If it is a property, then you can change how the get/set works, without impacting any other code relying on getting/setting the value (non-breaking change vs breaking change). – Erik Philips May 11 '14 at 01:04

1 Answers1

5

Your constructor is implicitly private and since you provide zero public constructors, it cannot instantiate your class, even though the class itself is public..

You need to specify that it is public.

public Dealer() { }

As for your second question, you don't need to tell your methods that they belong to the class. They are already aware. Change your method signature like so:

public Tuple<int, string> GetCard(int cardNum)
{
    // getter
    return deck[cardNum];
}

Note that now the method is public and that we are scoped properly. In addition, note the PascalCasing on your method name. This is the appropriate naming convention for method names in C#.

Also, on another note, since C# is a managed language, you probably don't need your destructor.

David L
  • 32,885
  • 8
  • 62
  • 93
  • Thank you! Then I get an error on my getCard function: "'Dealer' in explicit interface declaration is not an interface". I read that C# uses . instead of :: to declare functions. Am I using it incorrectly? This happens even if I use public in front of that getCard function. – ch-pub May 11 '14 at 00:51
  • 1
    @Clark In this case you don't need to tell your Dealer class to include the getCard function. It is already properly scoped just by existing in the class. Sorry, was slow to update an answer for your second question. Please see my most recent update. – David L May 11 '14 at 00:53
  • 1
    [When a class-member-declaration does not include any access modifiers, private is assumed.](http://msdn.microsoft.com/en-us/library/aa645626(v=vs.71).aspx) – Erik Philips May 11 '14 at 00:55
  • @ErikPhilips Thank you for the link :). I knew it was out there somewhere. Yes, this is exactly what I am referring to. – David L May 11 '14 at 00:56
  • @DavidL If you don't mind, is there some problem with the loop in my main method that is easy for you to notice? Thanks! – ch-pub May 11 '14 at 00:58
  • 1
    @Clark Erik already answered it, but I highly recommend creating another question so that he can move his answer to that question and receive credit for it. – David L May 11 '14 at 01:02