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. Extending Classes?

Extending Classes?

Scheduled Pinned Locked Moved C#
question
12 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.
  • T TyronX

    I created a own datatype called ID. Now I want to have a function which converts a string into an ID. So I thought it would be the best to kind of append a method to the System.Convert class, so that I can simply call it with Convert.ToID("somewhat"); Is that possible? P.S.: Sorry for asking so much ^^

    D Offline
    D Offline
    DavidNohejl
    wrote on last edited by
    #2

    See MSDN[^]. This should be what you are looking for.. Changes in .NET classes are possible, bt I don't like this idea (in this case). Why not to make static function ID.FromString(string s)? And hey! It is in Convert class... MSDN[^] hmm thats cool! TyronX wrote: P.S.: Sorry for asking so much ^^ nah... how would we earn gold memberships, etc.? :P Plus from some question (like this one) I learnt something new.. isn't iot great? :-D best regards, David 'DNH' Nohejl Never forget: "Stay kul and happy" (I.A.)

    D 1 Reply Last reply
    0
    • T TyronX

      I created a own datatype called ID. Now I want to have a function which converts a string into an ID. So I thought it would be the best to kind of append a method to the System.Convert class, so that I can simply call it with Convert.ToID("somewhat"); Is that possible? P.S.: Sorry for asking so much ^^

      D Offline
      D Offline
      Dennis C Dietrich
      wrote on last edited by
      #3

      TyronX wrote: So I thought it would be the best to kind of append a method to the System.Convert class, so that I can simply call it with Convert.ToID("somewhat"); You can't do this. The Convert Class[^] is sealed[^]. That means you can not derive classes from it. And even if it wasn't sealed and you'd derive it you would end up with a new type. You should simply provide a method ID.FromString(string) and/or a corresponding constructor for new instances (public ID(string)). Best regards Dennis

      T 2 Replies Last reply
      0
      • D Dennis C Dietrich

        TyronX wrote: So I thought it would be the best to kind of append a method to the System.Convert class, so that I can simply call it with Convert.ToID("somewhat"); You can't do this. The Convert Class[^] is sealed[^]. That means you can not derive classes from it. And even if it wasn't sealed and you'd derive it you would end up with a new type. You should simply provide a method ID.FromString(string) and/or a corresponding constructor for new instances (public ID(string)). Best regards Dennis

        T Offline
        T Offline
        TyronX
        wrote on last edited by
        #4

        Okay. Thanks to both of you.

        1 Reply Last reply
        0
        • D DavidNohejl

          See MSDN[^]. This should be what you are looking for.. Changes in .NET classes are possible, bt I don't like this idea (in this case). Why not to make static function ID.FromString(string s)? And hey! It is in Convert class... MSDN[^] hmm thats cool! TyronX wrote: P.S.: Sorry for asking so much ^^ nah... how would we earn gold memberships, etc.? :P Plus from some question (like this one) I learnt something new.. isn't iot great? :-D best regards, David 'DNH' Nohejl Never forget: "Stay kul and happy" (I.A.)

          D Offline
          D Offline
          Dennis C Dietrich
          wrote on last edited by
          #5

          dnh wrote: See MSDN[^]. This should be what you are looking for.. I don't think so. He wants to convert a string into his own type (FromString()) not the other way round (which would be IConvertible.ToString()). dnh wrote: Changes in .NET classes are possible, bt I don't like this idea (in this case). How would you do that? dnh wrote: nah... how would we earn gold memberships, etc.? :cool: Best regards Dennis

          D 1 Reply Last reply
          0
          • T TyronX

            I created a own datatype called ID. Now I want to have a function which converts a string into an ID. So I thought it would be the best to kind of append a method to the System.Convert class, so that I can simply call it with Convert.ToID("somewhat"); Is that possible? P.S.: Sorry for asking so much ^^

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

            Your best choice is to use a TypeConvertor or an explicit cast. top secret
            Download xacc-ide 0.0.3 now!
            See some screenshots

            D 1 Reply Last reply
            0
            • D Dennis C Dietrich

              dnh wrote: See MSDN[^]. This should be what you are looking for.. I don't think so. He wants to convert a string into his own type (FromString()) not the other way round (which would be IConvertible.ToString()). dnh wrote: Changes in .NET classes are possible, bt I don't like this idea (in this case). How would you do that? dnh wrote: nah... how would we earn gold memberships, etc.? :cool: Best regards Dennis

              D Offline
              D Offline
              DavidNohejl
              wrote on last edited by
              #7

              Dennis C. Dietrich wrote: I don't think so. He wants to convert a string into his own type :doh: you are right! [edit]what's even worse I actually told him to have stg like ID.FromString() so I understood bt just said bs then :(([/edit] Dennis C. Dietrich wrote: How would you do that? Sorry. I was very inaccurate. I meant deriving from .NET classes (do not apply to Convert). [psst i didn't say that]change rotor & recompile [/psst] thx for correcting me. (how come I haven't included "what I say CAN be bullsh" yet into my sig- I was really thinking about it!) David Never forget: "Stay kul and happy" (I.A.)

              D 1 Reply Last reply
              0
              • L leppie

                Your best choice is to use a TypeConvertor or an explicit cast. top secret
                Download xacc-ide 0.0.3 now!
                See some screenshots

                D Offline
                D Offline
                DavidNohejl
                wrote on last edited by
                #8

                actually it's TypeConverter ( what makes it easier to find :) ) Thx for solution. :cool: best regards, David Never forget: "Stay kul and happy" (I.A.)

                L 1 Reply Last reply
                0
                • D DavidNohejl

                  Dennis C. Dietrich wrote: I don't think so. He wants to convert a string into his own type :doh: you are right! [edit]what's even worse I actually told him to have stg like ID.FromString() so I understood bt just said bs then :(([/edit] Dennis C. Dietrich wrote: How would you do that? Sorry. I was very inaccurate. I meant deriving from .NET classes (do not apply to Convert). [psst i didn't say that]change rotor & recompile [/psst] thx for correcting me. (how come I haven't included "what I say CAN be bullsh" yet into my sig- I was really thinking about it!) David Never forget: "Stay kul and happy" (I.A.)

                  D Offline
                  D Offline
                  Dennis C Dietrich
                  wrote on last edited by
                  #9

                  dnh wrote: how come I haven't included "what I say CAN be bullsh" yet into my sig- I was really thinking about it! Don't bother! Otherwise everybody here would have to include that. Well, maybe everybody except for Heath Stewart. ;) Best regards Dennis

                  D 1 Reply Last reply
                  0
                  • D Dennis C Dietrich

                    dnh wrote: how come I haven't included "what I say CAN be bullsh" yet into my sig- I was really thinking about it! Don't bother! Otherwise everybody here would have to include that. Well, maybe everybody except for Heath Stewart. ;) Best regards Dennis

                    D Offline
                    D Offline
                    DavidNohejl
                    wrote on last edited by
                    #10

                    Dennis C. Dietrich wrote: Well, maybe everybody except for Heath Stewart. LOL. that's true! :-D David Never forget: "Stay kul and happy" (I.A.)

                    1 Reply Last reply
                    0
                    • D DavidNohejl

                      actually it's TypeConverter ( what makes it easier to find :) ) Thx for solution. :cool: best regards, David Never forget: "Stay kul and happy" (I.A.)

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

                      dnh wrote: actually it's TypeConverter Damn intellisense :laugh: top secret
                      Download xacc-ide 0.0.3 now!
                      See some screenshots

                      1 Reply Last reply
                      0
                      • D Dennis C Dietrich

                        TyronX wrote: So I thought it would be the best to kind of append a method to the System.Convert class, so that I can simply call it with Convert.ToID("somewhat"); You can't do this. The Convert Class[^] is sealed[^]. That means you can not derive classes from it. And even if it wasn't sealed and you'd derive it you would end up with a new type. You should simply provide a method ID.FromString(string) and/or a corresponding constructor for new instances (public ID(string)). Best regards Dennis

                        T Offline
                        T Offline
                        TyronX
                        wrote on last edited by
                        #12

                        Next Question: Can I add something to the Point Class/Structure (I need to add the TypeConverter)?

                        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