Mathematical Operations And Implicit Widening Support

What does this mean? It means that support for performing mathematical operations directly with the NumericInput Box has been built-in.

You will be able to enter statements like these:

VB
Dim d as double = NumericInputBox1 + 3.4
NumericInputBox1.Value = NumericInputBox2 + NumericInputBox2

C#
double d = numericInputBox1 + 3.4;
numericInputBox1.Value = numericInputBox2 + numericInputBox2;

Be advised that this support involves widening NumericInputBox.Value and the other operand to a common return type. The common type is based on the other operand’s type. The type widened to and available operators are shown in the following table.

Second Operand Type Returned Type Supported Operators

Byte , SByte,
Int16, Int32, Int64

Int64

+, -, *, /, ^, \

VB: Mod, And, Or, Xor

C#: %, &, |, ~

UInt16, UInt32, UInt64

UInt64

+, -, *, /, ^, \

VB: Mod, And, Or, Xor

C#: %, &, |, ~

Single, Double, Decimal

Double

+, -, *, /, ^, \

NumericInputBox

Double

+, -, *, /, ^, \

While the IDE may allow you to enter these types of statements, you may still encounter a run-time error if Widening the NumericInputBox's value type is invalid. This is by design and does not take into account that a valid conversion may be possible based on the current value of the NumericInputBox.  This error is thrown solely based on the specified System Type of the NumericInputBox.

For Example:

VB
Dim ui64 As UInt64 = CULng(NIBInt16 + 3UI)

C#
System.UInt64 ui64 = (System.UInt64)(NIBInt16 + 3u);

The IDE will allow the above statements to be entered with out warning.  However, run-time checks will be performed to verify the NIBInt16’s Type (Int16) can be safely widened to a UInt32.  In the strictest terms, this is not a valid widening operation and this run-time exception will be generated.

Invalid Widening Error

This error may be avoided by rewriting these statements as shown below.

VB
Dim ui64 As UInt64 = NIBInt16.ToUInt + 3UI

C#
System.UInt64 ui64 = NIBInt16.ToUInt + 3u;

However, to do so may not prevent another run-time error if NIBInt16 is assigned a negative value. This type of error will not occur until such case exists and may not occur until your application is in your user's hands. This is the reason that the NumericInputBox's default widening imposes a strict run-time type conversion check. These checks are for the benefit of the developer but as seen by these examples poor attention to selected types can result in errors sneaking into your code. The following error message will result if the NIBInt16.Value any negative value.

Overflow Error

Typical GotchaYa

VB
Dim D As Double = NumericInputBox1 + 5

C#
double d = numericInputBox1 + 5;

The above code will throw an Invalid Widening error because "5" is implied to be an integer; were-as the following will work without error.

VB
Dim D As Double = NumericInputBox1 + 5.0

C#
double d = numericInputBox1 + 5.0;

Implicit Widening Support allows for implicit conversions from a NumericInputBox to the standard numeric types.

For Example:

VB
Dim d as Double = NumericInputBox1

C#
double d = NumericInputBox1;

Last edited Feb 22, 2013 at 2:00 PM by TnTInMN, version 26

Comments

No comments yet.