- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
你好,我是一名初级程序员,所以如果这个问题已经得到回答或没有意义,请原谅 n=me。我的问题是我使用的是 C#,但我无法从我创建的文件之一访问标签。但是,在我重新启动计算机之前,该文件一直有效。这是代码。
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 在这里给出错误,错误 141 无法通过嵌套类型“Moneypoly.mainForm.chance”访问外部类型“Moneypoly.mainForm”的非静态成员
考虑到它在我的其他文件中有效并且代码相同,它应该有效。其中一个文件代码如下
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)
{
}
}
}
最佳答案
C# 中的内部类与 Java 中的内部类工作方式不同。
在 Java 中,内部类可以访问其外部类:在构造期间,每个实例都会存储对其构造该实例的父类的引用。
在 C# 中,内部类是定义可以访问实例私有(private)成员的类的一种简单方法。换句话说,当您接收 或存储 对mainForm
的引用时,您可以读取/写入/修改私有(private)字段并调用私有(private)方法。不存在外部关系。
因此,您可以在内部类中定义构造函数到外部,然后设置父类的字段:
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 = "";
}
}
}
}
请注意,当您构造一个chance
时,您需要提供对mainForm
的引用:可能使用this
。
这实际上也是 Java 所做的(除了它对程序员是不可见的)。
此外,在 C#(和 Java)中习惯以大写字母开头类名(在 C# 方法的情况下也是如此)。
关于C# 无法访问外部类型的非静态成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24222296/
如何指示 webpack 排除所有 d3 模块? // does not work externals: { "d3-*": "d3" } 所以如果应用导入了d3-submod
这个问题在这里已经有了答案: 9年前关闭。 Possible Duplicate: What does “outer =>” really mean? 我在哪里可以找到有关信息 trait After
这是一个简单的循环,我正在尝试对性能进行基准测试。 var extremeLoop=function(n){ var time=new Date() var t=0; for(
问题+概述 下面是两个片段,其中包含最初隐藏的 div,然后通过单击 button 和 jQuery 的 .show() 显示。两个 div 都具有由外部样式表应用的 display: grid; 样
我有一个 HTML 页面和一个单独的 .js 文件,该文件包含在带有 的页面中标签。 这是我的 .js 文件: element = document.getElementById("test");
我在 linux 静态库项目中有 3 个文件,我想在两个类方法实现文件中使用的静态字段存在链接问题。我有 class1.h、class1main.cpp 和 class1utils.cpp。 clas
我正在尝试将颜色背景更改为默认背景颜色,当我点击输入框外 我尝试使用“null”或“none”但没有用? window.addEventListener('click', outsideClick);
我正在编写一个应用程序,要求用户在手机上选择各种类型的文件。我使用此代码启动文件选择器 Intent : Intent intent = new Intent(Intent.ACTION_GET_C
在 android 中,不可移动(内部)的外部存储和内部存储有什么区别?我不确定在哪里保存我的数据。我只需要保存一个人可以随时提取的游戏统计数据 谢谢 最佳答案 在许多较新的设备中,将不再有物理区别,
在 C++ 中,假设我们有这个头文件: myglobals.h #ifndef my_globals_h #define my_globals_h int monthsInYear = 12; #en
我正在尝试使用 externs 在 C++ 中连接到 Ada。这两种实现有什么区别? 实现A namespace Ada { extern "C" { int getN
这个问题在这里已经有了答案: Get selected element's outer HTML (30 个答案) 关闭 2 年前。 想象一下我们有这样的东西: Hello World 如果我们这样
假设我在模块的顶部有这个: Public Declare Function getCustomerDetails Lib "CustomerFunctions" () As Long 如果我从 VB6
我目前正在使用这段代码: var wordRandomizer = { run: function (targetElem) { var markup = this.creat
我们正在使用 SVN 试水,并以 Beanstalk 作为主机。我们的设置如下所示: 存储库:模块 模块一 模块二 模块 3 存储库:网站1 自定义网站代码 svn:对模块 1 的外部引用 svn:对
有没有办法在负载均衡器中设置自动外部 IP 分配给像谷歌这样的服务? 我在裸机上运行 Kubernetes。 谢谢 最佳答案 使用 nodePort 类型的服务,它会将您的服务绑定(bind)到所有节
是否有可能在 Controller 之外使用 generateUrl() 方法? 我尝试在带有 $this->get('router') 的自定义存储库类中使用它,但它没有用。 更新 我在这里找到了一
我目前正在尝试通过 Webpack 外部对象外部化 Angular 依赖项来缩短构建时间。到目前为止,我已经为 React 和其他小库实现了这一目标。 如果我只是移动 '@angular/compil
我想创建一个自动应用其他插件的插件(外部插件)。这要求在我称为“应用插件”之前为插件设置构建脚本依赖项。但是似乎我无法在插件中添加buildscript依赖项,或者得到了: 您不能更改处于未解析状态的
我是R包的创建者EnvStats . 有一个我经常使用的函数,叫做 stripChart .我刚开始学习ggplot2 ,并在过去几天里仔细研究了 Hadley 的书、Winston 的书、Stack
我是一名优秀的程序员,十分优秀!