views:

112

answers:

5

I have a class named Car with property Name. Now every time I like to change Name of my Car I must write Car1.Name = "Porka Turbo". Now what I would like to acomplish is 5 characters less to write like this:

Car1 = "Porka Turbo"

It should work like this: if I assign a class derrived from Car class it should make regular class assignment, but when I assign string to my class than this string should be redirected to property called "Name".

So that it will work the same as:

Car1.Name = "Porka Turbo"

Is this possible in C# .NET 3.5 and if not do you know any other method to write less in this particular example?

A: 

It is not possible since the assignment operator cannot be overloaded.

Even it it would be possible, I shouldn't do it. What do you gain with it ?
A few less characters that you have to type ? Keep the fact in the back of your mind that code is being read far more often than it is written.
When you re-read / review that code after a year or 2, I think you'll be scratching the back of your head a few times ...

Besides, suppose that it would be possible to overload the assignment operator:

What will you do if you want to assign another Car instance to Car1 ?

What will you do if Car1 is NULL ?

Frederik Gheysels
@Frederik that can be taken care in operator overloading method (using type of the object being assigned), but why does he needs this, not a good programming practice. What is the motive?
Ratnesh Maurya
1. If you pass Car instance than it will use regular object assignment like usuall.2. If Car1 will be NULL than nothing happens
tomaszs
1: ok2: not ok. This will be unexpected (to me , that is).
Frederik Gheysels
And if it would throw exception?
tomaszs
A: 

No, C# does not allow you to overload the assignment operator.

Andrew Hare
So is there any other way to write less?
tomaszs
Use single character names for everything - properties, variables, classes etc. The code will be horrible, but it will be shorter.
Jon Skeet
@Jon Skeet - I don't need everything shorter, but only Name property. And thats it. Name is essential part of a object definition, don't you think?
tomaszs
@tomaszs: Name is apparently and essential part of *your* object but not every object. What you would like to do is impossible; you cannot overload the assignment operator to assign a value to a property. Others (like Jon) have suggested solutions that minimize typing (as that seems to be your main goal) - but the fact remains: ***you cannot overload the assignment operator***.
Andrew Hare
+2  A: 

I don't know if there is a way of accomplishing what you are looking for (perhaps there is), but I would advice against doing so, since I would find it confusing. For starters, it would make the Car1 instance in your example to act as if it was a string (which it is not), and it will be less clear what the code actually does.

Fredrik Mörk
Yes, I know it's not clear, but i would like to acomplish this solution.
tomaszs
+3  A: 

Assuming Car1 is a variable of type Car, you can't do it. Nor should you - it's a readability nightmare.

Assignment to a variable should mean a change in the value of the variable to the (possibly converted) value on the RHS of the assignment operator. You're trying to change the meaning of the operator.

If you're bothered by the number of characters in the far more readable:

Car1.Name = "Porka Turbo";

you could create a single-letter method:

Car1.X("Porka Turbo");

Or another property:

Car1.N = "Porka Turbo";

I wouldn't do either of those though. Keep the code simple and readable. Whoever has to maintain the code in the future (which may be you) will not thank you if you prioritise "minimal number of characters in source" over readability.

EDIT: One option which hasn't been presented yet is providing an implicit conversion from string to Car. That would let you write

Car1 = "Porka Turbo";

but it wouldn't be changing the name property of the existing car - it would be creating a new car and assigning that value to Car1. I mention this only in case someone else drops that in as an option without mentioning the problem with it :)

Jon Skeet
I won't downvote, because what you're saying is perfectly right, and you do give the proper suggestions as to why patterns like that should really not be implemented, but somehow i would still have preferred if polishwords didn't find out about this alternative. after all, whoever has to maintain the code in the future could be *me* :)
David Hedlund
I didn't ask if it's the right thing to do, but how to do it.
tomaszs
In my view it would be remiss of us not to explain to you that what you're trying to do is not only impossible but a *very bad idea*.
Jon Skeet
Ok, its good, so now when you explained disandventages of this solution to beginners I wait for an answer for my question.
tomaszs
@polishwords.test..pl: Jon has already answered your question. This **cannot** be done in C#. If you really want to avoid typing then use a single-letter property name (but future maintainers of the code won't thank you for it).
LukeH
@Luke - Ok, so I need to find solution somewhere else.
tomaszs
A: 

I think a very deep misunderstanding of OOP is the root of this request, in the first place. The good thing with an object oriented language like C# is that an object of the type Car will always be a Car, and nothing else. A string will always be a string, and a String will never be a Car. Wrapping a piece of text within double quotation marks happens to be shorthand for creating a new string instance. There is no such shorthand for creating a new Car instance. You can never say that "This string is actually a Car", for reasons like, say, the fact that a String doesn't have the "Name" property.

The best thing I can say is, keep working with C#, and soon enough you'll come to appreciate that this is the case =)

David Hedlund
I can tell many things that make this solution not good from many points of view, not only OOP, but I would like to acomplish this solution and that's all. Hope you will learn C# more and give me the answer.
tomaszs
It's not a case of "learning C# more". Fortunately C# doesn't let you shoot yourself in the foot like this. If you want an overloaded assignment operator, use C++.
Jon Skeet
@Jon - I know that it's available in C++ and in some extremal cases it's usefull and I think the same should be available in C#. So give me that gun, so I can shoot this bug off my foot.
tomaszs