What does "UPGRADE_WARNING: Mod has a new behavior" refer to?
Asked Answered
D

1

5

I had to convert some VB6 code to VB.net using the old VS2008 conversion tool. That tool helpfully inserts warning comments when it was not sure that the original & converted code was equivalent. One such example is:

UPGRADE_WARNING: Mod has a new behavior

Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'

This ms-help://... "link" which as far as I can tell no longer functions. I was not able to find anything on the web referencing that link anymore either.

What is this warning actually referring to? What's the difference in the mod function?

Dunaj answered 15/8, 2022 at 14:41 Comment(1)
The 'UPGRADE_WARNING' entries refer to "... for Visual Basic 6.0 users" entries in the Visual Studio 2008 help. These entries are also available on MSDN.Drivein
D
6

First off, this explains the language change itself. Secondly addressing the point about the ms-help link.


There is an explanation given on https://www.vbmigration.com/documentation/chapter3.aspx:

Mod operator

The VB6’s Mod operator automatically converts its operands to integer values and returns the remainder of integer division:

Dim d As Double, i As Integer
d = 1.8: i = 11
Debug.Print i Mod d     ' Displays 1, because it rounds up 1.8 to 2

VB.NET and C# don’t convert to Integer and return the remainder of floating-point division if any of the two operands is of type Single or Double.

That same page goes on to suggestion that by adding a CInt() call you can reproduce the original behavior, such as:

Debug.WriteLine(i Mod CInt(d))

This is corroborated by official VB documentation.

VB6:

Remarks

The modulus, or remainder, operator divides number1 by number2 (rounding floating-point numbers to integers) and returns only the remainder as result.

(actually the VBA docs...)

VB.Net:

Remarks

If either number1 or number2 is a floating-point value, the floating-point remainder of the division is returned. The data type of the result is the smallest data type that can hold all possible values that result from division with the data types of number1 and number2.


Note that the ms-help links would actually work if you still have VS2008 installed. It brings up the built in local help GUI. Not sure when these links stopped working, but I think by at least VS2015 they did not.

For this particular warning about Mod the help system had this to say:

Mod Operator

In Visual Basic 6.0, the Mod operator accepted any numeric expression and the result was always returned as an integer.

In Visual Basic 2008, the Mod operator no longer accepts variants, and if either operand is a floating-point number, the result will be a floating-point number.

IMHO this is NOT actually a very precise description of what is different. In particular, it does not sufficiently emphasize that the division result has been changed from always performing integer division. It only mentions about the return result being an integer.

The information above is more thorough.

For completeness, it also added:

What to do next

If a variant has been converted to an object during upgrade, convert it to the appropriate numeric data type.

If either of the operands is a floating-point number, modify the data type of the result, or modify your code to convert the operand(s) using the CShort function before using the Mod operator.

Dunaj answered 15/8, 2022 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.