Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made this calculator in C# and it was working fine, but, when I add a method with argument modifier out for the percentage option, the calculator stopped working and the error statement declares that "
the out parameter must be assigned to before control leaves the current method
", how is that possible? I am perfectly assigning the value before control leaves the method. Any idea why is this happening?
Thanks

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

            Button button = new Button
            {

                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.Center

            };

            button.Clicked += async (sender, args) =>
            {
                await Navigation.PushAsync(new MainPage());

            };

        }
        private void Button_Click(object sender, EventArgs e)
        {
          
            var key = operation.Text;
            

            int num1 = Convert.ToInt32(number1.Text);

            int num2 = int.Parse(number2.Text);
            float intpercent = 0;
            label3.Text = calculator.DoSomeMath(num1, num2, key).ToString();

            calculator.DoSomeMath(num1, num2, out intpercent, key);
                  
        }
        public class calculator
        {
          
            public static int DoSomeMath(int num1, int num2, string key)
            {
              
                string operation=key; 
                if(operation=="Add")
                {                 
                    int sum = num1 + num2;

                    return sum;
                } else if (operation == "Minus")
                {
                    int substract = (int)(num1 - num2);
                    return substract;
                } else if (operation == "Multiply")
                {
                    int Multiply = (int)(num1 * num2);

                    return Multiply;
                }
                else if (operation == "Divide")
                {
                    int Divide = (int)(num1/num2);

                    return Divide;
                }else
                return 0;
               

            }
            public static float DoSomeMath(float num1, float num2, string key)
            {

                string operation = key;
                if (operation == "Add")
                {

                    float sum = num1 + num2;

                    return sum;
                }
                else if (operation == "Minus")
                {
                    float Substract = (float)(num1 - num2);
                    return Substract;
                }
                else if (operation == "Multiply")
                {
                    float Multiply = (float)(num1 * num2);

                    return Multiply;
                }
                else if (operation == "Divide")
                {
                    float Divide = (float)(num1 / num2);

                    return Divide;
                }
                return 0;

            }
          
            public static void DoSomeMath(int num1, int num2, out float percentage, string key)
            {
                
                string operation = key;
                do
                {
                    percentage = ((float)num1 / (float)num2) * 100;
                } while (operation == "percentage");



            }

        }


    }
}


What I have tried:

-I tried by changing the argument modifier out by ref.
-I placed
percentage = ((float)num1 / (float)num2) * 100;
before the method
Posted
Updated 18-Dec-21 20:30pm

1 solution

If I copy and paste your code into an online compiler:
using System;
namespace ReadXml1
{
    internal class Class1
    {
        private static void Main(string[] args)
        {
			string[] data = {"hello", "world"};
			Console.WriteLine(String.Join(" ", data));
            int num1 = 666;
			string key = "Add";
            int num2 = 3;
            float intpercent = 0;
            string Text = calculator.DoSomeMath(num1, num2, key).ToString();
			Console.WriteLine(Text);
            calculator.DoSomeMath(num1, num2, out intpercent, key);
			Console.WriteLine(intpercent);
        }
    }
        public class calculator
        {
          
            public static int DoSomeMath(int num1, int num2, string key)
            {
              
                string operation=key; 
                if(operation=="Add")
                {                 
                    int sum = num1 + num2;

                    return sum;
                } else if (operation == "Minus")
                {
                    int substract = (int)(num1 - num2);
                    return substract;
                } else if (operation == "Multiply")
                {
                    int Multiply = (int)(num1 * num2);

                    return Multiply;
                }
                else if (operation == "Divide")
                {
                    int Divide = (int)(num1/num2);

                    return Divide;
                }else
                return 0;
               

            }
            public static float DoSomeMath(float num1, float num2, string key)
            {

                string operation = key;
                if (operation == "Add")
                {

                    float sum = num1 + num2;

                    return sum;
                }
                else if (operation == "Minus")
                {
                    float Substract = (float)(num1 - num2);
                    return Substract;
                }
                else if (operation == "Multiply")
                {
                    float Multiply = (float)(num1 * num2);

                    return Multiply;
                }
                else if (operation == "Divide")
                {
                    float Divide = (float)(num1 / num2);

                    return Divide;
                }
                return 0;

            }
          
            public static void DoSomeMath(int num1, int num2, out float percentage, string key)
            {
                
                string operation = key;
                do
                {
                    percentage = ((float)num1 / (float)num2) * 100;
                } while (operation == "percentage");



            }

        }
}
It compiles and runs cleanly. I get results:
hello world
669
22200
So it's unlikely that your code as shown is generating that error: If you have had this:
public static void DoSomeMath(int num1, int num2, out float percentage, string operation)
{
    while (operation == "percentage")
    {
        percentage = ((float)num1 / (float)num2) * 100;
    }
}
Or this:
public static void DoSomeMath(int num1, int num2, out float percentage, string operation)
{
    if (operation == "percentage")
    {
        percentage = ((float)num1 / (float)num2) * 100;
    }
}
Then you would, because in both cases there is a path through the code where the value of percentage is not assigned.
But your do ... whileloop always executes a minimum of once so it is always set.

Check your code: I don't think you are compiling what you think you are!

BTW: setting the value of an out parameter before calling a method is irrelevant as teh value is not passed into the method at all!
 
Share this answer
 
Comments
Diasalva5 19-Dec-21 14:48pm    
Dear OriginalGriff
Thank you for your attention to my code.
You used my code in a console. I made my code for a Windows form Xamarin. I tried again and is still declaring the same and I really don't know what else to do. Thanks
OriginalGriff 19-Dec-21 14:53pm    
It doesn't matter if it's a console, Xamarin, Winforms, Website, WPF, or Service: they all behave in the same way as far as out modifiers go.
Diasalva5 19-Dec-21 15:02pm    
Dear OriginalGriff
I copied the whole code (Both C# and XAML ) and I opened a new project and now it works!!, however when I enter percentage, it returns a 0. Any idea why?
Thanks
Diasalva5 19-Dec-21 15:15pm    
I got it, Thanks
OriginalGriff 19-Dec-21 17:41pm    
You're welcome!

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