Jump to content
  • Advertisement
Sign in to follow this  
gusgah36

C# - Can't access a class

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I have start working on a simple card game and now I have a problem accessing a class from another. What I can't do is to access Cards from CardGame. How do i fixa that?
using System;
using System.Drawing;
using System.Windows.Forms;

namespace CardGame 
{
	//////////////////////////////////////////////////////////////////////////
	// Cards Class ///////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////
	public class Cards
	{	
		/*
		//Creates an array with all the cards
		private int[ ] cards = new int[ ]{1,  2,  3,  4,  5,  6,  7,  8,  9, 10,
										 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
										 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
										 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
										 41, 42, 43, 44, 45, 46, 47, 48, 49, 40,
										 51, 52,};
		*/
		private struct Card
		{
			public byte CardNum; //Card number
			public byte Color;   //Card color
			public byte X, Y;    //Position; x, y 	
		} 
        
		private Card[ ] arrayCards = new Card[51];
		
		//Constructor for card
		public Cards()
		{
			for(byte color = 0; color <= 4; color++)
			{
				for(byte numCards = 0; numCards <= 13; numCards++)
				{
					arrayCards[color * numCards].Color = color;
					arrayCards[color * numCards].CardNum = numCards;
					arrayCards[color * numCards].X = 0;
					arrayCards[color * numCards].Y = 0;
				}
			}
		}

		public int getCardNum(byte num)
		{
			return arrayCards[num].CardNum;
		}
	}
	//////////////////////////////////////////////////////////////////////////
	// CardGame Class ////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////
	class CardGame : System.Windows.Forms.Form
	{
		public Cards game = new Cards(); //Creates an instance of Cards
		
		static int Main()
		{
			
			return 0;
		}
	}
}

Share this post


Link to post
Share on other sites
Advertisement
My guess is you are having a fence post error with this line:

private Card[ ] arrayCards = new Card[51];

that should be "= new Card[52];" since you are creating an array with
52 cards, no?

Share this post


Link to post
Share on other sites
Also, why don't you just have a loop assign values to that array? Because hardcoding that many in just looks ugly.

Share this post


Link to post
Share on other sites
No, I don't think that is the problem becouse an array starts on 0 (correct me if I'm wrong).

Fora an exempel on my problem: When I write "game." nothing pops up, for exempel "game.getCardNum()"

Share this post


Link to post
Share on other sites
Quote:
Also, why don't you just have a loop assign values to that array? Because hardcoding that many in just looks ugly.


I don't.
I have /* before and */ after.

Share this post


Link to post
Share on other sites
Quote:
Original post by gusgah36
No, I don't think that is the problem becouse an array starts on 0 (correct me if I'm wrong).

Fora an exempel on my problem: When I write "game." nothing pops up, for exempel "game.getCardNum()"



No you have to declare the TOTAL amount of items in your array when you initialize it. 51 would be the max value when you are REFERENCING objects in your array.

Share this post


Link to post
Share on other sites
I put that code into a c# .net 2.0 class library project and there didnt seem to be any problems. Maybe you can post the error you are getting.

Share this post


Link to post
Share on other sites
The problem is that I don't get any error. There is just nothing happening in Visual Studio when I write "game." in the main-function. Normaly a list with other functions would popup.

Share this post


Link to post
Share on other sites
Quote:
Original post by gusgah36
The problem is that I don't get any error. There is just nothing happening in Visual Studio when I write "game." in the main-function. Normaly a list with other functions would popup.

Main is a static function of the CardGame class, while game is an instance field. Instance fields do not exist inside the static portions of a class, so you can't access them. Visual Studio recognizes this and correctly prevents you from trying to access instance fields. You could change game to be static, or else just create an instance of it directly inside the class, e.g.


// ... in Main

Cards c = new Cards();
byte bIndex = 0; // first card in the deck
int i = c.getCardNum(bIndex);
Console.WriteLine(i); // prints integer value of first card


I realize you're probably new, but there's a few other problems with your code too. For starters, it doesn't make much sense to index your array with a byte value.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!