Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, everyone!
I made this calculator in C# linked to XAML, and it works perfect, however, part of the assignment for the school ask for overloaded methods and for methods with arguments modifiers, too. Is that possible? Is possible to have overloaded methods and methods with arguments for event handlers? Or do I need to remake my calculator without event handlers. I just need some advise, please. Thanks

This is the C# code for adding and resting, rest is the same, just changes name of the math operations.

public partial class MainPage : ContentPage
    {
       
        public MainPage()
        {
            InitializeComponent();
            

        }

        private void button1_click(object sender, EventArgs e)
        {
            var btn = sender as Button;
            var key = btn.Text;
          
            float num1;
            float num2;
            float sum;
            num1 = Convert.ToInt32(number1.Text);
            num2 = Convert.ToInt32(number2.Text);
          
            sum = num1 + num2;

            label3.Text = "Result is "+ sum;
        }
        private void button2_click(object sender, EventArgs e)
        {
            var btn = sender as Button;
            var key = btn.Text;

            int num1;
            int num2;
            int rest;
            num1 = Convert.ToInt32(number1.Text);
            num2 = Convert.ToInt32(number2.Text);

            rest = num1 - num2;

            label3.Text = "Result is " + rest;
        }


What I have tried:

I have tried many ways:
-Like adding object sender1 instead of object sender from one of the methods
-Deleting object sender from one of the methods
-Deleting EventArgs e from one of the methods
-Adding string s after EventArgs to one of the methods and then use it like
{
MessageBox.Show(s)
}
Nevertheless, Nothing seems to work.
Thanks
Posted
Updated 8-Dec-21 23:18pm

Why are you using Convert.ToInt32 to get a float type? But you should not use Convert at all, as it will crash your app if the user types garbage in the text box. You should always use Single.TryParse Method (System) | Microsoft Docs[^] or if you are only using integers then Int32.TryParse Method (System) | Microsoft Docs[^]. In that way you can catch any bad input at source.
 
Share this answer
 
No, there's no point to it. Your event handler method MUST follow a specific method definition or it won't be called.

You should move the code you have in the event handlers to other methods, calling them from the event handlers. Then you open yourself up to using method overloading, but since this is a school assignment, you're going to get any code from us.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900