Từ 1 tới 4 trên tổng số 4 kết quả

Đề tài: Khởi tạo array bằng các dữ liệu truyền vào từ event

  1. #1
    Ngày gia nhập
    10 2014
    Bài viết
    17

    Mặc định Khởi tạo array bằng các dữ liệu truyền vào từ event

    Dưới đây là code một chương trình đơn giản của mình. Mình muốn tạo một array 2 chiều để lưu thông tin về job và các giá trị liên quan đến nó. Mình gặp vấn đề là không biết cách khai báo cái arrayTable trong class như thế nào. Ban đầu mình tạo array rỗng như trong quote, sau khi sử dụng button 1 và 2 để gán giá trị cho nó thì khi show cái table ra bằng button 3 thì báo lỗi như trong hình. Do đó mình nghĩ là ở event của button 3, chương trình vẫn gọi cái arrayTable ra chứ ko phải cái arrayTable mình đã sử dụng ở button 2.

    Mong mọi người chỉ dẫn cách xử lý lỗi nhỏ này T___T. Cám ơn nhiều.


    namespace WindowsFormsApplication1
    {
    public partial class Form1 : Form
    {
    static int arrayRow;
    static int[,] arrayTable = new int[,] {};
    static int numfactor = 4; // numfactor = job, processing time, duel time, weight

    public Form1()
    {
    InitializeComponent();

    }



    private void button1_Click(object sender, EventArgs e)
    {
    string numjob = numJob.Text;
    arrayRow = Int32.Parse(numjob);
    jobID.Text = "1";

    numJob.Enabled = false;
    }

    private void button2_Click(object sender, EventArgs e)
    {
    int[,] arrayTable = new int[arrayRow + 1, numfactor];

    int _jobID = Int32.Parse(jobID.Text);
    arrayTable[_jobID, 0] = Int32.Parse(jobID.Text);
    arrayTable[_jobID, 1] = Int32.Parse(pID.Text);
    arrayTable[_jobID, 2] = Int32.Parse(dID.Text);
    arrayTable[_jobID, 3] = Int32.Parse(wID.Text);
    _jobID++;

    if (_jobID > arrayRow)
    {
    button2.Enabled = false;
    MessageBox.Show("You've already initialized the data table!", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else
    jobID.Text = Convert.ToString(_jobID);

    }

    private void button3_Click(object sender, EventArgs e)
    {
    string output = " ";
    output += "Jobs\tProcessing Time\tDuel Time\tWeight";

    for (int i = 1; i <= arrayRow; i++)
    output += arrayTable[i, 0] + "\t" + arrayTable[i, 1] + "\t" + arrayTable[i, 2] + "\t" + arrayTable[i, 3] + "\n"; <-- Báo lỗi "Index was outside the bounds of the array."

    MessageBox.Show(output, "Data Table", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    }



    }

  2. #2
    Ngày gia nhập
    02 2013
    Bài viết
    219

    Cái tag này là quote chứ đâu phải code, đọc không nổi bạn ơi

    Gợi ý nên thay array bằng List<T> với T là 1 instance của class Job:
    Visual C# Code:
    1.  public int JobID { get; set;}
    2.  public int PID { get; set;}
    3.  public int DID { get; set;}
    4.  public int WID { get; set;}
    5. }
    Lúc nào cũng lãng đãng như người trên mây

    Những người mới : Hãy tìm hiểu luật lệ diễn đàn trước khi post bài !

  3. #3
    Ngày gia nhập
    10 2009
    Nơi ở
    gầm cầu
    Bài viết
    156

    Do đó mình nghĩ là ở event của button 3, chương trình vẫn gọi cái arrayTable ra chứ ko phải cái arrayTable mình đã sử dụng ở button 2.
    quá chuẩn
    Visual C# Code:
    1. private void button2_Click(object sender, EventArgs e)
    2. {
    3. int[,] arrayTable = new int[arrayRow + 1, numfactor]; // <=== sai lầm ở đây
    4.  
    5. int _jobID = Int32.Parse(jobID.Text);
    6. arrayTable[_jobID, 0] = Int32.Parse(jobID.Text);
    7. arrayTable[_jobID, 1] = Int32.Parse(pID.Text);
    8. arrayTable[_jobID, 2] = Int32.Parse(dID.Text);
    9. arrayTable[_jobID, 3] = Int32.Parse(wID.Text);
    10. _jobID++;
    11. ......................
    12. }
    thiết nghĩ nên khởi tạo arrayTable ở button1 chứ ko phải button2

    Visual C# Code:
    1. private void button1_Click(object sender, EventArgs e)
    2. {
    3. string numjob = numJob.Text;
    4. arrayRow = Int32.Parse(numjob);
    5. jobID.Text = "1";
    6. arrayTable = new int[arrayRow + 1, numfactor];
    7. numJob.Enabled = false;
    8.  
    9.  
    10. }

    Visual C# Code:
    1. private void button2_Click(object sender, EventArgs e)
    2. {
    3. //int[,] arrayTable = new int[arrayRow + 1, numfactor];
    4.  
    5. int _jobID = Int32.Parse(jobID.Text);
    6. arrayTable[_jobID, 0] = Int32.Parse(jobID.Text);
    7. arrayTable[_jobID, 1] = Int32.Parse(pID.Text);
    8. arrayTable[_jobID, 2] = Int32.Parse(dID.Text);
    9. arrayTable[_jobID, 3] = Int32.Parse(wID.Text);
    10. _jobID++;
    11.  
    12. if (_jobID > arrayRow)
    13. {
    14. button2.Enabled = false;
    15. MessageBox.Show("You've already initialized the data table!", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);
    16. }
    17. jobID.Text = Convert.ToString(_jobID);
    18.  
    19. }

  4. #4
    Ngày gia nhập
    10 2014
    Bài viết
    17

    Mình hiểu rồi và đã giải quyết đc. Cám ơn mọi người nhiều ^^

Quyền hạn của bạn

  • Bạn không thể gửi đề tài mới
  • Bạn không thể gửi bài trả lời
  • Bạn không thể gửi các đính kèm
  • Bạn không thể chỉnh sửa bài viết của bạn