W
Webbert
I wrote a little test to demonstrate.
Test 1 - should fail - can not cast null to an Int16
Test 2 - should result in the value 0 - one cast on the value returned
Test 3 - should result in the value 0 - cast on each return value
Test 4 - should result in the value 0 - normal cast
Test 5 - should result in the value 0 - Same as test 2 but hard coded
Any thoughts are appreciated.
Dave
============
Test 1: Failed: Object reference not set to an instance of an object.
Test 2: Failed: Specified cast is not valid.
Test 3: Successful: 0
Test 4: Successful: 0
Test 5: Successful: 0
============
using System;
using System.Data;
using System.Diagnostics;
namespace ConditionalCheck
{
class Program
{
static void Main( string[] args )
{
bool rc;
Int16 number;
// Setup
DataTable dt = new DataTable();
dt.Columns.Add( new DataColumn( "port", typeof( Int16 ) ) );
dt.Rows.Add();
Debug.WriteLine( "\n\n\n" );
Debug.WriteLine( "===================\n" );
// Test
object v = dt.Rows[0]["port"];
try { number = (Int16)( ( v != DBNull.Value ) ? v : null );
Debug.WriteLine( "Test 1: Successful: " + number ); }
catch ( Exception ex ) { Debug.WriteLine( "Test 1: Failed: " + ex.Message
); }
try { number = (Int16)( ( v != DBNull.Value ) ? v : 0 ); Debug.WriteLine(
"Test 2: Successful: " + number ); }
catch ( Exception ex ) { Debug.WriteLine( "Test 2: Failed: " + ex.Message
); }
try { number = ( ( v != DBNull.Value ) ? (Int16)v : (Int16)0 );
Debug.WriteLine( "Test 3: Successful: " + number ); }
catch ( Exception ex ) { Debug.WriteLine( "Test 3: Failed: " + ex.Message
); }
number = 0;
object y = number;
try { number = (Int16)( y ); Debug.WriteLine( "Test 4: Successful: " +
number ); }
catch ( Exception ex ) { Debug.WriteLine( "Test 4: Failed: " + ex.Message
); }
rc = ( number == 500 );
try { number = (Int16)( ( rc ) ? 1 : 0 ); Debug.WriteLine( "Test 5:
Successful: " + number ); }
catch ( Exception ex ) { Debug.WriteLine( "Test 5: Failed: " + ex.Message
); }
Debug.WriteLine( "\n===================\n" );
}
}
}
Test 1 - should fail - can not cast null to an Int16
Test 2 - should result in the value 0 - one cast on the value returned
Test 3 - should result in the value 0 - cast on each return value
Test 4 - should result in the value 0 - normal cast
Test 5 - should result in the value 0 - Same as test 2 but hard coded
Any thoughts are appreciated.
Dave
============
Test 1: Failed: Object reference not set to an instance of an object.
Test 2: Failed: Specified cast is not valid.
Test 3: Successful: 0
Test 4: Successful: 0
Test 5: Successful: 0
============
using System;
using System.Data;
using System.Diagnostics;
namespace ConditionalCheck
{
class Program
{
static void Main( string[] args )
{
bool rc;
Int16 number;
// Setup
DataTable dt = new DataTable();
dt.Columns.Add( new DataColumn( "port", typeof( Int16 ) ) );
dt.Rows.Add();
Debug.WriteLine( "\n\n\n" );
Debug.WriteLine( "===================\n" );
// Test
object v = dt.Rows[0]["port"];
try { number = (Int16)( ( v != DBNull.Value ) ? v : null );
Debug.WriteLine( "Test 1: Successful: " + number ); }
catch ( Exception ex ) { Debug.WriteLine( "Test 1: Failed: " + ex.Message
); }
try { number = (Int16)( ( v != DBNull.Value ) ? v : 0 ); Debug.WriteLine(
"Test 2: Successful: " + number ); }
catch ( Exception ex ) { Debug.WriteLine( "Test 2: Failed: " + ex.Message
); }
try { number = ( ( v != DBNull.Value ) ? (Int16)v : (Int16)0 );
Debug.WriteLine( "Test 3: Successful: " + number ); }
catch ( Exception ex ) { Debug.WriteLine( "Test 3: Failed: " + ex.Message
); }
number = 0;
object y = number;
try { number = (Int16)( y ); Debug.WriteLine( "Test 4: Successful: " +
number ); }
catch ( Exception ex ) { Debug.WriteLine( "Test 4: Failed: " + ex.Message
); }
rc = ( number == 500 );
try { number = (Int16)( ( rc ) ? 1 : 0 ); Debug.WriteLine( "Test 5:
Successful: " + number ); }
catch ( Exception ex ) { Debug.WriteLine( "Test 5: Failed: " + ex.Message
); }
Debug.WriteLine( "\n===================\n" );
}
}
}