Jump to content

Confused beginner's question!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
andersh

andersh

    Newbie

  • Members
  • Pip
  • 2 posts
Hello,
I learned a bit of C# a few years ago but never used it again and forgot most of what I'd learned. I'm trying it out again and am a bit confused about how it works. I am writing a very simple card game using Sharpdevelop and would like a bit of help. Could you pleas let me know where I'm going wrong in the following:

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Windows.Forms;


namespace CardGame

{

	/// <summary>

	/// Description of MainForm.

	/// </summary>

	public partial class MainForm : Form

	{

		public MainForm()

		{

			//

			// The InitializeComponent() call is required for Windows Forms designer support.

			//

			InitializeComponent();

			

			//

			// TODO: Add constructor code after the InitializeComponent() call.

			//

			

			

			cardCount cc = new cardCount();

			CardStatus Card1 = new CardStatus();

			Card1.setFace(true);

			Card1.setType(1);

			CardStatus Card2 = new CardStatus();

			Card2.setFace(true);

			Card2.setType(1);

			CardStatus Card3 = new CardStatus();

			Card3.setFace(true);

			Card3.setType(2);

			CardStatus Card4 = new CardStatus();

			Card4.setFace(true);

			Card4.setType(2);

			CardStatus Card5 = new CardStatus();

			Card5.setFace(true);

			Card5.setType(3);

			CardStatus Card6 = new CardStatus();

			Card6.setFace(true);

			Card6.setType(3);

			int ct = Card1.getType();

			

			

			

		

		}

		void Card1Click(object sender, EventArgs e)

		{

	

			int ans = cc.getcardCount();

		

			Bitmap kingPic = (Bitmap)Image.FromFile("C:\\Documents and Settings\\ahoe\\My Documents\\SharpDevelop Projects\\CardGame\\king.jpg");

			

			if (ans < 2)

				{

				cc.inccardCount(1);

				

				if (ct = 1)

					{

						Card1Button.BackgroundImage = kingPic;

					}

				}

		} 

		

		

	

		

	}

	

}

Basically I want to set up 6 card objects when the program starts and use them when I click on one of the cards, but within the code for the button click it can't reference the cards I set up when the program starts. There is something fundamental about C# or perhaps OO programming in general which I don't seem to be getting - any help would be much appreciated!

Errors:
The name 'cc' does not exist in the current context (CS0103) - C:\Documents and Settings\xxxx\My Documents\SharpDevelop Projects\CardGame\MainForm.cs:62,14

The name 'cc' does not exist in the current context (CS0103) - C:\Documents and Settings\xxxx\My Documents\SharpDevelop Projects\CardGame\MainForm.cs:68,5

The name 'ct' does not exist in the current context (CS0103) - C:\Documents and Settings\xxxx\My Documents\SharpDevelop Projects\CardGame\MainForm.cs:70,9

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
You are trying to use the cc object out of scope. You initialized it in mainform() and once you leave mainform() the cc object is no longer in scope, meaning you can't use it Card1Click(). Move it out of the function and make it global or pass it to the function. Here is how to make it global:

[highlight="c#"]
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace CardGame
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{

cardCount cc;

public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//


cc = new cardCount();
CardStatus Card1 = new CardStatus();
Card1.setFace(true);
Card1.setType(1);
CardStatus Card2 = new CardStatus();
Card2.setFace(true);
Card2.setType(1);
CardStatus Card3 = new CardStatus();
Card3.setFace(true);
Card3.setType(2);
CardStatus Card4 = new CardStatus();
Card4.setFace(true);
Card4.setType(2);
CardStatus Card5 = new CardStatus();
Card5.setFace(true);
Card5.setType(3);
CardStatus Card6 = new CardStatus();
Card6.setFace(true);
Card6.setType(3);
int ct = Card1.getType();




}
void Card1Click(object sender, EventArgs e)
{

int ans = cc.getcardCount();

Bitmap kingPic = (Bitmap)Image.FromFile("C:\\Documents and Settings\\ahoe\\My Documents\\SharpDevelop Projects\\CardGame\\king.jpg");

if (ans < 2)
{
cc.inccardCount(1);

if (ct = 1)
{
Card1Button.BackgroundImage = kingPic;
}
}
}




}

}
[/highlight]

#3
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
This was kind of a repeat post of Jordan's:) He was posting at the same time but beat me to it so I Just erased mine. I did notice that you should probably delete your other original cc declaration as well as move your int ct declaration to a class or global level object as well.

Edited by gaylo565, 11 August 2008 - 07:43 AM.


#4
andersh

andersh

    Newbie

  • Members
  • Pip
  • 2 posts
Many thanks for your help so far - I'm getting there! I've started this again from scratch at home and that bit works ok using the following code. I understand the problem that you're telling me but don't really understand the solution :o
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;


namespace CardGame

{

    public partial class Form1 : Form

    {

        int cardUpCount = 0;

        


        public Form1()

        {

            InitializeComponent();

initialiseCards();

        }


        public void initialiseCards()

        {

            CardStatus Card1 = new CardStatus();

            Card1.setFaceDown(true);

            Card1.setCardType(1);

            CardStatus Card2 = new CardStatus();

            Card2.setFaceDown(true);

            Card2.setCardType(1);

            CardStatus Card3 = new CardStatus();

            Card3.setFaceDown(true);

            Card3.setCardType(2);

            CardStatus Card4 = new CardStatus();

            Card4.setFaceDown(true);

            Card4.setCardType(2);

            CardStatus Card5 = new CardStatus();

            Card5.setFaceDown(true);

            Card5.setCardType(3);

            CardStatus Card6 = new CardStatus();

            Card6.setFaceDown(true);

            Card6.setCardType(3);

            

        }


        private void Card1_Click(object sender, EventArgs e)

        {

            if (cardUpCount < 2)

            {

                cardUpCount = +1;

                

            }

        }

    }

}

If I initialise the cards in initialiseCards() how do I then edit them in Card1_Click? i.e. use Card1.setFaceDown(false); when I click the card?
I have a bit of a mental block somewhere - thanks for all your help!

Edited by andersh, 11 August 2008 - 11:58 AM.


#5
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
If you want your card status variables to be available in all the classes methods you need to declare them outside the initialize method as well, or pass them to your new method as parameters. Any variables declared within a method only have the lifespan of that method so if the other modifying methods are called from within your original method it is usually passed as a parameter but if this isn't the case, as is your code, the variable is usually declared at the class level.