Implicit coversions

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

Why does VB.Net convert:

Dim CCNumber as String = "123"
For i As Integer = 0 To (CCNumber.Text.Length / 2)

to

For i As Integer = 0 To 1.5

In C# it leaves the conversion as an integer and truncates it, so that 3/2 =
1 and 4/2 = 2 and 5/2 = 2.

Thanks,

Tom
 
(CCNumber.Length/2) results in a decimal value so VB returns a double
http://msdn2.microsoft.com/en-us/library/ms144717.aspx

It would have been caught in your for...next loop though if you would have
put:

Option Explicit On
Option Strict On

at the top of your code page.

Now then, on to the rounding aspect. 1 is the floor value of 3/2 while 2 is
the normal rounded value of 3/2. This is because VB rounds to the nearest
even number.

cInt(1.5) = 2
cInt(2.5) = 2

math.round(1.5,0) =2
math.round(2.5,0) =2

Well that is the default anyway. To change this behavior you will need to
specify a third option in the round statement.

math.round(1.5,0,midpointrounding.awayfromzero) = 2
math.round(2.5,0,midpointrounding.awayfromzero) = 3

However math.floor() will truncate all decimal values towards zero.

hope that helps.
 
Why does VB.Net convert:

Dim CCNumber as String = "123"
For i As Integer = 0 To (CCNumber.Text.Length / 2)

to

For i As Integer = 0 To 1.5

In C# it leaves the conversion as an integer and truncates it, so that 3/2 =
1 and 4/2 = 2 and 5/2 = 2.

Thanks,

Tom

Change your division operator...

For i As Integer = 0 To (CCNumber.Text.Length \ 2)

That should do integer division, instead of using double values.
VB.NET has two division operators. The one you normally think of /
converts it's operands to double values before it does the division
and yeilds a double result. In C#, the reason you see integer
division is because all the operands are integers. If you were to
change your / 2 to / 2.0 you would get the same result as in VB.NET.
 
amdrit said:
(CCNumber.Length/2) results in a decimal value so VB returns a double
http://msdn2.microsoft.com/en-us/library/ms144717.aspx

It would have been caught in your for...next loop though if you would have
put:

Option Explicit On
Option Strict On

at the top of your code page.

Now then, on to the rounding aspect. 1 is the floor value of 3/2 while 2
is the normal rounded value of 3/2. This is because VB rounds to the
nearest even number.

cInt(1.5) = 2
cInt(2.5) = 2

math.round(1.5,0) =2
math.round(2.5,0) =2

Well that is the default anyway. To change this behavior you will need to
specify a third option in the round statement.

math.round(1.5,0,midpointrounding.awayfromzero) = 2
math.round(2.5,0,midpointrounding.awayfromzero) = 3

However math.floor() will truncate all decimal values towards zero.

hope that helps.

It does.

Thanks,

Tom
 
Tom Shelton said:
Change your division operator...

For i As Integer = 0 To (CCNumber.Text.Length \ 2)

That should do integer division, instead of using double values.
VB.NET has two division operators. The one you normally think of /
converts it's operands to double values before it does the division
and yeilds a double result.

Didn't know that.

That would solve the problem.
In C#, the reason you see integer
division is because all the operands are integers. If you were to
change your / 2 to / 2.0 you would get the same result as in VB.NET.

And that was what I assumed here. I assumed that because both of the
operands were integer - the result would be integer. Especially since the
counter is integer.

And it does make sense if as you say that the "/" changes both operands to
double that the result would also be double.

Thanks,

Tom
 
Back
Top