473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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

Nov 17 '05 #1
11 1699
"Johann Blake" <jo*********@yahoo.com> schrieb im Newsbeitrag
news:11*********************@z14g2000cwz.googlegro ups.com...
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.
Nov 17 '05 #2
> 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....
Nov 17 '05 #3
> 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.
Nov 17 '05 #4
I either need new contact lenses or the poor guy needs to upgrade his
MCSD skills.

Nov 17 '05 #5
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.

Nov 17 '05 #6
"Johann Blake" <jo*********@yahoo.com> wrote in message
news:11********************@z14g2000cwz.googlegrou ps.com...
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.
Nov 17 '05 #7
Johann Blake wrote:
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-
Nov 17 '05 #8
"Johann Blake" <jo*********@yahoo.com> wrote:
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.
Nov 17 '05 #9

Johann Blake wrote:
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;

Nov 17 '05 #10
How would you have written it?

--
Jonathan Allen
"David Parker" <dp@kewly.com> wrote in message
news:%2******************@TK2MSFTNGP15.phx.gbl...
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....

Nov 17 '05 #11
Jonathan,
How would you have written it?

I will not use so many variables with the same constant. Does remind me on
Cobol.

Just my thought,

Cor
Nov 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
112
by: Andy | last post by:
Hi All! We are doing new development for SQL Server 2000 and also moving from SQL 7.0 to SQL Server 2000. What are cons and pros for using IDENTITY property as PK in SQL SERVER 2000? Please,...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
86
by: Michael Kalina | last post by:
Because when I asked for comments on my site-design (Remember? My site, your opinion!) some of you told me never to change anything on font-sizes! What do you guys think of that:...
14
by: mjkahn | last post by:
I've read (and read!) that you shouldn't store objects in Session variables. I've read these reasons: - The object takes up memory that may not be freed until the session times out. Better to...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
1
by: Frank Rizzo | last post by:
Some of the classes in the framework are marked as thread-safe in the documentation. In particular the docs say the following: "Any public static (*Shared* in Visual Basic) members of this type...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
10
by: Franky | last post by:
I think I misread a post and understood that if I do: System.Windows.Forms.Cursor.Current = Cursors.WaitCursor there is no need to reset the cursor to Default. So I made all the reset...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.