Thread: Sigh, Brainfart. Controlling how += operator is used?

  1. #1
    #junkie
    Join Date
    Oct 2004
    Posts
    240

    Sigh, Brainfart. Controlling how += operator is used?

    Ok, i have a custom dialog and a custom property inside it. This property simply writes or returns a richTextBox's .Text property.

    Now i want to modify how += is used on that property. If someone uses
    Code:
    MyDialog.MyText += "Text"
    i want the text to simply be added in my dialog as
    Code:
    this.richTextBox.Text += value +"\n"+ this.richTextBox.Text;

    Now i am so friggin rusty as i havent been coding in a while, i forgot even what doing that is called. Something like Overriding the Modifier but without knowledge of exactly what it is called google does me no good as it does not search none Alhpa-Numeric Characters.


    So any help would be much appreciated! And note that this += operator modification should only apply to that specific property.. so, any help?
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    In your case, it might work if you simply write a property MyText that "get"s the text from the edit box and "set"s the text of the edit box. The default operator should do the rest.

    If you were looking for operator overloading, you can only overload single operators like "+". "+=" is automatically adjusted when you change "+".
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    is it possible to modify an operator for a single property? as apposed to a whole class?
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    No, it's not possible. You can create a class and use it as type for the property, that would mimic the behaviour you are asking for. However, I think you want something much simpler. What does your property look like and what error messages do you get if any ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    I'm not getting any errors, and i changed how i did it anyway. Couldent wait around that long, but i'm still interested ofcourse lol.

    But i was just trying to save time by modifying how my own .Text property handles +=. Instead of "string + string", it would be "string + \n + originalstring" Nothin complicated, but that would have saved me time.
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    But i was just trying to save time by modifying how my own .Text property handles +=.
    It might handle it the same way it does for the underlying datatype. I don't have a compiler at hand, so I can not tell what exactly the behaviour of the default += operator was. Did it differ from what you needed ? The System::String += operator would do exactly what you describe as far as I remember.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    No, this is what it does.

    "newstring + oldstring"

    this is what i want it to do

    "newstring + \n + oldstring"


    notice the "\n" character added. But by doing that, i may as well be adding a whole book between them, it still ends up as me modifying how it works :/
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  8. #8
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    myInstance.MyText += "\n" + Variable;

    This is what you seem to need. Does this throw errors or do you get unexpected results ? If so, post some code how your implemented your MyText property please.

    If you want to automatically add a newline whenever something is added, you need to write a function like "void AddLine( string Variable )" that calls the first line of this post. There is no operator += in C#, += is just a short version of a = a + b, where the only thing you can modify is how operator + works for your own classes ( and I guess the the property is of type string ).
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  9. #9
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    Well first off,

    myInstance.MyText += "\n" + Variable;

    is

    Code:
    Oldstring +"\n"+ Newstring
    and not,
    Code:
    Newstring +"\n"+ Oldstring
    ( by doing this the newest string is always on top. )


    But yes, my code and that code both "work" ( except yours is not what i desire ), the problem simply lies in my wanting to shorten it to its full potential. Thanks though
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

Popular pages Recent additions subscribe to a feed