7

I have a complex object type for which I'm overriding the "base.ToString()" method so that it returns a string property value.

for example:

class foo
{
 public string StringValue{get;set;}
 public int SomeOtherValue{ get;set;}
 public override ToString()
 {
   return StringValue;
 }
}

So this allows me to use retrieve the value of the StringValue property easily.

Is there a way in which I can override or extend the base functionality so that I can use simple code such as below to set the StringValue property?

foo aFoo = new foo();
aFoo = "Some string value";
ChrisBD
  • 9,104
  • 3
  • 22
  • 35
  • 1
    Is `aFoo.StringValue = "Some string value";` really *that* onerous to type? ;-) – Adam Houldsworth Sep 13 '13 at 09:04
  • Now that I read the question properly I have to agree with Adam. C# offers no support here on purpose, so that reading the code makes it clear what exactly is happening. Did we learn nothing from C++? – Jon Sep 13 '13 at 09:06
  • 1
    Btw, what would be you reason? Lazyness? (don't get me wrong, I respect that) Or the intention to confuse your fellow developers? :-D Seriously, I really hate all kinds of operator overloading. If nothing else, "Go to definition" doesn't work and you have no indication of what's going on by looking at the code. – vgru Sep 13 '13 at 09:06
  • I didn't think that there was a way, but thought that I'd ask anyway. It was really laziness on my part as I have a large amount of legacy code that will need to be changed from a straight assignment to what is required now. – ChrisBD Sep 13 '13 at 09:15

4 Answers4

11

You can use implicit cast operator overload: http://msdn.microsoft.com/library/z5z9kes2.aspx

rpeshkov
  • 4,877
  • 3
  • 27
  • 43
  • 4
    This would cause the creation of an entirely new `foo`, not simply set the `StringValue` property of an existing `foo`. That said, it might still be a good enough solution depending on the usage. – Adam Houldsworth Sep 13 '13 at 09:01
7

You can't overload the = operator as stated here

See this page for a complete list of overridable operators.

Ray Hayes
  • 14,896
  • 8
  • 53
  • 78
Marshall777
  • 1,196
  • 5
  • 15
3

No, this is not possible. The assignment operator is not overridable and for good reason.

Developers are, even within the confines of C# and modern object-oriented programming languages, often "abusing" the system to make it do things it's not supposed to do. If you could assign a different meaning than passing a reference value to a variable to an assignment operator, think of the chaos that would create when inexperienced developers use it.

What you can do is to provide methods that allow your object to take its values from a string, like so:

afoo.TakeValuesFrom("Some string value");

or

MyThingy afoo = MyThingy.FromString("Some string value");

which would be almost identical to what you are asking, and perfectly legal and readable.

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • 1
    Although everyone else's answers are valid I'm selecting yours for suggesting the addition of a FromString method, which is what I'll go with. – ChrisBD Sep 13 '13 at 09:17
-2

No you cannot. It is a protected part of the C# language specification. Being able to override this would be like overriding your bodies ability to breathe air with breathing water.

BenM
  • 4,218
  • 2
  • 31
  • 58