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. C# creating my own class that acts like a string

C# creating my own class that acts like a string

Scheduled Pinned Locked Moved C#
csharpcomtutorial
9 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.
  • H Offline
    H Offline
    hpjchobbes
    wrote on last edited by
    #1

    I am trying to create some classes that act like a string, but allow me to test the type at a later date. Because string and String are sealed, I can't seem to inherit from them. I also found out you can't override the assignment operator. I found something close to what I want here on stackoverflow, but it uses structs which can't be inherited. Here's a example of what I want to do, but I can't figure out how:

    public class ORSerialLotNumber : string {}
    public class SerialNumber : ORSerialLotNumber {}
    public class LotNumber : ORSerialLotNumber {}

    // ......

    List mySLNList = new List();
    SerialNumber sNum1 = "Sd-23s-3sf";
    LotNumber nNum1 = "2342434";

    mySLNList.Add(sNum1);
    mySLNList.Add(nNum1);

    // ......

    foreach(var v in mySLNList)
    if(v.GetType() == typeof(SerialNumber))
    string serial = v;
    if(v.GetType() == typeof(LotNumber))
    string lot = v;

    L B 2 Replies Last reply
    0
    • H hpjchobbes

      I am trying to create some classes that act like a string, but allow me to test the type at a later date. Because string and String are sealed, I can't seem to inherit from them. I also found out you can't override the assignment operator. I found something close to what I want here on stackoverflow, but it uses structs which can't be inherited. Here's a example of what I want to do, but I can't figure out how:

      public class ORSerialLotNumber : string {}
      public class SerialNumber : ORSerialLotNumber {}
      public class LotNumber : ORSerialLotNumber {}

      // ......

      List mySLNList = new List();
      SerialNumber sNum1 = "Sd-23s-3sf";
      LotNumber nNum1 = "2342434";

      mySLNList.Add(sNum1);
      mySLNList.Add(nNum1);

      // ......

      foreach(var v in mySLNList)
      if(v.GetType() == typeof(SerialNumber))
      string serial = v;
      if(v.GetType() == typeof(LotNumber))
      string lot = v;

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      That example doesn't really depend on it being a struct, it's about the implicit conversion operator. Here's a minimal implementation of a class to implicitly converts to and from string:

      class CustomString
      {
      public static implicit operator CustomString(string s)
      {
      return new CustomString();
      }

      public static implicit operator string(CustomString s)
      {
          return "";
      }
      

      }

      Do that for both subclasses. By the way, as far as I can tell you should be able to use is[^] instead of the GetType() == typeof() "not-quite-anti-pattern-but-close".

      H 1 Reply Last reply
      0
      • L Lost User

        That example doesn't really depend on it being a struct, it's about the implicit conversion operator. Here's a minimal implementation of a class to implicitly converts to and from string:

        class CustomString
        {
        public static implicit operator CustomString(string s)
        {
        return new CustomString();
        }

        public static implicit operator string(CustomString s)
        {
            return "";
        }
        

        }

        Do that for both subclasses. By the way, as far as I can tell you should be able to use is[^] instead of the GetType() == typeof() "not-quite-anti-pattern-but-close".

        H Offline
        H Offline
        hpjchobbes
        wrote on last edited by
        #3

        Thanks, this seems to be just what I need, though I imagine that I need to save the actual string value and have the constructor set that value and the operator string(CustomString s) return the string instead of an empty string?

        class CustomString
        {
        private string _value;

        private CustomString(string s)
        {
            \_value = s;
        }
        
        public static implicit operator CustomString(string s)
        {
            return new CustomString(s);
        }
        
        public static implicit operator string(CustomString s)
        {
            return s.\_value;
        }
        

        }

        Would I also be able to implement INotifyPropertyChanged with this class? I don't see how as there's no public property. Would I just use "_value" even though it's private?

        L 1 Reply Last reply
        0
        • H hpjchobbes

          Thanks, this seems to be just what I need, though I imagine that I need to save the actual string value and have the constructor set that value and the operator string(CustomString s) return the string instead of an empty string?

          class CustomString
          {
          private string _value;

          private CustomString(string s)
          {
              \_value = s;
          }
          
          public static implicit operator CustomString(string s)
          {
              return new CustomString(s);
          }
          
          public static implicit operator string(CustomString s)
          {
              return s.\_value;
          }
          

          }

          Would I also be able to implement INotifyPropertyChanged with this class? I don't see how as there's no public property. Would I just use "_value" even though it's private?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You could just give it a public property though, right?

          OriginalGriffO H 2 Replies Last reply
          0
          • L Lost User

            You could just give it a public property though, right?

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            There wouldn't be a whole lot of point in implementing the interface if he doesn't! :laugh:

            Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • L Lost User

              You could just give it a public property though, right?

              H Offline
              H Offline
              hpjchobbes
              wrote on last edited by
              #6

              So the idea would be to include a public property, but still have the implicit operator act on that property? Would that then allow someone to either use the implicit operator or the property directly?

              L 1 Reply Last reply
              0
              • H hpjchobbes

                So the idea would be to include a public property, but still have the implicit operator act on that property? Would that then allow someone to either use the implicit operator or the property directly?

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Yes, is it giving you any trouble?

                H 1 Reply Last reply
                0
                • L Lost User

                  Yes, is it giving you any trouble?

                  H Offline
                  H Offline
                  hpjchobbes
                  wrote on last edited by
                  #8

                  No, I just didn't even think about this, or even know that it was possible. I'm reading more about `implicit`, which seems very useful. It doesn't seem to be touched on in most 'beginner' reading, though.

                  1 Reply Last reply
                  0
                  • H hpjchobbes

                    I am trying to create some classes that act like a string, but allow me to test the type at a later date. Because string and String are sealed, I can't seem to inherit from them. I also found out you can't override the assignment operator. I found something close to what I want here on stackoverflow, but it uses structs which can't be inherited. Here's a example of what I want to do, but I can't figure out how:

                    public class ORSerialLotNumber : string {}
                    public class SerialNumber : ORSerialLotNumber {}
                    public class LotNumber : ORSerialLotNumber {}

                    // ......

                    List mySLNList = new List();
                    SerialNumber sNum1 = "Sd-23s-3sf";
                    LotNumber nNum1 = "2342434";

                    mySLNList.Add(sNum1);
                    mySLNList.Add(nNum1);

                    // ......

                    foreach(var v in mySLNList)
                    if(v.GetType() == typeof(SerialNumber))
                    string serial = v;
                    if(v.GetType() == typeof(LotNumber))
                    string lot = v;

                    B Offline
                    B Offline
                    BobJanova
                    wrote on last edited by
                    #9

                    I'm not quite sure what you're trying to achieve here. I think it is that you want a data object that has a 'number' (a string identifier) and also a type. Using GetType or is/as functions is generally a sign that you haven't quite got the solution you want; switching based on type usually implies you need more polymorphic behaviour. I think the better solution is

                    enum ItemType { Serial, Lot };
                    struct ObjectIdentifier {
                    string Identity { get; set; }
                    ItemType Type { get; set; }
                    }

                    List<ObjectIdentifier> mySLNList = ...;

                    foreach(ObjectIdentifier oi in mySLNList)
                    switch(oi.Type)
                    ...

                    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