Reputation: 141
Ok, so I'm working for a company and before I was hired the company had a guy writing a program in Visual Basic. However, he got mad and ran off so I cannot communicate with him. My job is to convert his program to C#. He has no formal programming training so there are no comments in his code at all. In his code he has the following line (Written in C#):
if (boolVariable == 13)
I'm assuming I should just convert the int to bool or vice versa, but I figured I would ask you guys first. To give you more information, the boolVariable is only referenced in two other places. The declaration where it is being set to false, and in another if statement checking if it is true. No where else in the code is it being defined to something else, so I'm think that this just might be useless code. However, this piece of code does not cause an error in Visual Basics which also has me a little confused. Does visual basics automatically convert it for you? Any help will be greatly appreciated.
Upvotes: 0
Views: 1599
Reputation: 37020
Yes, Visual Basic will convert it for you. Zero will be converted to False
, and all other integers will be converted to True
.
The simplest way to 'convert' the code you posted is to remove the '== 13'. This should accomplish the same thing.
if (boolVariable)
{
// Do stuff
}
But really the best thing to do would be to understand what logic is taking place:
boolVariable
is true
?boolVariable
? boolVariable
really represent (...and then change the name!)If you can understand these things, then you can give the variable a more meaningful name (and possibly a more meaningful type), and the code will be easier to maintain in the future.
I don't envy you - it's a tough job converting bad, uncommented code. My advice is: don't just do a direct convertion. If you do that, then you just have bad, uncommented code in a different language. And a year from now we will see another post like yours about the code that you converted! :)
I would make sure your boss understands and signs off on the effort needed to do a decent conversion first, then spend the extra time to get it right.
Upvotes: 3
Reputation: 1140
VB behaviour :
You said it was written in VB, so, I assume that the original code looks like this :
If boolVariable = 13 Then // Remeber : it was VB..
// Code block if True
// ...
Else
// Code block if False
// ...
End If
Now let's take a look at how the code will behave...
// Remeber : it was VB..
If boolVariable = 13 Then
/* The boolVariable is a Boolean
The "13" is an Integer
First step is performing a Widening conversion Boolean -> Integer
boolVariable is converted to 0 if False, or 1 if True.
So, depending on the value of boolVariable, the statement is
either : */
If 0 = 13 Then // which is False
// either :
If 1 = 13 Then // which is also False
// That means that writing If boolVarialbe = 13 is the same as writing :
If False Then
// Block of code THAT WILL NEVER BE EXECUTED
Else
// Block of code that will ALWAYS be executed
End If
So IMHO, it doesn't make sense writing If (boolVariable == 13)
(wether it's VB or C#)
I'm doing such weird things when I'm testing something specific that has nothing to do in the final code. Maybe the former programmer made something similar and failed to delete that part of the code. Without knowing what the code does and what its impact on an overall scope, I can't say if it's a critical part of the application that should be fixed, or garbage.
Anyway, such statements like If (boolVar == numVar)
are a no-go in today ways of programming (IMHO) The former programmer failed to activate Option Strict
which would have prevented him from writing it.
Upvotes: 0
Reputation: 1647
I am not pretending to be an expert in VB but here is a working example:
Imports System
Public Class Test
Public Shared Sub Main()
Dim runningVB As Boolean
' Check to see if program is running on Visual Basic engine.
If scriptEngine = "VB" Then
runningVB = 16
If runningVB = True Then
Console.WriteLine("True")
End If
runningVB = 0
If runningVB = False Then
Console.WriteLine("False")
End If
End If
End Sub
End Class
With the output:
True
False
So, like many other programming languages, false will be for Zero and true otherwise. Even though this type of checking is perfectly acceptable in C, the C# compiler probably will complain.
Upvotes: 4
Reputation: 641
maybe the sample code below will help.. ;-)
Option Strict Off ' this code will not compile with (Option Strict On)
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim boolVariable As Boolean = False
boolVariable = -1 ' anything non-zero will set boolean to True
Debug.WriteLine(boolVariable)
boolVariable = 0 ' zero will set boolean to False
Debug.WriteLine(boolVariable)
End Sub
End Class
Upvotes: 0