Operator Overloading Strings?

已答复 Operator Overloading Strings?

  • vendredi 11 mai 2012 21:59
     
      A du code

    I want custom math done on the hexadecimal addresses stored in string formats with just using operator overloading. I can do it without but it requires more lines in a sense. Here's the code I have to overload it and it's not working. It still tells me I can't apply the subtraction operator to types of string and string.

            public static string operator -(string Address1, string Address2) {
                return Math.ToHex(Math.ToDecimal(Address1) - Math.ToDecimal(Address2));
            }
    
            public static string operator +(string Address1, string Address2) {
                return Math.ToHex(Math.ToDecimal(Address1) + Math.ToDecimal(Address2));
            }
    
            public string GetOffsetValue(MemoryAddress FunctionTop) {
                return (this.Address - FunctionTop.Address).ToString(); // Error
            }

    How would I go about doing this without having to create the new methods I've already thought up for it. Which are:

    public string Subtract(string Address1, string Address2) {
    return ToHex(ToDecimal(Address1) - ToDecimal(Address2));
    }
    
    public string Add(string Address1, string Address2) {
    return ToHex(ToDecimal(Address1) + ToDecimal(Address2));
    }

    Thanks,

    Jamie


    Programmer (noun) ["proh - grahm - er"]: A human being that can turn caffeine into code.

Toutes les réponses

  • vendredi 11 mai 2012 23:42
     
      A du code

    to convert hex string to integer use

    Convert.ToInt32(hex, 16);

    Source

    How to: Convert Between Hexadecimal Strings and Numeric Types (C# Programming Guide)

    http://msdn.microsoft.com/en-us/library/bb311038.aspx


    Regards,
    Ahmed Ibrahim
    SQL Server Setup Team
    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you.
    This can be beneficial to other community members reading the thread.

  • samedi 12 mai 2012 01:40
     
     
    I already know how to do that, that's what the ToHex method that is being called is. It takes a 32 bit integer argument, passes it to Convert.ToInt32(string, int) to convert it using the 16 value. ToDecimal uses .ToString("x"). What' I'm asking is how to overload the string's operators.

    Programmer (noun) ["proh - grahm - er"]: A human being that can turn caffeine into code.

  • samedi 12 mai 2012 05:41
    Modérateur
     
     Traitée

    "What' I'm asking is how to overload the string's operators."

    You can't and you shouldn't. A string is a string, it has its own operators including + which does concatenation. It's quite weird to do numeric computations on numbers represented as strings but if you insist in doing that then you have to define your own type.

  • samedi 12 mai 2012 08:08
     
     Traitée

    If you want to overload operators you must do it in the class that is being overloaded. So you must do it in the String class. Unfortunately you don't have the source code so you can't do it.

    The best you can do is implement your own class that uses a string field (since you can't inherit from string) and provide the operators in that. Or use your Add and Subtract methods.


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.