I've decided to continue using my Add() method. However your point about the "abc" string; since I would be converting the string to an int in my overloaded method, I'd get an error in case "abc" were used; so to avoid this I'd check that the user only enters numeric data. that out of the way, I'd still like to know how to overload + for string or int. (just curios). thanks....hasan
hzs
Posts
-
Operator overloading with strings -
Operator overloading with stringsHmm...I believe you're right about the maintinence...overloading would cause a lot of confusion so I guess I'll just use my Add & Sub functions. However you've mentioned that "although the language might allow you to do what you want, do you really want to do it?" and I'm curious how does the languge allow me to do what I want i.e how does it allow me to overload +, - operators etc. for primitive datatypes like int and sting. Hasan P.S. The part about going home at 5 went straight to my heart :-D
-
Operator overloading with stringsYour solution Ista wrote: public static string operator + ( string s1, string s2 ) { return (Int32.Parse(s1) + Int32.Parse(s2)).ToString(); } doesn't work...I've had already tried it. The compiler generates an error: One of the parameters of a binary operator must be a containing type". So the + operator can't be overloaded in this way.
-
Operator overloading with stringsright. But I don't want to have to convert the stings to ints every time as i'm using around 15 for each calculation and doing what you suggested would make the code sort of confusing. I think it can be done using operator overloading so that the developer can add two strings simply by writing
string3=string1+string2
Anyone with some other solution? -
Operator overloading with stringsI need to apply arithmatic operations on string i.e. string1 = "12"; string2 = "10"; string3 = string1+ string2; and now I want string3 to contain "22" currently I'm using functions like Add(string1, string2) which is a pain. The problem is that the above statement (string3 = string1+ string2) will concatenate the two strings not add them and overloading the + operator doesn't work. The compiler gives an error saying One of the parameters of a binary operator must be a containing type. If I make my own String class, the overloading works fine. Anyone got any ideas? thanks in advance...hasan