What exactly does a = b = c = d mean??

  • Thread starter Thread starter Johann Blake
  • Start date Start date
J

Johann Blake

Can someone explain how the following code evaluates:

string a = "a";
string b = "b";
string c = "c";
string d = "d";

a = b = c = d;

Believe it or not, a similar piece of code was published on msdn. I
can't imagine why anyone would ever want to program like this. It looks
horrendous.

Best Regards
Johann Blake
 
Johann Blake said:
Can someone explain how the following code evaluates:

string a = "a";
string b = "b";
string c = "c";
string d = "d";

a = b = c = d;

It means: take the value of d and assign it to a, b and c.
equivalent to:
a=d; b=d; c=d;
or more precise:
c=d; b=c; a=b;

similar to x++ assignemnts not only change the value of a variable, but
also return a value, (the assigned value), burt in most cases it's not used.
 
string a = "a";
string b = "b";
string c = "c";
string d = "d";

a = b = c = d;

Believe it or not, a similar piece of code was published on msdn. I
can't imagine why anyone would ever want to program like this. It looks
horrendous.


I don't think it's *that* bad, it just sets c to d, then b to c and then a
to b, so they all end up d.

Now here's some VB junk someone else did that I'm having to maintain right
now (this is the text change event for a textbox containing the number of
labels to print - one thing I'm having to do right now is change it so some
labels print from one printer and some get printed several weeks later on
another printer)....


Private Sub txtnol_Change()
On Error GoTo errhandler
Dim X, Z
Dim StrTmp, StrTMP2 As String
Z = 0
If Abs(txtnol) <> 1 Then
If Left(cbclsl, 1) = "C" Then
StrTmp = UCase(Right(Left(cbclsl, 11), 2))
Z = 1
ElseIf Left(cbclsl, 1) = "L" Then
StrTmp = UCase(Right(Left(cbclsl, 13), 2))
Z = 1
End If
End If
If Z = 1 Then
For X = 12 To 17
StrTMP2 = UCase(Right(Left(cbclsl.List(X), 9), 2))
If StrTmp = StrTMP2 Then
cbclsl = cbclsl.List(X)
Exit For
End If
Next
End If
If Abs(txtnol) > 50 Then txtnol = 50
Exit Sub
errhandler:
MessageBox "ACCESS DENIED!!!!"
End
End Sub


What's worse is the guy who wrote that claims to have an MCSD....
 
MessageBox "ACCESS DENIED!!!!"

By the way, that MessageBox function turned out to be a function that
checked a TMP file on a network share to see if it contained a certain
number (looks like a phone number). If it does it displays the message,
otherwise it doesn't. Course there's no comments in to explain why or
anything, my guess is that the guy's took lessons in how to write
unmaintainable code.
 
What I found unintuitive about the syntax was the order in which it was
evaluated. Was it from right to left or left to right? I had to
actually run the code to figure it out.
 
Johann Blake said:
What I found unintuitive about the syntax was the order in which it was
evaluated. Was it from right to left or left to right? I had to
actually run the code to figure it out.

How could it possibly be left to right?

Consider a=b or a=b+c. The expression to the right of the = is evaluated
and
then assigned to the variable on the left.
 
Johann said:
Can someone explain how the following code evaluates:

string a = "a";
string b = "b";
string c = "c";
string d = "d";

a = b = c = d;

Believe it or not, a similar piece of code was published on msdn. I
can't imagine why anyone would ever want to program like this. It looks
horrendous.

It's just successive assignment (from right to left - they all end up being
"d"). It's an old syntactical style which predates "structured programming" and
even the C language AFAIK. I first learned it in a Fortran class taught by a
math professor in the 70's and it was surely not new then. It was better than
using 4 separate assignment statements for one reason only that I know of - but
you likely won't think of that reason if you have never entered a program by
punching 80-column cards, one statment per card . . .

Personally I would prefer a = b = c = d = "d"

Regards,
-rick-
 
Johann Blake said:
I either need new contact lenses or the poor guy
needs to upgrade his MCSD skills.

It's quite a common and acceptable idiom for initialisation, e.g.

lives = 3;
score = highScore = 0;

P.
 
Johann said:
Can someone explain how the following code evaluates:

string a = "a";
string b = "b";
string c = "c";
string d = "d";

a = b = c = d;

Believe it or not, a similar piece of code was published on msdn. I
can't imagine why anyone would ever want to program like this. It looks
horrendous.

Best Regards
Johann Blake

Such code is useful for scenarios were you are assigning many values to
the same value.

i.e.

x = y = z = yaw = pitch = roll = 0.0f;
 
Back
Top