implicit conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • docschnipp

    implicit conversion

    Hi,

    I have an object that manages a kind of Variant type. For comfort reasons I
    want to have the possibility to do this:

    MyPropclass prop = new MyPropclass();
    string m = "blabla";

    prop = m;

    How can I do this? Is there a way to overwrite "="? Or must I extend "string"?

    Thanks
    doc

  • ssamuel

    #2
    Re: implicit conversion

    doc,

    Technically, you'd have to extend String. I'm not sure if base types
    are declared partial in 2.0, but even if they are, that's probably a
    bad idea.

    You'd do much better to have a MyPropclass(str ing) constructor, or
    maybe a static MyPropclass MyPropclass.Fac tory(string) method. If
    MyPropclass encapsulates substantially more than a string, void
    MyPropclass.Ass imilate(string) may be appropriate. Alternately, if
    you're into "clean" language constructs, you can write static
    MyPropclass operator+(strin g, MyPropclass) and end up with:

    MyPropclass prop = new MyPropclass();
    [ ... do stuff with prop ... ]
    string m = "mystring";
    prop += m;


    Does that help?


    Stephan


    docschnipp wrote:
    Hi,
    >
    I have an object that manages a kind of Variant type. For comfort reasons I
    want to have the possibility to do this:
    >
    MyPropclass prop = new MyPropclass();
    string m = "blabla";
    >
    prop = m;
    >
    How can I do this? Is there a way to overwrite "="? Or must I extend "string"?
    >
    Thanks
    doc

    Comment

    • Martin Z

      #3
      Re: implicit conversion

      Depends. Will the user be making changes to the string object before
      it is put into the propclass? Or can the "new MyPropclass();" be
      dropped? That is

      MyPropclass prop = "blabla"; //constructs a MyPropclass object out of
      the string

      use implicit user-defined conversion operators:

      http://www.c-sharpcorner.com/Languag...nCSharpRVS.asp -
      scroll down to "user-defined".

      I'm not positive it'll work, haven't tried it myself, but I *think*
      that's what they're for.

      docschnipp wrote:
      Hi,
      >
      I have an object that manages a kind of Variant type. For comfort reasons I
      want to have the possibility to do this:
      >
      MyPropclass prop = new MyPropclass();
      string m = "blabla";
      >
      prop = m;
      >
      How can I do this? Is there a way to overwrite "="? Or must I extend "string"?
      >
      Thanks
      doc

      Comment

      • docschnipp

        #4
        Re: implicit conversion

        Hi Stephan,

        "ssamuel" wrote:
        doc,
        >
        Technically, you'd have to extend String. I'm not sure if base types
        are declared partial in 2.0, but even if they are, that's probably a
        bad idea.
        Agree, sounds bad or at least not very elegant.
        You'd do much better to have a MyPropclass(str ing) constructor, or
        maybe a static MyPropclass MyPropclass.Fac tory(string) method. If
        MyPropclass encapsulates substantially more than a string, void
        MyPropclass.Ass imilate(string) may be appropriate. Alternately, if
        you're into "clean" language constructs, you can write static
        MyPropclass operator+(strin g, MyPropclass) and end up with:
        >
        MyPropclass prop = new MyPropclass();
        [ ... do stuff with prop ... ]
        string m = "mystring";
        prop += m;
        I am sticking with a bunch of overloaded ".Set(#sometype # newValue)"
        functions. When I am deeper in C# I will try to come up with something nicer,
        but for now it is okay.

        Thanks for your help,

        doc

        Comment

        • Göran Andersson

          #5
          Re: implicit conversion

          docschnipp wrote:
          Hi,
          >
          I have an object that manages a kind of Variant type. For comfort reasons I
          want to have the possibility to do this:
          >
          MyPropclass prop = new MyPropclass();
          string m = "blabla";
          >
          prop = m;
          >
          How can I do this? Is there a way to overwrite "="? Or must I extend "string"?
          >
          Thanks
          doc
          >
          Implicit conversions works just fine. Example:

          public class Demo {

          private string _value;
          public Demo(string value) {
          _value = value;
          }
          public static implicit operator Demo(string value) {
          return new Demo(value);
          }

          }

          Usage:

          Demo d = "asdf";

          Comment

          Working...