subclassing primitve types

  • Thread starter Thread starter umop apisdn
  • Start date Start date
U

umop apisdn

I have a bunch of properties like this:

private Single _Amount;
public Single Amount
{
get { return _Amount; }
set { _Amount = value; }
}

Now I need to change all the Singles to Doubles. So that I don't have to do
this again, how can I define a type

MyAmountType = typeof(Double)

so that I can declare

private MyAmountType _Amount;
public MyAmountType Amount
{
get { return _Amount; }
set { _Amount = value; }
}

?

Thanks for any help,

Rob
 
Since you appear to have access tot he source code, you can use search &
replace to change all the singles to doubles.

Mike.
 
Hi umop,

you could use an using-alias-directive:

using MyAmountType = Double;

a drawback of this approach is, that you must add this to any codefile,
where you use it and maintain it parallel.
 
I have a bunch of properties like this:
private Single _Amount;
public Single Amount
{
get { return _Amount; }
set { _Amount = value; }
}
Now I need to change all the Singles to Doubles. So that I don't have
to do this again, how can I define a type

MyAmountType = typeof(Double)

so that I can declare

private MyAmountType _Amount;
public MyAmountType Amount
{
get { return _Amount; }
set { _Amount = value; }
}
?

A technique that I've used with some success is to create a new struct that
wraps the primitive and implicitly convertible to the primitive type. For
example:

public struct MyAmountType: IEquatable<MyAmountType>
{
private double m_Value;

public MyAmountType(double value)
{
m_Value = value;
}

public override bool Equals(object obj)
{
if (obj is MyAmountType)
return Equals((MyAmountType)obj);

return base.Equals(obj);
}
public bool Equals(MyAmountType other)
{
return m_Value.Equals(other.m_Value);
}
public override int GetHashCode()
{
return m_Value.GetHashCode();
}
public override string ToString()
{
return m_Value.ToString();
}

public static implicit operator double(MyAmountType obj)
{
return obj.m_Value;
}
public static implicit operator MyAmountType(double obj)
{
return new MyAmountType(obj);
}
}

That isn't a full implementation but you get the idea.

Best Regards,
Dustin Campbell
Developer Express Inc.
 
Thanks for the suggestions. Dustin's is the closest to what I'm looking
for, but it's rather long winded, since I might have several different types
of amounts, e.g.,

MyCurrencyRateType = Decimal
MyPercentageType = Single
MyAmountType = Double
etc.

In Delphi I would just have gone:
type
MyAmountType = Double;

Is there nothing similar in C#?

Rob
 
Thanks for the suggestions. Dustin's is the closest to what I'm
looking for, but it's rather long winded, since I might have several
different types of amounts, e.g.,

MyCurrencyRateType = Decimal
MyPercentageType = Single
MyAmountType = Double
etc.
In Delphi I would just have gone:
type
MyAmountType = Double;
Is there nothing similar in C#?

Just the type-aliases that Christof mentioned but those are scoped to the
file that they are declared in and C# doesn't support #include files so that
doesn't help either.

Best Regards,
Dustin Campbell
Developer Express Inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top