Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to extend System.Int32?

How to extend System.Int32?

Scheduled Pinned Locked Moved C#
tutorialquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ralfoide
    wrote on last edited by
    #1

    Hi! I'd like to create an "id" class, i.e. a class which could hold any positive non-null integer, yet somehow semantically generated ids would be > 0, but 0 and -1 would have special meanings. Basically something like that: public class MyId: System.Int32 { public const int kServer = 0; public const int kBroadcast = -1; } Unfortunately that is not possible since System.Int32 is a struct and thus equivalent to a sealed class: I can't derive it. Can anyone suggest another approach to this? Maybe the class could just not derive from Int32 and just contain an Int32, generated to be unique from the constructor. Thanks in advance, all ideas welcome. R/

    E L 2 Replies Last reply
    0
    • R ralfoide

      Hi! I'd like to create an "id" class, i.e. a class which could hold any positive non-null integer, yet somehow semantically generated ids would be > 0, but 0 and -1 would have special meanings. Basically something like that: public class MyId: System.Int32 { public const int kServer = 0; public const int kBroadcast = -1; } Unfortunately that is not possible since System.Int32 is a struct and thus equivalent to a sealed class: I can't derive it. Can anyone suggest another approach to this? Maybe the class could just not derive from Int32 and just contain an Int32, generated to be unique from the constructor. Thanks in advance, all ideas welcome. R/

      E Offline
      E Offline
      Eric Gunnerson msft
      wrote on last edited by
      #2

      Containing a int32 is your best bet. If you'd like, you can write implicit conversions to and from int32, so you'd be able to write: MyID id = 13; or code like that.

      R 1 Reply Last reply
      0
      • R ralfoide

        Hi! I'd like to create an "id" class, i.e. a class which could hold any positive non-null integer, yet somehow semantically generated ids would be > 0, but 0 and -1 would have special meanings. Basically something like that: public class MyId: System.Int32 { public const int kServer = 0; public const int kBroadcast = -1; } Unfortunately that is not possible since System.Int32 is a struct and thus equivalent to a sealed class: I can't derive it. Can anyone suggest another approach to this? Maybe the class could just not derive from Int32 and just contain an Int32, generated to be unique from the constructor. Thanks in advance, all ideas welcome. R/

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        As siad by Eric your best best is to make a continer class. Stephen Toub from MS has written an execellent (as all his stuff) utility for this. Search GotDotNet for stoub (his username).

        leppie::AllocCPArticle(Generic DFA State Machine for .NET);

        D 1 Reply Last reply
        0
        • E Eric Gunnerson msft

          Containing a int32 is your best bet. If you'd like, you can write implicit conversions to and from int32, so you'd be able to write: MyID id = 13; or code like that.

          R Offline
          R Offline
          ralfoide
          wrote on last edited by
          #4

          I understand how to write an implicit conversion to in32, but I don't get how to write an implicit conversion from int32 to my class. Currently I came up with something like that: public struct MyId { public const int kServer = 0; public const int kBroadcast = -1; public override bool Equals(object obj) { return mId.Equals(obj); } public override string ToString() { return mId.ToString(); } public override int GetHashCode() { return mId.GetHashCode(); } public static implicit operator int(MyId id) { return id.mId; } private int mId; } The C# reference says operator = cannot be overloaded. What I my missing so far? Is it useful to overload GetHashCode/ToString/Equal? TIA R/

          R E 2 Replies Last reply
          0
          • R ralfoide

            I understand how to write an implicit conversion to in32, but I don't get how to write an implicit conversion from int32 to my class. Currently I came up with something like that: public struct MyId { public const int kServer = 0; public const int kBroadcast = -1; public override bool Equals(object obj) { return mId.Equals(obj); } public override string ToString() { return mId.ToString(); } public override int GetHashCode() { return mId.GetHashCode(); } public static implicit operator int(MyId id) { return id.mId; } private int mId; } The C# reference says operator = cannot be overloaded. What I my missing so far? Is it useful to overload GetHashCode/ToString/Equal? TIA R/

            R Offline
            R Offline
            ralfoide
            wrote on last edited by
            #5

            Funny, I could not find any reference to Stephen Toub in GotDotNet, yet I found a nice team page for Eric and his article on MSDN regarding operator overloading :-) Anyway, I am in the right direction when I say that: - it's a C++-programmer reflex of mine to expect to be able to overload operator=() - in the case above, I actually don't need such an operator=, what I need is an operator from int to MyId, such as: public static implicit operator MyId(int id) { MyId v = new MyId; v.mId = id; return v; } R/

            L 1 Reply Last reply
            0
            • R ralfoide

              Funny, I could not find any reference to Stephen Toub in GotDotNet, yet I found a nice team page for Eric and his article on MSDN regarding operator overloading :-) Anyway, I am in the right direction when I say that: - it's a C++-programmer reflex of mine to expect to be able to overload operator=() - in the case above, I actually don't need such an operator=, what I need is an operator from int to MyId, such as: public static implicit operator MyId(int id) { MyId v = new MyId; v.mId = id; return v; } R/

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              To "override" the = operator, all u need to do is an implicit cast :)

              leppie::AllocCPArticle(Generic DFA State Machine for .NET);

              1 Reply Last reply
              0
              • L leppie

                As siad by Eric your best best is to make a continer class. Stephen Toub from MS has written an execellent (as all his stuff) utility for this. Search GotDotNet for stoub (his username).

                leppie::AllocCPArticle(Generic DFA State Machine for .NET);

                D Offline
                D Offline
                David Stone
                wrote on last edited by
                #7

                leppie wrote: Search GotDotNet for stoub (his username). I had to look on his blog just to find his username...it's toub...not stoub. ;P


                Hawaian shirts and shorts work too in Summer. People assume you're either a complete nut (in which case not a worthy target) or so damn good you don't need to worry about camouflage... -Anna-Jayne Metcalfe on Paintballing

                1 Reply Last reply
                0
                • R ralfoide

                  I understand how to write an implicit conversion to in32, but I don't get how to write an implicit conversion from int32 to my class. Currently I came up with something like that: public struct MyId { public const int kServer = 0; public const int kBroadcast = -1; public override bool Equals(object obj) { return mId.Equals(obj); } public override string ToString() { return mId.ToString(); } public override int GetHashCode() { return mId.GetHashCode(); } public static implicit operator int(MyId id) { return id.mId; } private int mId; } The C# reference says operator = cannot be overloaded. What I my missing so far? Is it useful to overload GetHashCode/ToString/Equal? TIA R/

                  E Offline
                  E Offline
                  Eric Gunnerson msft
                  wrote on last edited by
                  #8

                  Here's how you overload the conversion from int to your class: public static implicit operator MyId(int id) { return new MyId(id); }

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups