Extending Classes?
-
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 ^^
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 -
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 -
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.)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 beIConvertible.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 -
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 ^^
Your best choice is to use a TypeConvertor or an explicit cast. top secret
Download xacc-ide 0.0.3 now!
See some screenshots -
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 beIConvertible.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 DennisDennis 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.)
-
Your best choice is to use a TypeConvertor or an explicit cast. top secret
Download xacc-ide 0.0.3 now!
See some screenshotsactually it's TypeConverter ( what makes it easier to find :) ) Thx for solution. :cool: best regards, David Never forget: "Stay kul and happy" (I.A.)
-
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.)
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
-
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
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.)
-
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.)
dnh wrote: actually it's TypeConverter Damn intellisense :laugh: top secret
Download xacc-ide 0.0.3 now!
See some screenshots -
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