亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法在 Windows 窗體中使用數組

無法在 Windows 窗體中使用數組

C#
神不在的星期二 2021-10-09 11:02:17
我正在嘗試初始化一個數組,將其設置為保存其他變量,然后在用戶按下表單上的按鈕時使用它。當我嘗試使用 button1_Click 類中的數組時,它無法識別它并說它在當前設置中不存在。namespace WindowsFormsApp1{    public partial class Form1 : Form    {        string activity;        int activityTime;        bool enteredInt = false;        //initializes variables used to store activities        string activity1 = "";        string activity2 = "";        string activity3 = "";        string activity4 = "";        string activity5 = "";        //intitializes variables used to store activity times        int activityTime1 = 0;        int activityTime2 = 0;        int activityTime3 = 0;        int activityTime4 = 0;        int activityTime5 = 0;        int i;        public Form1()        {            InitializeComponent();            string[] activities = { activity1, activity2, activity3, activity4, activity5 };            int[] activityTimes = { activityTime1, activityTime2, activityTime3, activityTime4, activityTime5 };        }        private void label1_Click(object sender, EventArgs e)        {        }        private void button1_Click(object sender, EventArgs e)        {            enteredInt = Int32.TryParse(textBox2.Text, out activityTime);            if (textBox1.Text != "" && textBox2.Text != "" && enteredInt == true)            {                activity = textBox1.Text;                activityTime = Int32.Parse(textBox2.Text);            }            else            {                MessageBox.Show("Please enter valid activity and time.");            }            for(i=0;i<5;i++)            {                if (activity==activities[i])                {                    activity += activities[i];                    i = 5;                }            }        }        private void textBox1_TextChanged(object sender, EventArgs e)        {        }        private void textBox2_TextChanged(object sender, EventArgs e)        {        }    }}對不起,如果這是一個愚蠢的問題,我真的需要回答。
查看完整描述

1 回答

?
FFIVE

TA貢獻1797條經驗 獲得超6個贊

你應該定義 int[]activityTimes 作為一個全局變量,把它放在外面


public partial class Form1 : Form

{

    string activity;

    int activityTime;

    bool enteredInt = false;


    //initializes variables used to store activities

    string activity1 = "";

    string activity2 = "";

    string activity3 = "";

    string activity4 = "";

    string activity5 = "";



    //intitializes variables used to store activity times

    int activityTime1 = 0;

    int activityTime2 = 0;

    int activityTime3 = 0;

    int activityTime4 = 0;

    int activityTime5 = 0;


    int i;

    string[] activities=new string[length];



    public Form1()

    {

        InitializeComponent();

    }

}


查看完整回答
反對 回復 2021-10-09
  • 1 回答
  • 0 關注
  • 172 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號