3

How to add an argument modifier to a method in C#?

 2 years ago
source link: https://www.codeproject.com/Questions/5320389/How-to-add-an-argument-modifier-to-a-method-in-Csh
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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 "

Copy Code
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
Expand ▼   Copy Code
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
Copy Code
percentage = ((float)num1 / (float)num2) * 100;
before the method

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK