Member Avatar for humorousone

Kind of stuck here.
I'm porting over a section of code from Java to C#, and I'm having an issue with method permissions constructors.

Early in my code, i have this class..

public class TCard
{
    int suit;
    int rank;
}

and later i've got this method, which throws a bunch of errors.

Identifiers highlighted with the double asterixes throw the "is innaccessible due to its protection level" error.
I've tried rebuilding my solution, and this hasn't worked.

public void loadDeck(TCard[] deck)
{
    int count;
    for (count = 1; count <= 52; count++)
    {
        deck[count] = new TCard();
        deck[count].**suit** = 0;
        deck[count].**rank** = 0;
    }

    String lineFromFile;
    string currentFile = "";
    currentFile = new AQAReadTextFile2014();
    currentFile.openTextFile("deck.txt");
    count = 1;
    lineFromFile = currentFile.readLine();

    while (lineFromFile != null)
    {
        deck[count].**suit** = Integer.parseInt(lineFromFile);
        lineFromFile = currentFile.readLine();
        deck[count].**rank** = Integer.parseInt(lineFromFile);
        lineFromFile = currentFile.readLine();
        count = count + 1;
    }

    currentFile.closeFile();

}

There's probably some kind of blatant error here.
This is my first time porting from C# to Java, so forgive me if I've gotten myself confused between the two.

Recommended Answers

All 6 Replies

Default access is private, so making your properties public will solve this.

public class TCard
{
    public int suit;
    public int rank;
}
public class TCard
{
    int suit;
    int rank;
}

The compiler considers this as being:

public class TCard
{
    private int suit;
    private int rank;
}

So proceed as Pritaeas pointed out.

I feel default access specifier of java is more inclined to the internal access specifier of c#.

Member Avatar for humorousone

Ok, so I tried to change my class properties as Pritaeas suggested, but this didn't work.

I spoke to my tutor, and he suggested using (get;set;). It fixed the issue, at least in an example he showed me.

Would this be an appriopriate use of (get;set;)?

Ok, so I tried to change my class properties as Pritaeas suggested, but this didn't work.

It should work, but it's hard to tell why not without seeing a complete bare bones example that exhibits the problem.

Would this be an appriopriate use of (get;set;)?

Yes. In fact, that would be my default choice for publicly accessible data items:

public class TCard
{
    public int Suit { get; set; }
    public int Rank { get; set; }
}

Though in this case I'd prefer to also write classes or enumerations representing suit and rank, since just using int means you'd end up having to validate and error check invalid values.

Member Avatar for humorousone

I should probably have mentioned, the program I'm porting is a skeleton program used during a computing exam, so the closer to the original I can get, the better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.