[英]How do I reference a variable from one class to another?
我是 C# 的新手,我不知道如何將變量的值從一個 class 引用到另一個。
它設置為按下按鈕時,它將獲取文本框中的文本並將其設置為“alphaask”。 然后它實例化“alphaanswer”,它會告訴 label 更改其文本。
“alphaanswer” 將取值“alphaQuest”,看看它是否等於“bob”,這將改變 label。
所有我想知道如何從“alphaask”的值中設置“alphaQuest”的值,以便字符串可以用“alphaanswer”檢查它
public partial class QuestionTab : Form
{
public string alphaask = "null";
public void button1_Click(object sender, EventArgs e)
{
// alphabutton
// Checks if something is in textbox then says bool is true
bool asked = false;
if(textBoxAlpha.Text != "")
{
alphaask = textBoxAlpha.Text;
asked = true;
}
if(asked==true)
{
// If bool is true than instance auxy
var instance = new Alpha();
instance.alphaanswer();
}
}
}
public class Alpha
{
string alphaQuest = // <-- I want to make alphaQuest equal to alphaask
alphaanswer();
public void alphaanswer()
{
if (alphaanswer == bob)
{
//change text in label1
}
}
}
做這些改變
public partial class QuestionTab : Form
{
public string alphaask = "null";
public void button1_Click(object sender, EventArgs e)
{
bool asked = false;
if(textBoxAlpha.Text != "")
{
alphaask = textBoxAlpha.Text;
asked = true;
}
if(asked==true)
{
// If bool is true than instance auxy
var instance = new Alpha();
instance.alphaAnswer(alphaask);
//Here you are sending the current value of alphaAsk to the alphaanswer method.
}
}
}
public class Alpha
{
public void alphaAnswer(string alphaAnswer) //This string receives the value you sent
{
if (alphaAnswer == "bob")
{
//change text in label1
}
}
}
使用字符串參數在 class Alpha 中創建一個承包商
public Alpha(String value)
{
}
那么當你調用它時
var instance = new Alpha(alphaask);
instance.show();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.