Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] c#: overloading operator=

Started by Daniel Miller Jul 27, 2005 at 3:27 PM 6 replies 850+ views
Original Post
Daniel Miller
Daniel Miller
Considering that you can't overload operator=, how are you supposed to allow something like this? MyClass obj = new MyClass(); obj = "hello"; edit: Besides, of course, a normal method. ;)
TheBluMage
TheBluMage
Do you want to implicitly convert a string to your object type? If that's what you want, check out this link.
Daniel Miller
Daniel Miller
Actually, I want to allign an iterator-ish thing with the matching object in a collection.
Nypyren
Nypyren
Now that I think about it, an implicit cast overload won't work... because the MyClass that would get returned would completely overwrite the reference that you assign it to rather than use any other operator.

I don't think you're going to be able to do it using operator =

I think the closest thing you'll be able to come to is a property that accepts a string, and give it some easy to use name.

obj.Iterator = "hello";

public string Iterator{  set  {    // do whatever kind of iterator "find" code you want in order to set the internal iterator member.  }}
Daniel Miller
Daniel Miller
I don't think that would work, because the string could not know about the underlying collection.
Nypyren
Nypyren
Sorry, I edited my post while you were responding ;)
Xai
Xai
iterator.MoveTo("hello")
kSquared
kSquared
Quote:
Original post by Daniel Miller
Actually, I want to allign an iterator-ish thing with the matching object in a collection.

That seems really nonintuitive for operator=. What if you *did* want the iterator to point at a different collection? I agree with the behavior of not overloading operator=. Stick with something like iterator.MoveTo("myObject"), iterator["myObject"], etc.
- k2 "Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ<

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.