我有一個Class
和一個Form
。這是我在我的表單代碼:C#名稱在當前上下文中不存在
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 Tamagotchi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
label3.Text = "doplntamboredom";
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
label6.Text = GetMood();
}
}
}
這最後一部分(label6.Text = GetMood()
)正在錯誤:
The name 'GetMood' does not exist in the current context.
這是我在我的類代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tamagotchi
{
class Mazlicek
{
private int hunger = 1;
private int boredom = 1;
private string mood;
public string GetMood()
{
return mood;
}
private void Moods(int hunger, int boredom, string strHunger, string strBoredom)
{
if ((boredom + hunger) > 15)
{
mood = "Angry!";
}
else if ((boredom + hunger) > 10 && (boredom + hunger) < 15)
{
mood = "Frustrated..";
}
else if ((boredom + hunger) > 5 && (boredom + hunger) < 11)
{
mood = "Fine.";
}
else
{
mood = "Happy!";
}
}
}
}
我不知道爲什麼有這個問題。我正在使用Visual Studio 2015,它都在一個項目中。
您的表單中的「Mazlicek」實例在哪裏?你爲什麼試圖從沒有定義的地方調用'GetMood'? –
你的'情緒'方法是'私人',所以沒有人可以稱之爲。即使你創建了一個實例來修復代碼,GetMood()總是返回null。 –
https://msdn.microsoft.com/library/x9afc042(v=vs.110).aspx請閱讀本文以及https://msdn.microsoft.com/en-us/library/79b3xss3.aspx – MethodMan