I have a structure named "Card" in Codes.cs file which is located in Codes folder of project. The source code of this structure is as follows:

struct Card
{

    //Suit that this card belongs to
    private CardSuit suit;

    public CardSuit Suit
    {
        get { return suit; }
        set { suit = value; }
    }


    ///The value of this card
    private CardValue value;

    public CardValue Value
    {
        get { return value; }
        set { this.value = value; }
    }

    public Card(CardSuit s, CardValue v)
    {
        this.suit = s;
        this.value = v;
    }

}
enum CardSuit : int { Clubs, Spades, Hearts, Diamonds };
enum CardValue : int { Ace, King, Queen, Jack, Ten, Nine, Eight, Seven, Six, Five, Four, Three, Two };

I have anathor class file named "Class1.cs" which is outside the Codes folder. The source of the class is as follows:

public class Class1
    {
        Card card = new Card();
        
    }

In this class file i created an instance of "Card" structure successfully as shown in above code. But i cannot access the properties of structure like this(as shown bleow):

card.Suit = CardSuit.Clubs;

Please Help,
Thanks

Recommended Answers

All 8 Replies

The scope of the struct is private therefore you cannot access its' fields in another class. Write public struct card instead.

You've something wrong it should work right.

Ema005 who said it's private the default is internal and it's!, and they can use it in the same assembly.
I take the code and run it, no problem.

I have anathor class file named "Class1.cs" which is outside the Codes folder. The source of the class is as follows:

You probably need to add a "using appName.Codes;" if you have folders with codes in it. It is likely the code is in another namespace

@Scott

In this class file i created an instance of "Card" structure successfully as shown in above code. But i cannot access the properties of structure like this(as shown bleow):

i created an instance of "Card" structure successfully as shown in above code
I bet there's no problem, I believe they have problem in something else.

Well, Guys and Girls
The problem was not with the code. The problem was something else.

I tried to access the value of the Card Struct directly in the class definition: like this

public class Class1
    {
        Card card = new Card(CardSuit.Clubs,CardValue.Ace);       
        card.suit = CardSuit.Clubs;//Wrong method
      

    }

but it was supposed to be accessed in side method or property like this:

public class Class1
    {
        Card card = new Card(CardSuit.Clubs,CardValue.Ace);
        

        public void SetHands()
        {          
           card.suit = CardSuit.Clubs;  
        }

    }

Anyways,
That was crazy of me
I solved the problem.
Thanks to you all

Ah, the problem was that you cannot write code inside if a class ... You have to write it inside of a method. You can only declare members, new classes, interfaces, etc inside of a class -- and you can call their contructors for initial assignments but you can't call other methods.

ya i got it.

That was pertty basic. I missed to notice this fact and end up wasting hours of time until one of my coworker noticed this and we solved the problem.

Thanks for the time you took to look at my thread.

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.