Reputation: 35
I know that you cannot divide anything by zero.
But, is it possible to have visual studio to give a result of zero if any arithmetic operation attempts to divide by zero?
if so, how?
for this particular project, it would actually save me a lot of work and code if I could allow anything that is / 0 = 0
I am looking to have the result of an integer divided by zero to equal zero. VB.NET Visual Studio 2010.
Upvotes: 1
Views: 3331
Reputation: 1674
Why not using if in a static method if you want to access it in all computation?
C#/Java code
public static int myDivision(int dividend, int divisor)
{
//return is the answer
if(divisor == 0)
{ return 0; }
else
{ return dividend/divisor; }
//just do something like this
}
or for something clearer
public static int myDivision(int dividend, int divisor)
{
int quotient = 0;
if(divisor == 0)
{ return quotient; }
else
{
quotient = dividend/divisor;
return quotient;
}
//just do something like this
}
//note: you may also use it through an object method
Upvotes: 1
Reputation: 62
I think the best solution is simply by checking the divisor of the division with an if statement. You can decide to perform the division if the divisor is not 0.
Upvotes: 2
Reputation: 226
Try Catch
Catch the exception Divide By Zero
Do as you will.
Upvotes: 1