T
Thomas Hansen
Hi!
I fell in a pit today while coding.. calling OverLoadedMethod (see
below) with anything but literal 0 calls the object overload, but with
0 the enum overload is automagically called. No warnings, no errors.
The Framework class System.Data.SqldClietn.SqlParameter behaves this
way... you can create SqlParameter using an object value and that
works great if the value is not literal 0.. then it decides the 0
matches the enum and you get something quite different than what you
expected..
By design or flaw?
using System;
class App
{
public enum SomeKindOfEnum
{
Should,Red,Flag,ComeUp,Now
}
static void Main(string[] args)
{
int varZero = 0;
int varOne = 1;
OverloadedMethod( varZero );
OverloadedMethod( varOne );
OverloadedMethod( 0 );
OverloadedMethod( 1 );
}
static void OverloadedMethod( object a)
{
// This is the one I want to call.
}
static void OverloadedMethod( SomeKindOfEnum a)
{
Console.WriteLine("Bummer!");
}
}
I fell in a pit today while coding.. calling OverLoadedMethod (see
below) with anything but literal 0 calls the object overload, but with
0 the enum overload is automagically called. No warnings, no errors.
The Framework class System.Data.SqldClietn.SqlParameter behaves this
way... you can create SqlParameter using an object value and that
works great if the value is not literal 0.. then it decides the 0
matches the enum and you get something quite different than what you
expected..
By design or flaw?
using System;
class App
{
public enum SomeKindOfEnum
{
Should,Red,Flag,ComeUp,Now
}
static void Main(string[] args)
{
int varZero = 0;
int varOne = 1;
OverloadedMethod( varZero );
OverloadedMethod( varOne );
OverloadedMethod( 0 );
OverloadedMethod( 1 );
}
static void OverloadedMethod( object a)
{
// This is the one I want to call.
}
static void OverloadedMethod( SomeKindOfEnum a)
{
Console.WriteLine("Bummer!");
}
}