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 ... while
loop 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!