PDA

View Full Version : c# interfaces problem


Bassmansam
10-16-2009, 02:18 PM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CalculatorApplication
{
interface iCalculator
{
decimal add(decimal a, decimal b);

// void subtract(decimal a, decimal b);



// void divide(decimal a, decimal b);



//void multiply(decimal a, decimal b);

}
}

Interface


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CalculatorApplication
{

public class Calculator: iCalculator


{
public static decimal result;

public decimal add(decimal a, decimal b)
{
result = a + b;
return result;
}



}
}

Class with method implemented

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CalculatorApplication
{
class Class1 : iCalculator
{

decimal k = Calculator.result;
// decimal add(decimal a, decimal b);

public decimal round(decimal j)
{

j = Math.Round(k, 2);




return j;


}
}
}


The problem is here , I am trying to use this class to round the result's from the Calculator class to two decimals , but i can't get it to work. Anyone got any ideas? Thanks

oracleguy
10-16-2009, 05:06 PM
I don't get what you are trying to do, there is no reason to implement an interface. Though I would have expected your Class1 to inherit from Calculator and not iCalculator.

Your k variable in Class1 will only get set once when the class is instantiated. Decimals are value types, not reference types so the value is copied, there isn't a pointer.

If you want a calculator that lets you set how much rounding you want, just either make it a property you can set in the Calculator class or use a decorator. In either case, the interface isn't needed.