473,395 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

subclassing primitve types

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
Jan 17 '07 #1
5 1302
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.
"umop apisdn" <ne**********@yahoo.comschrieb im Newsbeitrag
news:45***********************@news.free.fr...
>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

Jan 17 '07 #2
Since you appear to have access tot he source code, you can use search &
replace to change all the singles to doubles.

Mike.

"umop apisdn" <ne**********@yahoo.comwrote in message
news:45***********************@news.free.fr...
>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

Jan 17 '07 #3
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.
Jan 17 '07 #4
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

"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comwrote in message
news:c1**************************@news.microsoft.c om...
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.


Jan 17 '07 #5
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.
Jan 17 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: WhiteRavenEye | last post by:
Why can't I subclass any window except mine in VB? Do I have to write dll for this? I've tried to subclass it with SetWindowLong but without success... Does anyone know how to subclass window...
1
by: Jacek Generowicz | last post by:
The Numeric docs state that "Subclassing Numeric arrays is not possible due to a limitation of Python." What is this limitation? My first guess is that it is the unsbclassability of built-in...
4
by: GrelEns | last post by:
hello, i wonder if this possible to subclass a list or a tuple and add more attributes ? also does someone have a link to how well define is own iterable object ? what i was expecting was...
2
by: Fuzzyman | last post by:
I recently went through a bit of a headache trying to subclass string.... This is because the string is immutable and uses the mysterious __new__ method rather than __init__ to 'create' a string....
11
by: Iker Arizmendi | last post by:
Hello all, I've defined a class in a C extension module that is (or rather, I would like to be) a subclass of another class. I do this by simply setting the tp_base pointer of my subclass's...
2
by: BJörn Lindqvist | last post by:
A problem I have occured recently is that I want to subclass builtin types. Especially subclassing list is very troublesome to me. But I can't find the right syntax to use. Take for example this...
12
by: Jane Austine | last post by:
Please see the following code: -------------------------------- class rev_wrap(object): def __init__(self,l): self.l=l def __getitem__(self,i): return self.l class rev_subclass(list): def...
5
by: Pieter Linden | last post by:
Hi, This question refers sort of to Rebecca Riordan's article on Access web about subclassing entities: http://www.mvps.org/access/tables/tbl0013.htm How practical is this? I am writing a...
16
by: manatlan | last post by:
I've got an instance of a class, ex : b=gtk.Button() I'd like to add methods and attributes to my instance "b". I know it's possible by hacking "b" with setattr() methods. But i'd like to do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.