C# cannot access a non-static member of outer type

Hello i am a beginner programmer so please forgive n=me if this question has been answered or does not make sense. My problem is that i am using C# and i cant access a label from one of the files i have created. However this file worked until i rebooted my computer. Here is the code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;

namespace Moneypoly
{
    public partial class mainForm
    {

        public class chance
        {
          public void chanceOptionOne()
          {
              discriptionPlayer1.Text = "";           
          }
        }

    }
} 

discriptionPlayer1.Text gives an eror here it is Error 141 Cannot access a non-static member of outer type 'Moneypoly.mainForm' via nested type 'Moneypoly.mainForm.chance'

it should work considering it works in my other files and the code is the same. one of the other files code is as follows

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;
using System.Media;

namespace Moneypoly
{
    public partial class mainForm : Form 
    {
        public mainForm()
        {
            InitializeComponent();
            giantView1.SizeMode = PictureBoxSizeMode.StretchImage;
            giantView2.SizeMode = PictureBoxSizeMode.StretchImage;
            giantView1.Image = Properties.Resources.startPlayerNoOne;
            giantView2.Image = Properties.Resources.startPlayerNoOne;
            player2Roll.Visible = false;
            dicePlayer1.Visible = false;
            dicePlayer2.Visible = false;
            player1Wallet.Text = " $ " + variables.player1Wallet.ToString();
            player2Wallet.Text = " $ " + variables.player2Wallet.ToString();
        }


        private void buttonRollDice_Click(object sender, EventArgs e)
        {
            movePlayer1();
            movePlayer2();
        }

        private void mainForm_Load(object sender, EventArgs e)
        {

        }

        private void player1_Click(object sender, EventArgs e)
        {

        }

        private void player1Roll_Click(object sender, EventArgs e)
        {
            movePlayer1();
            player1Roll.Visible = false;
            player2Roll.Visible = true;
            dicePlayer1.Visible = true;
        }

        private void player2Roll_Click(object sender, EventArgs e)
        {
            movePlayer2();
            player2Roll.Visible = false;
            player1Roll.Visible = true;
            dicePlayer2.Visible = true;
        }

        private void discriptionPlayer1_Click(object sender, EventArgs e)
        {

        }

        private void discriptionPlayer2_Click(object sender, EventArgs e)
        {

        }
    }

        }

Upvotes: 2

Views: 3660

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476584

Internal classes in C# work different than they do in Java.

In Java an internal class has access to its outer class: during the construction, each instance stores a reference to its parent that constructed the instance.

In C#, internal classes are a simple way to define classes that have access to an instance private members. In other words when you receive or store a reference to a mainForm you can read/write/modify private fields and call private methods. There is no such thing as a outer-relation.

As a result you can define a constructor in your internal class to the outer and then set the field of the parent:

namespace Moneypoly {

    public partial class mainForm {

        public class chance {

          private readonly mainForm outer;

          public chance (mainForm outer) {
              this.outer = outer;
          }

          public void chanceOptionOne() {
              outer.discriptionPlayer1.Text = "";           
          }

        }

    }
}

Note that you will need to give a reference to the mainForm when you construct a chance: probably using this.

This is in fact what Java does as well (except that it is invisible to the programmer).

Furthermore it is custom in C# (and Java) to start the name of classes (and in the case of C# methods as well) with a capital letter.

Upvotes: 6

Pappy
Pappy

Reputation: 1

This is trying to create a class inside of the partial main form class:

namespace Moneypoly
{
    public partial class mainForm
    {
    // UP TO HERE

        public class chance
        {
          public void chanceOptionOne()
          {
              discriptionPlayer1.Text = "";           
          }
        }

    }  // MOVE THIS BRACE
} 

Upvotes: -5

Related Questions