Josh Hornsbyu
Josh Hornsbyu

Reputation: 31

Make own datatype without new modifier

I am trying to make a Money datatype (like int, string) in C#. However I can't seem to work out how to use it without the new Modifier. The class cannot be static as it has to be assigned to. I figured there must be a way, here is the code I have for the class, I may be doing this completely wrong.

public class Money {
    private float _raw;

    public float Raw {
        get {  return _raw;  }
        set {  _raw = value;  }
    }

    public string Pound {
        get {  return "£" + string.Format("{0:0.00}", _raw);  }
    }
} 

Then I have the class I am calling it in and would like to just use:

private Money _money;

Instead of:

private Money _money = new Money();

Sorry if this is a stupid question but I couldn't find anything online nor could I figure it out myself.

Upvotes: 3

Views: 796

Answers (5)

adt
adt

Reputation: 4360

Using impilicit operator will allow you to set it like primitive data type.

http://haacked.com/archive/2012/09/30/primitive-obsession-custom-string-types-and-self-referencing-generic-constraints.aspx

public class Money

{
    private float _raw;

    public float Raw
    {
        get { return _raw; }
        set { _raw = value; }
    }

    public string Pound
    {
        get { return "£" + string.Format("{0:0.00}", _raw); }
    }


    public static implicit operator Money(float value)
    {
        return new Money(){Raw = value};
    }
}

Then you can use it like this.

 Money m = 12;
 Console.Write(m.Raw);

Upvotes: 2

bateloche
bateloche

Reputation: 709

If you do not want to use property Raw to assign value you can make a operator overload, something like that:

public static implicit operator Money(Double value)
{
    var money = new Money();

    money.Raw = value;

    return money;
}

And then in the calling code:

Money money = 10d;

But IMHO, i don't see any advantage in doing that, if you really need a muttable type, there should be no problem calling 'new' to instantiate it.

Upvotes: 1

Tim
Tim

Reputation: 15237

Adding a private constructor would prevent outsiders from "newing up" an instance... but then you also need to write a factory method or something to create it (in your class so that it has access to the private constructor). Otherwise your class is pretty useless!

That said, maybe if we knew why you didn't want to be able to "new up" an instance, we might be able to provide some tips.

Upvotes: 0

user47589
user47589

Reputation:

An alternative solution involves using a factory method of some kind.

public class Money {
    private float _raw;

    public float Raw {
        get {  return _raw;  }
        set {  _raw = value;  }
    }

    public string Pound {
        get {  return "£" + string.Format("{0:0.00}", _raw);  }
    }

    public static Money From(float val) 
    {
        Money x = new Money();
        x.Raw = val;
        return x;
    }
} 

usage:

Money m = Money.From(9.95);

Upvotes: 3

Robert Harvey
Robert Harvey

Reputation: 180868

You'll have to new it up somewhere. If you don't want to do it in the member declaration, then do it in the class constructor:

public MyClass()
{
     _money = new Money();
}

Upvotes: 3

Related Questions