views:

114

answers:

3

I was reading a Business Primitives by CodeBetter.com and was toying around with the idea.

Taking his example of Money, how would one implement this in a way that it can be used similarily as regular value types?

What I mean by that is do this:

Money myMoney = 100.00m;

Instead of:

Money myMoney = new Money(100.00m);

I understand how to override all the operators to allow for functionality doing math etc, but I don't know what needs to be overriden to allow what I'm trying to do.

The idea of this is to minimize code changes required when implementing the new type, and to keep the same idea that it is a primitive type, just with a different value type name and business logic functionality.

Ideally I would have inherited Integer/Float/Decimal or whatever required, and override as needed, however obviously that is not available to structures.

A: 

Please go through to get more clarity about this http://www.csharphelp.com/2006/03/c-operator-overloading/

Ravisha
+5  A: 

You could provide an implicit cast operator from decimal to Money like so:

class Money {
    public decimal Amount { get; set; }
    public Money(decimal amount) {
        Amount = amount;
    }
    public static implicit operator Money(decimal amount) {
        return new Money(amount);
    }
}

Usage:

Money money = 100m;
Console.WriteLine(money.Amount);

Now, what is happening here is not that we are overloading the assignment operator; that is not possible in C#. Instead, what we are doing is providing an operator that can implicitly cast a decimal to Money when necessary. Here we are trying to assign the decimal literal 100m to an instance of type Money. Then, behind the scenes, the compiler will invoke the implicit cast operator that we defined and use that to assign the result of the cast to the instance money of Money. If you want to understand the mechanisms of this, read §7.16.1 and §6.1 of the C# 3.0 specification.

Please note that types that model money should be decimal under-the-hood as I have shown above.

Jason
Aha, that's what I was looking for!Is it possible to convert it implicitly to a type it is being assigned to?Could I for instance have:decimal dec = myMoney;
Aequitarum Custos
@Aequitarum Custos: Yes, you absolutely could. Just say `public static implicit operator decimal(Money money) { return money.Amount; }` and then `decimal d = money` is perfectly legal. So the magic words you have to incant are `public static implicit operator` and then you give the return type (so here `decimal`) and then, as the single parameter to the operator, a parameter of the type you want to convert (so here `Money`). Note that one of the return type or the type of the parameter must be the type of the enclosing class. Thus `public static implicit operator string(int i)` is not legal.
Jason
Good to know it works both ways, thanks!
Aequitarum Custos
A: 

The assignment operator cannot be defined in C#, but for this kind of assignment you can overload the implicit cast operator:

(inside Money class)

public static implicit operator Money(double value)
{
    return new Money(value);
}

Note: I recommend using decimal for accurate money calculation

Alex LE
Please never use double for representing money!
Dan McGrath
@Dan that's why there's a note, the user is using a double literal.
Alex LE
Sorry, when I was typing it, that note wasn't there.
Dan McGrath