locked
is field but is used as a type? RRS feed

  • Question

  • im very new to classes and i havnt found anyone with the same problem in this scenario, i have ftm 2 calsses: Form1 class and a Player class. in the player class: 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Drawing;
    
    namespace _4irad
    {
        class Player
        {
            public int posInLine = 10;
    
            public bool myTurn;
    
            public int[,] Positions = new int[7, 6];
    
            public Color PlayerColor = Color.White;
    
            public Player(Color color){
                setColor(color);         
            }
            public void setColor(Color color)
            {
                this.PlayerColor = color;
    
            }
    
            public Color getColor()
            {
                return this.PlayerColor;
    
            }
    
    
    
        }
    }

    and in the Form class:

    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 _4irad
    {
        public partial class Form1 : Form
        {
            int[,] layout = new int[7, 6];
            Player player1 = new Player(Color.Green);
            
            public Form1()
            {
                InitializeComponent();
            }
    
    
            
            
    
    
            void placeBrick(int Row, Player playerTurn)
            {
                for (int i = 0; i < layout.GetLength(1); i++)
                {
                    if(layout[Row,i] == null){
                        Panel brick = new Panel();
                        brick.Name = Row.ToString() + "-" + i.ToString();
                        brick.Left = Row * 100;
                        brick.Top = 500 - (i * 100);
                        brick.BackColor = playerTurn.getColor();
                        layout[Row, i] = playerTurn.posInLine;
                        playerTurn.Positions[Row, i] = 1;
                        Controls.Add(brick);
                        break;
                    }
                }
    
            }
    
        }
    }
    

    when i try ro use "player1" it pops up:

    "_4irad.Form1.player1 is a field but is used as a type"as i said i am new to classes and whould appreachiate feedback and an explaination to why this happends so that it wont happen again.

    Thursday, June 18, 2015 3:20 PM

Answers

  • _4irad.Form1.player1

    is not a valid identifier.  _4irad.Form1 is the namespace-prefixed name of your type and would be used if you wanted to create an instance of the type:

    var form = new _4irad.Form1();

    Once you have an instance of your type you can then access the members which would include PlayerColor and Positions:

    form.PlayerColor = Color.Red;

    player1 is defined inside Form1 as a private field and is therefore not accessible outside the type (which is good).  Inside the methods/properties of Form1 you can refer to the field simply by using its field name:

    private void SomeMethodInForm1 ()
    {
       player1.PlayerColor = Color.Green;
    }

    Members of types (fields, properties, methods) can only be accessed by using the instance name (form1 or this) in most cases (unless they are static but that is a topic for a different day). 

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Proposed as answer by Kristin Xie Friday, June 19, 2015 6:44 AM
    • Marked as answer by Kristin Xie Sunday, June 28, 2015 9:31 AM
    Thursday, June 18, 2015 4:07 PM

All replies

  • How do you use player1? It's never used in the code you have posted, I suspect there's more code than this. That error should point to a particular line of code, post that line.

    Thursday, June 18, 2015 3:56 PM
  • _4irad.Form1.player1

    is not a valid identifier.  _4irad.Form1 is the namespace-prefixed name of your type and would be used if you wanted to create an instance of the type:

    var form = new _4irad.Form1();

    Once you have an instance of your type you can then access the members which would include PlayerColor and Positions:

    form.PlayerColor = Color.Red;

    player1 is defined inside Form1 as a private field and is therefore not accessible outside the type (which is good).  Inside the methods/properties of Form1 you can refer to the field simply by using its field name:

    private void SomeMethodInForm1 ()
    {
       player1.PlayerColor = Color.Green;
    }

    Members of types (fields, properties, methods) can only be accessed by using the instance name (form1 or this) in most cases (unless they are static but that is a topic for a different day). 

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Proposed as answer by Kristin Xie Friday, June 19, 2015 6:44 AM
    • Marked as answer by Kristin Xie Sunday, June 28, 2015 9:31 AM
    Thursday, June 18, 2015 4:07 PM
  • Sounds like some serious syntax error is tripping the compiler up.

    My guess is you have a semikolon to many, propably after a function parameter declaration. So the bracketed area behind this are interpreted as part of the class definition, rather then part of the function (and the function is just empty).

    Example:

    void someFunction(); //function ends at semikolon
    //Start of totally unrelated stuff that is interpreted as being part of the class definition and includes elements that do not belong outside of function definitions.
    {
        
    }
    

    Thursday, June 18, 2015 9:56 PM