Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this code even compile?

Tags:

.net

vb.net

Question:
Why does the below code (not written by me) even compile ?
I mean apart from the fact that option strict is off and option infer is on...

If Not actdate.DayOfWeek = DayOfWeek.Saturday And Not actdate.DayOfWeek.Sunday Then
...
End If
**Edit:**
Just for those not fluent in VB, it's oviously the same as:
if (!(actdate.DayOfWeek == DayOfWeek.Saturday) & !actdate.DayOfWeek.Sunday) {
...
}
which basically answers the question already, since the thing to remember is that the VB-`AND` is actually a bitwise and.
like image 520
Stefan Steiger Avatar asked Sep 16 '13 13:09

Stefan Steiger


People also ask

Why does code compile?

Compile is the creation of an executable program from code written in a compiled programming language. Compiling allows the computer to run and understand the program without needing programming software used to create it.

Does all code need to be compiled?

The short answer is no, all programming languages do not compile into the same machine code. Some programming languages do typically compile into machine code, but each CPU architecture has its own unique machine code.

What does it mean to compile program code?

Compiling the Program Compiling a Java program means taking the programmer-readable text in your program file (also called source code) and converting it to bytecodes, which are platform-independent instructions for the Java VM.

When should you compile your code?

Not necessarily as often as you test-build, but every time you complete a feature or sub-feature you should give it a quick test or two right away, even if it's going to be later tested in other ways or by other people.


1 Answers

The accepted answer is not correct, operator precedence in VB.NET ensures that the logical version of And operator is used, same one as AndAlso. Both the left-hand and right-hand operands are of type Boolean thanks to the Not operators being used. Precedence in VB.NET is relational > Not > And. In C# it is ! > relational > &. Or to put it another way, you don't need parentheses in VB.NET like you do in C#.

The Not operator in Visual Basic accepts a Boolean or numeric expression. Just like in C#, an enum value is implicitly convertable to an integral value type that matches the Enum's base type. Integer in this case. A numeric value of 0 converts to False. Since DayOfWeek.Sunday's underlying value is 0, the Not expression always produces True.

So this is acceptable syntax. You do however get a warning for this code, very similar to the error you get in C#:

warning BC42025: Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.

Produced by the Sunday enum member used in the actdate.DayOfWeek property expression. That is certainly a code smell. Short from not ignoring warnings, you can turn that warning into an error. Project + Properties, Compile tab, Warning configuration section. Change the "Instance variable accesses shared member" setting from Warning to Error.

like image 114
Hans Passant Avatar answered Oct 20 '22 00:10

Hans Passant