9 Replies - 339 Views - Last Post: 22 February 2013 - 09:13 AM Rate Topic: -----

#1 TommoUK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-February 13

Protection level problem

Posted 22 February 2013 - 06:05 AM

Hey guys am just starting working on a basic card game for an assessment, I currently have 2 forms (Form1.cs calls Game.cs when the play button is clicked), The below code is the code I am using inside game.cs.

I am trying to simply create a base class for a top trumps card deck, But am getting an error from this line

Card1.Text = TopTrump1.height.ToString();

telling me that it's inaccessible due to protection level. I have tried manually chanding the text box properties from private to public but still same error. Below is a code dump of my game.cs, this is my first time working with multiple forms with multiple source files and I assume my ordering is probably wrong.

Any tips on tidying up code would be nice aswell, Thanks :)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TopTrumps
{

 

    public partial class Game : Form
    {
        public class TopTrumps  //Top trumps class
        {
            int height;
            int length;
            int speed;

            public TopTrumps(int a, int b, int c)  //Pass height length and speed as arguements to class
            {
                this.height = a;   // Set objects height to the parsed value of a
                this.length = b;
                this.speed = c;
            }
        }

        public Game()
        {
            InitializeComponent();
        }

        public static void DefineCards()
        {
            TopTrumps TopTrump1 = new TopTrumps(10, 20, 30);
        }

        public static void InitialiseDeck()
        {
            DefineCards();
           
        }

        private void Hide_Click(object sender, EventArgs e)
        {
            Form1 MainScreen = new Form1();
            this.Hide();
            MainScreen.ShowDialog();

        }

        public void DealCards_Click(object sender, EventArgs e)
        {
            TopTrumps TopTrump1 = new TopTrumps(10, 20, 30);
            Card1.Text = TopTrump1.height.ToString();

        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Protection level problem

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Protection level problem

Posted 22 February 2013 - 06:16 AM

What is Card1? Is that a textbox of your Game form?

Check the protection level of the "height" property in the TopTrump class.

This post has been edited by MrShoes: 22 February 2013 - 06:19 AM

Was This Post Helpful? 1
  • +
  • -

#3 TommoUK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-February 13

Re: Protection level problem

Posted 22 February 2013 - 06:23 AM

Hey, Yes Card1 is a text box within the game form. How do I go about changing the height property, I assumed a public class of Top Trumps would allow me to access all variables any where?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3569
  • View blog
  • Posts: 11,089
  • Joined: 05-May 12

Re: Protection level problem

Posted 22 February 2013 - 06:36 AM

Incorrect assumption. The public attribute of class TopTrumps makes the class available to everybody. It doesn't make its member variables public.
Was This Post Helpful? 1
  • +
  • -

#5 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Protection level problem

Posted 22 February 2013 - 06:38 AM

No. You set an access modifier to each property within a class. So, if a class is public, you can access any public variables. By default, if you don't specify, it's private. Therefore, just change the declaration from int height; to public int height;
Was This Post Helpful? 1
  • +
  • -

#6 TommoUK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-February 13

Re: Protection level problem

Posted 22 February 2013 - 06:43 AM

Great after bit more research after your advice found out just needed public before variables, Thanks :)

View PostTommoUK, on 22 February 2013 - 06:41 AM, said:

Great after bit more research after your advice found out just needed public before variables, Thanks :)/>


Ah got a reply just as I found fixed, Thanks everyone sorted
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3569
  • View blog
  • Posts: 11,089
  • Joined: 05-May 12

Re: Protection level problem

Posted 22 February 2013 - 07:17 AM

Unless a class is meant to be used as a POCO or POJO, it is usually not a good idea to make your member variables public.
Was This Post Helpful? 0
  • +
  • -

#8 TommoUK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-February 13

Re: Protection level problem

Posted 22 February 2013 - 07:53 AM

I see, What other way would you recommend going about my situation if not to make them public?
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3569
  • View blog
  • Posts: 11,089
  • Joined: 05-May 12

Re: Protection level problem

Posted 22 February 2013 - 08:02 AM

Use of Properties is the usual recommendation. http://msdn.microsof...0(v=vs.71).aspx

This post has been edited by Skydiver: 22 February 2013 - 08:02 AM

Was This Post Helpful? 0
  • +
  • -

#10 TommoUK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-February 13

Re: Protection level problem

Posted 22 February 2013 - 09:13 AM

Ah I see that looks cleaner to use after having read through, I notice however everything is done within that single class, Where as with my current code I create the class, And then can use the variables within other events, Does this still apply with this approach?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1