Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top