Windows Form Not Returning Correct Value from Public Method

Emdadul Karim Bakth

I have three classes, code provided below.

Network - Add and Remove Phone, Process Calls Phone1 and Phone2 can call each other when added to the network.

But I am having issue when I am connecting both phone to the network and trying to call phone1 to phone2, it is keep giving me "receiver busy". I have tried to do little debugging and read status of phone2 when calling from phone1 but it returns an empty string (Which should actually return "A", when it is added to the network).

Any help would much appreciated.

-----Networks Class------------------

namespace Demo
{
    public partial class network : Form
    {
        phone1 p1 = new phone1();
        phone2 p2 = new phone2();
        public network()
        {
            InitializeComponent();
        }

        public Boolean numberValidator(int number)
        {

            Boolean exist = false;
            if (comboBox2.Items.Equals(number))
            {
                exist = true;
            }

            return exist;
        }

        public void processCall(int rNumber)

        {

            if (!numberValidator(rNumber))
            {
                p1.TextBox1.Clear();
                p1.TextBox1.Text = "Not connected";

                //MessageBox.Show(p2.returnPhoenStatus());
            }

            else
            {

                    p1.TextBox1.Clear();

                    p1.TextBox1.Text = "Call in progress";

                    p2.receiveCall(1);

                    p1.setStatus("Busy");
                    /*
                    if (p2.btnCallPressStatus())
                    {
                        p1.TextBox1.Clear();

                        p1.TextBox1.Text = "Call initiated";
                    }*/

             }


           }


        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                p1.Show();
                comboBox2.Items.Add(1);
                p1.setStatus("A");
            }
            if (comboBox1.SelectedIndex == 1)
            {
                p2.Show();
                comboBox2.Items.Add(2);
                p2.setStatus("A");
            }
        }
    }
}

----------Phone1 Class---------

namespace Demo
{
    public partial class phone1 : Form
    {
        public phone1()
        {
            InitializeComponent();

        }




        string status;

        public void setStatus(string Status)
        {
            status = Status;
        }

        public string returnStatus()
        {
            return status;
        }

        public void receiveCall(int callerNumber)
        {
            setStatus("Busy");

            btnCall.Text = "Answer";

            textBox1.Text = "Phone " + callerNumber + " Calling.";

        }

        public void makeCall(int number)
        {
            phone2 p2 = new phone2();
            network net = new network();

            MessageBox.Show(p2.returnStatus()); // this line not returing status of phone2
            if (p2.returnStatus() == "A")
            {
                net.processCall(number);
            }
            else
            {
                textBox1.Text = "Receiver Busy";
            }


        }

        public TextBox TextBox1
        {
            get
            {
                return textBox1;
            }
        }

        private void btnCall_Click(object sender, EventArgs e)
        {
            string number = textBox1.Text;
            int numberInt = Convert.ToInt16(number);


            makeCall(numberInt);
        }

        string phoneNo = "";
        private void btn2_Click(object sender, EventArgs e)
        {
            phoneNo = phoneNo + btn2.Text;

            textBox1.Text = phoneNo;
        }
    }
}

-------------phone2 Class--------------

namespace Demo
{
    public partial class phone2 : phone1
    {
        public phone2()
        {
            InitializeComponent();
        }
    }
}
Enigmativity

Piyush has the right answer, but I thought I'd add this answer as a handy hint to avoid this kind of error.

Try writing your button1_Click method like this:

private void button1_Click(object sender, EventArgs e)
{
    var i = comboBox1.SelectedIndex;
    var p = (new [] { p1, p2 })[i]; // Or `var p = i == 0 ? p1 : p2;`

    p.Show();
    comboBox2.Items.Add(i + 1);
    p.setStatus("A");
}

This way you avoid the code duplication and the mistyping that occurred.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Returning a value from a Method

분류에서Dev

numpy not returning the correct median value

분류에서Dev

Better Practice: Printing from void method OR returning value from method and printing from method caller

분류에서Dev

Returning Value from Actor

분류에서Dev

Returning a value from a Thread?

분류에서Dev

Jquery UI Mobile returning empty value for form

분류에서Dev

How to save value in correct format if I received it by getEditor from Ext.form.field.Date?

분류에서Dev

Returning a value from a recursive function

분류에서Dev

Delphi: Use public procedures from a dynamic form

분류에서Dev

Method returning null value to database statement

분류에서Dev

map.find(var) isn't returning correct value

분류에서Dev

Returning managed pointer from a clone method

분류에서Dev

Rails: The correct way of returning json from get request?

분류에서Dev

The correct use of method post-get in flask in select form

분류에서Dev

Returning a value from a function with Alamofire and SwiftyJson

분류에서Dev

Access Windows Form from a Folder

분류에서Dev

Rails getting form value to create method

분류에서Dev

Pass value from <form> to button? with {if $' }

분류에서Dev

What is the correct public access config for s3 bucket to access from lambda with http proxy integration?

분류에서Dev

Default select value in form from MySQL database

분류에서Dev

Float value is not correct when parsing it from a String, double works, why?

분류에서Dev

Use a value from a script as a default value inside a form field?

분류에서Dev

How return value from anonymous method to label?

분류에서Dev

Passing Enum value in a method from dropdown?

분류에서Dev

Undefined value when calling an input from a HTML form from PHP

분류에서Dev

Returning null after form submission

분류에서Dev

PHP function not returning value

분류에서Dev

AngularJS : service not returning value

분류에서Dev

ArrayList returning null value

Related 관련 기사

  1. 1

    Returning a value from a Method

  2. 2

    numpy not returning the correct median value

  3. 3

    Better Practice: Printing from void method OR returning value from method and printing from method caller

  4. 4

    Returning Value from Actor

  5. 5

    Returning a value from a Thread?

  6. 6

    Jquery UI Mobile returning empty value for form

  7. 7

    How to save value in correct format if I received it by getEditor from Ext.form.field.Date?

  8. 8

    Returning a value from a recursive function

  9. 9

    Delphi: Use public procedures from a dynamic form

  10. 10

    Method returning null value to database statement

  11. 11

    map.find(var) isn't returning correct value

  12. 12

    Returning managed pointer from a clone method

  13. 13

    Rails: The correct way of returning json from get request?

  14. 14

    The correct use of method post-get in flask in select form

  15. 15

    Returning a value from a function with Alamofire and SwiftyJson

  16. 16

    Access Windows Form from a Folder

  17. 17

    Rails getting form value to create method

  18. 18

    Pass value from <form> to button? with {if $' }

  19. 19

    What is the correct public access config for s3 bucket to access from lambda with http proxy integration?

  20. 20

    Default select value in form from MySQL database

  21. 21

    Float value is not correct when parsing it from a String, double works, why?

  22. 22

    Use a value from a script as a default value inside a form field?

  23. 23

    How return value from anonymous method to label?

  24. 24

    Passing Enum value in a method from dropdown?

  25. 25

    Undefined value when calling an input from a HTML form from PHP

  26. 26

    Returning null after form submission

  27. 27

    PHP function not returning value

  28. 28

    AngularJS : service not returning value

  29. 29

    ArrayList returning null value

뜨겁다태그

보관