C # cannot access a non-stationary element of an external type

Hi, 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 use C # and I cannot access the shortcut from one of the files that I 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 eror here Error 141 Unable to access the non-stationary member of the external type "Moneypoly.mainForm" through the nested type "Moneypoly.mainForm.chance"

it should work, given that it works in my other files, and the code is the same. one of the codes of the other files 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) { } } } 
+2
source share
2 answers

Inner classes in C # work differently than in Java.

In Java, the inner class has access to the outer class: during construction, each instance stores a reference to its parent, who created the instance.

In C #, inner classes are an easy way to define classes that have access to private members of an instance. In other words, when you get or save a link to mainForm , you can read / write / modify private fields and call private methods. There is no such thing as an external relation.

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

 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 link to mainForm when building chance : possibly using this .

This is actually what Java does (except that it is invisible to the programmer).

In addition, in C # (and Java) it is configured to start the class name (and in the case of C # methods) with an uppercase letter.

+6
source

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

 namespace Moneypoly { public partial class mainForm { // UP TO HERE public class chance { public void chanceOptionOne() { discriptionPlayer1.Text = ""; } } } // MOVE THIS BRACE } 
-5
source

All Articles