Reputation: 13313
Does the compiler see the same thing when it sees THEN...END IF
(in Vb) and { ... }
(in C#) which are what seems to be delimiting If
statements.
If they are treated the same way, why can I do this in C# :
if(true)
int x = 1;
//Instead of
if(true)
{
int x = 1;
}
But not in VB :
//Won't Compile
if(true)
x As Integer = 1
//Instead Of
if(true) then
x As Integer = 1
End If
Upvotes: 0
Views: 717
Reputation: 2416
The syntax in C# is
if (conditional)
statement
else
statement
A statement can be a single line of code ending with a semicolon, OR a set of brackets with multiple statements inside them.
Note that this is also perfectly valid:
if (conditional)
{
{
{
CallMyFunction();
CallMyOtherFunction();
}
}
}
As far as the if is concerned, there is one statement below it, the outermost curly braces. The additional braces have no bearing on the nature of the if statement, there's just two statements inside a statement, inside a statement, inside a statement...
Upvotes: 2
Reputation: 27496
They aren't equivalent because the "end of statement" delimiter in VB is the end of a line. The equivalent in VB would be:
Dim x As Integer
If True Then x = 1
As an aside, to make your C# example more correct, it should actually be:
int x = 0;
if (true)
x = 1;
Note that I'm only answering your question about equivalent syntax eps here, not judging the coding style, which is that you absolutely shouldn't do this, for reasons mentioned in other answers.
Upvotes: 2
Reputation: 81197
C# and vb.net have different rules when it comes to parsing statements. VB.net is derived from QuickBasic, which was derived from Microsoft BASIC interpreters in which an IF/THEN statement with a false condition would cause the interpreter to skip ahead until the end of the line or an ELSE statement, whichever occurred first. Because it was somewhat annoying having to place all the statements conditioned on an IF statement on the same line as that statement, QuickBasic was designed so that if a line contains an IF/THEN, ELSEIF/THEN, or an ELSE but no other statements, execution will skip until the next corresponding ELSEIF, ELSE, or ENDIF.
While the VB syntax is somewhat ad hoc, it actually works out quite nicely. One thing that might be nice in both languages would be a statement whose clearly-defined purpose was to begin a variable-scoping block. In vb.net, one can say:
If True Then ' Scoping block
Dim SB As New System.Text.StringBuilder()
...
End If
but that seems a bit icky. The ability to use braces alone for that purpose in C# might be seen as a "win" for that language, but absent a comment it's wouldn't be clear when reading code that the purpose of the braces was to serve as a variable-scoping block, rather than grouping some statements that were supposed to be executed conditionally or repeatedly, but for whatever reason "lost" their control statement.
Upvotes: 5
Reputation: 174349
C# and VB.NET are two different languages with different syntax rules. There is no one to one mapping of a rule in one language to a similar rule in the other language.
THEN and END IF are semantically the same as the curly braces of an if statement (i.e. they mean the same), but the VB.NET syntax doesn't allow them to be ommitted.
BTW: Your sample is rather bad, because in that special case even in C# you can't ommit the curly braces.
Upvotes: 7
Reputation: 152596
That's just the way the languages are designed. There are many out there that loathe the freedom to exclude curly brackets on single-line if
statements because it opens up the possibility of difficult-to-find bugs for novices:
if(p.Birthday == DateTime.Today)
SendBirthdayCard();
if you (seemingly innoccuously) modify it to:
if(p.Birthday == DateTime.Today)
Console.WriteLine("Happy Birthday!!");
SendBirthdayCard();
Suddenly everyone gets a birthday card!!
Upvotes: 0