John500
asked on
How does C# allow for blank assignments? MyVariable = <-------- compiles
Greetings:
I'm reviewing code written by another developer and I've noticed blocks of code that will have no value/object to the right of the variable that needs assignment, like:
if (TSMIAllPasswords.Checked || _searching)
{
bool localro = _password.IsReadOnly;
TBDomain.ReadOnly =
TBUsername.ReadOnly =
TBNotes.ReadOnly =
TBPassword.ReadOnly = localro;
DTPExpire.Enabled = !localro && _password.Expire;
BGenerate.Enabled =
CBExpire.Enabled = !localro;
}
else
{
TBDomain.ReadOnly =
TBUsername.ReadOnly =
TBNotes.ReadOnly =
TBPassword.ReadOnly = _sectionRO;
DTPExpire.Enabled =
BGenerate.Enabled =
CBBExpire.Enabled = _sectionRO;
}
}
How is it that this compiles?? During execution I certainly get an error that
I'm reviewing code written by another developer and I've noticed blocks of code that will have no value/object to the right of the variable that needs assignment, like:
if (TSMIAllPasswords.Checked || _searching)
{
bool localro = _password.IsReadOnly;
TBDomain.ReadOnly =
TBUsername.ReadOnly =
TBNotes.ReadOnly =
TBPassword.ReadOnly = localro;
DTPExpire.Enabled = !localro && _password.Expire;
BGenerate.Enabled =
CBExpire.Enabled = !localro;
}
else
{
TBDomain.ReadOnly =
TBUsername.ReadOnly =
TBNotes.ReadOnly =
TBPassword.ReadOnly = _sectionRO;
DTPExpire.Enabled =
BGenerate.Enabled =
CBBExpire.Enabled = _sectionRO;
}
}
How is it that this compiles?? During execution I certainly get an error that
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
they are not blank assignments...they are saying a = b = c = "ssfds;
so in essence they all have the same value until you reach the ; (semicolon)...
equates to:
a = b = c = d = "hello";
shorthand for
a="hello";
b="hello";
.
.
.
d= "hello";
a = b = c = d = "hello";
shorthand for
a="hello";
b="hello";
.
.
.
d= "hello";
Just to make it clear, in C#, a line will AUTOMATICALLY be continued until a line termination character (;) is encountered. Thus
TBDomain.ReadOnly =
TBUsername.ReadOnly =
TBNotes.ReadOnly =
TBPassword.ReadOnly = localro;
is EXACTLY the same as:
TBDomain.ReadOnly = TBUsername.ReadOnly = TBNotes.ReadOnly = TBPassword.ReadOnly = localro;
the white space is ignored, by the C# compiler.
AW
TBDomain.ReadOnly =
TBUsername.ReadOnly =
TBNotes.ReadOnly =
TBPassword.ReadOnly = localro;
is EXACTLY the same as:
TBDomain.ReadOnly = TBUsername.ReadOnly = TBNotes.ReadOnly = TBPassword.ReadOnly = localro;
the white space is ignored, by the C# compiler.
AW
They are continuations, not blank.
Oh wait, that is what mastoo already said 20 minutes ago.
Oh wait, that is what mastoo already said 20 minutes ago.