Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Can i avoid typecasting all the time by overloading an operator

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
K Viltersten - 19 Mar 2008 11:11 GMT
I'm working on a project where i use a
lot of sbyte-type variables. I notice
that most (all?) operators tend to cast
the result into int type.

While i can easily recast e.g.

 sbyte something = else % 10;

into

 sbyte something = (sbyte)(else % 10);

it make the code look less readable
and is a bit tedious. I've been thaugt
that overloading is a no-no, but
perhaps it's a good idea to do it here.

1. Can i overload e.g. modulo operator
using some sort of extension method,
in such manner so it looks like "%"
still but returns sbyte type?

2. Should i do so?

3. How (if not easily googleable and/or
connected to evil gotchas)?

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 19 Mar 2008 11:16 GMT
> I'm working on a project where i use a
> lot of sbyte-type variables. I notice
[quoted text clipped - 23 lines]
> 3. How (if not easily googleable and/or
> connected to evil gotchas)?

Do all your sbyte variables actually have the same logical type? If so,
why not encapsulate that in your own type, instead of using sbyte? (It
would probably be *backed* by an sbyte).

Then you can define your own operators for your type, which will
probably all return values of the same type.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

K Viltersten - 19 Mar 2008 19:54 GMT
> While i can easily recast e.g.
>
[quoted text clipped - 8 lines]
> that overloading is a no-no, but
> perhaps it's a good idea to do it here.

Hrmp... I Don't think this code would
compile, due to the name of the variable.

I can't believe nobody noticed it! I just
saw it myself! Shame on you, Mr. Skeet,
for missing that! It was an open goal...  :)

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 19 Mar 2008 20:49 GMT
> > While i can easily recast e.g.
> >
[quoted text clipped - 11 lines]
> Hrmp... I Don't think this code would
> compile, due to the name of the variable.

I'd kind of assumed it was pseudo-code... You could always make it:

sbyte something = (sbyte)(@else % 10);

Best not to though.

> I can't believe nobody noticed it! I just
> saw it myself! Shame on you, Mr. Skeet,
> for missing that! It was an open goal...  :)

;)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Mythran - 20 Mar 2008 17:27 GMT
> it make the code look less readable
> and is a bit tedious. I've been thaugt
> that overloading is a no-no, but perhaps it's a good idea to do it here.

Wha?!?  Who told you that overloading is a no-no?  Operating overloading is
there for a reason, and it works great!  Why would someone not want you
using operating overloading (if used appropriately and correctly)?

Mythran
K Viltersten - 20 Mar 2008 20:20 GMT
>> it make the code look less readable
>> and is a bit tedious. I've been thaugt
[quoted text clipped - 7 lines]
> operating overloading (if used
> appropriately and correctly)?

One of my supervisors during my master's
thesis. I'm not claiming him to be right
nor wrong (and perhaps it was a different
type of project).

Anyhow, i'm taking your advice gladly.

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Jon Skeet [C# MVP] - 20 Mar 2008 23:10 GMT
> >> it make the code look less readable
> >> and is a bit tedious. I've been thaugt
[quoted text clipped - 14 lines]
>
> Anyhow, i'm taking your advice gladly.

I would say that operator overloading should be used *very* cautiously.
I can't remember the last time I felt the need to do it (aside from
demo Complex structs). When it's useful, it's very useful. When it's
abused, it's a readability nightmare.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Mythran - 21 Mar 2008 16:54 GMT
>> >> it make the code look less readable
>> >> and is a bit tedious. I've been thaugt
[quoted text clipped - 19 lines]
> demo Complex structs). When it's useful, it's very useful. When it's
> abused, it's a readability nightmare.

Aye, Jon, and as you probably noticed, I did add at the end of my previous
post "if used appropriately and correctly".  But I probably could have used
a more precise term or statement that made it clearer.

--- BELOW IS AN OFF TOPIC REMARK ---

Jon, I was just gonna email ya before you posted this reply....

Read the following link:

http://www.evga.com/articles/395.asp

Now buy it for me.

Ok?  Whew, glad I convinced ya to send it to me.

Thanks,
Mythran

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.