Results 1 to 3 of 3

Thread: C# ส่งตัวแปลข้ามฟังก์ชันไงครับ

  1. #1
    Member Black_Hold's Avatar
    Join Date
    Sep 2009
    Location
    BlackHole
    Posts
    64


    จะส่งค่า หรือประกาศตัวแปลที่ใช้ได้ทุกฟังก์ชันยังไงหรอครับ

    Code:
      private void btnPlus_Click(object sender,  EventArgs e) //เมื่อกดปุ่มบวก
            {
                Int32 num1;
                num1  = Convert.ToInt32(txtInput.Text);
                txtInput.Clear();
            }
    
            private  void btnSum_Click(object sender, EventArgs e) //เมื่อกดปุ่มหาผลรวม
            {
                num2  = Convert.ToInt32(txtInput.Text);
                Int32 Sum = num1 +  num2; //ปัญหาคือมันเรียกใช้ตัวแปร num1 ไม่ได้อ่ะครับ
                txtInput.Clear();
                txtInput.Text  += +"ผลบวกของ" + num1 + "กับ" + num2 + "=" + Sum.ToString();
              }
    ลักษณะของโปรแกรมคือ 
    1.รับค่าเข้ามาทาง  textbox 
    2.เมื่อกดปุ่มบวก นำเลขที่ป้อนเข้ามาไปเก็บไว้ที่ตัวแปร num1   แล้วเคลีย textbox 
    3.จากนั้นก็รับค่าอีกครั้งเก็บไว้ที่ num2  
    4.เคลีย  textbox แล้วหาผลบอกของ num1,num2 (นี่แหละครับปัญหา)
    5.พิมพ์ sum  ออกทาง textbox
    ถ้านึกภาพไม่ออกลองเปิด calc ของ windows ดูสิครับ
    ขอบ คุณคร้าบ
    "โง่" กับ "ไม่รู้" มันต่างกันนะครับ
    อย่าไปว่าเขาเลยถ้าเขาถามอะไรที่เหมือนจะเป็นคำถามโง่ เขาคงไม่รู้
    สิ่งที่คุณรู้เขาอาจรู้แล้วแต่เขาไม่แสดงออก
    แม้เรารู้ในสิ่งที่เขากำลังบอก ฟังไว้เถิด ถือซะว่าเป็นการทบทวนและกัน

  2. #2
    Junior Member
    Join Date
    Jun 2009
    Posts
    15


    สร้างตัวแปรง 1 ตัวไว้นอกฟังก์ชั่น เพื่อให้อีกฟังก์ชั่นนึงมองเห็น แล้วให้มันเท่ากับ num1 ครับ ทำแบบนี้จะไม่ต้องส่งตัวแปรข้ามฟังก์ชั่น

    int temp_num1;

    private void btnPlus_Click(object sender, EventArgs e) //เมื่อกดปุ่มบวก
    {
    Int32 num1;
    num1 = Convert.ToInt32(txtInput);
    txtInput.Clear();

    temp_num1 = num1;
    }
    private void btnSum_Click(object sender, EventArgs e) //เมื่อกดปุ่มหาผลรวม
    {
    num2 = Convert.ToInt32(txtInput);
    Int32 Sum = temp_num1 + num2; //ปัญหาคือมันเรียกใช้ตัวแปร num1 ไม่ได้อ่ะครับ
    txtInput.Clear();
    txtInput.Text += +"ผลบวกของ" + num1 + "กับ" + num2 + "=" + Sum.ToString;
    }

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    1


    1 out of 1 members found this post helpful.
    ปัญหาคือ num1 ถูกประกาศไว้ในฟังชั่น btnPlus_Click ทำให้มีสถานะเป็น local variable ครับ
    ดังนั้น solution ที่คุณ rattisuk บอกไว้นั้นถูกแล้วคือสร้างตัวแปรอยู่นอกฟังชั่น ตัวแปรนั้นจะมีสถานะเป็น global variable คือทุกๆฟังชั่นสามารถเรียกใช้ได้
    แต่ว่าตัวแปร num1 ที่อยู่ใน btnPlus_Click เอาออกเลยก็ได้ครับ คิดว่ามันไม่ได้มีความเป็นอะไรเลย
    ถ้าเป็นผมจะเขียนแบบนี้ :

    private int num;
    private int sum;
    //โดยส่วนตัวคิดว่าประกาศตัวแปรเป็น private จะดีกว่าครับ(ศึกษาเพิ่มเติมได้ในส่วนของการเขียนโปรแกรมแบบ OOP)

    private void btnPlus_Click(object sender, EventArgs e) //เมื่อกดปุ่มบวก
    {
    num = Convert.ToInt32(txtInput.Text);
    txtInput.Clear();
    }

    private void btnSum_Click(object sender, EventArgs e) //เมื่อกดปุ่มหาผลรวม
    {
    sum = num + Convert.ToInt32(txtInput.Text);
    txtInput.Clear();
    txtInput.Text += +"ผลบวกของ" + num1 + "กับ" + num2 + "=" + sum.ToString();
    }

Members who have read this thread : 0

Actions : (View-Readers)

There are no names to display.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Who's Online (0)
0
0