Can assignments be used in expressions?
I came across this code and wanted others to provide their point of view ... is this good or bad?;)
Class ReportClass
{
public string ReportName {get; set;}
}
Then in code it was used like this:
displayReport(ReportClass.ReportName = cmbReportName.SelectedValue.ToString())
This is about the simplest form example I can give you. Quetion ... why can't I find examples? What would it be called? Is this just a question about problems?
EDIT: I mean the inplace assignment. Which I did not know until today
I try to avoid on-site prescriptions - or no side effects like this at all - except for one common idiom:
string line;
while ((line = reader.ReadLine()) != null)
{
// Do something with line
}
(And options for reading streams, etc.)
I am also fine with using object initializers in parameter calls, eg.
Foo(new Bar { X=1, Y=2 });
but assigning a property to an existing object ... well, it's all subjective, but this is not my cup of tea.
source to share
I think it is harder to maintain / harder to debug / harder to code. I would complete the assignment on the line prior to calling this method.
ReportClass.ReportName = cmbReportName.SelectedValue.ToString(); displayReport(ReportClass.ReportName);
I've never been a fan of complicated statements.
source to share
This is completely normal. Both.
- Automatic Properties (Object
{get; set;}
): Introduced in C #. A very useful feature. -
Assignment in parameters: rarely used in C #, but completely legal. Basically, it assigns the value of the expression to the right of the assignment operator (=) to the property per chunk to the left, and then passes that value to the method.
This is more common in C, but I see no reason why it shouldn't be allowed in C #. Sometimes it can help with readability, sometimes it gets much worse. Use common sense to decide what applies when.
source to share
Did you mean automatic properties or in-place assignment?
Depends on your IMO team. If your team consisted of C-style developers. Then I think it's okay.
But if this code is going to be further supported by, say, VB developers, then you will need to make it more readable.
Examples? The assignment operator (=) in C langauges also returns the values โโit assigned.
var a = 0;
var b = 0;
// This makes a *and* b equals to 1
a = b = 1;
// This line prints 3 and a is now equals to 3
Console.WriteLine(a = 3);
// This line prints 7 and a and b is now equals to 7
Console.WriteLine(a = b = 7);
I think this is quite natural for this kind of assignment. As C languages โโseem to encourage shorthand IMO.
If you want more readability and fewer problems, I'd say the newline is a nice addition.
displayReport( ReportClass.ReportName = cmbReportName.SelectedValue.ToString());
Make your intentions clearer when each compound statement is on different lines. (but still a compound operator)
Or first extract the right-hand side into your own variable like
var reportName = cmbReportName.SelectedValue.ToString();
displayReport(ReportClass.ReportName = reportName);
I use it all the time and I haven't seen anyone get confused reading it yet.
source to share
The Microsoft Framework design guidelines discourage putting multiple statements on the same line unless there is direct language support, such as in a statement for
. Since there is explicit language support for multiple assignments,
int a, b, c; a = b = c = 0;
allowed. On the other hand, the form code used in your example is not recommended.
How about this, people?
while ((packetPos = Packet.FindStart(buffer, nextUnconsideredPos)) > -1)
To avoid inline assignment, you would need to redundantly relate, for example:
packetPosition = Packet.FindStart(buffer, nextUnconsideredPosition);
while (packetPosition > -1)
{
...
packetPosition = Packet.FindStart(buffer, nextUnconsideredPosition);
}
source to share
Personally, I find assignment a not very readable parameter. The overall flow of execution is simply garbled by the fact that an operation is actually being performed on the parameter list.
I like to think that my code should express intent and not preserve spaces. And my intention in this example will be exactly what EvilSyn suggests, first assign a value and then pass it to the method.
source to share
It seems to me good. It is probably compiled with C # 3.0 and allows automatic C # properties .
source to share