Neeraj Kumar Gupta
Neeraj Kumar Gupta

Reputation: 2363

static implicit operator for integer

I have created a custom class to hold a integer value read from custom configuration written in application configuration file, it handles integer value perfectly but there is one problem when I set this custom class value to an object type variable it assign whole object to variable instead of its value. Following is the code that I have written.

Please help me how I can get only the value in object type variable instead of whole object of custom integer class.

public class IntValueElement : ConfigurationElement
{
    [ConfigurationProperty("value")]
    public int? Value
    {
        get
        {
            int result;
            if (int.TryParse(Convert.ToString(base["value"]), out result))
            {
                return result;
            }
            else
            {
                return null;
            }
        }
    }

    public static implicit operator int?(IntValueElement dt)
    {
        return dt.Value;
    }

    public static implicit operator int(IntValueElement dt)
    {
        if (dt.Value.HasValue)
        {
            return dt.Value.Value;
        }
        else
        {
            return 0;
        }
    }
}

public class CommonConfig : ConfigurationSection
{
    [ConfigurationProperty("companyID")]
    public IntValueElement CompanyID
    {
        get { return (IntValueElement)base["companyID"]; }
    }
}

class Program
{
    static void Main(string[] args)
    {
        StartProcess();
    }

    private static void StartProcess()
    {
        CommonConfig cc = AppConfigReader.GetCommonSettings();

        int compayID = cc.CompanyID;  // perfectly set the company id (an integer value read from app config file) in compayID variable;
        int? compayID2 = cc.CompanyID; // perfectly set the company id (an integer value read from app config file) in compayID2 variable;
        object companyID3 = cc.CompanyID; // here it does not set the company id an integer value in compayID3 variable, instead it set the whole IntValueElement object in companyID3 variable;

    }
}

Upvotes: 2

Views: 1451

Answers (2)

Nkosi
Nkosi

Reputation: 247068

It will only work if you explicitly cast it

object companyID3 = (int?)cc.CompanyID;
object companyID4 = (int)cc.CompanyID;

that is because all types derive from object so the implicit operator is not applied when assigning to a base type.

user-defined conversion to or from a base class are not allowed.

Documentation: Using Conversion Operators (C# Programming Guide)

Upvotes: 1

Abion47
Abion47

Reputation: 24661

When you assign a value to a variable of a completely different type without an explicit cast, the program will look for the first implicit casting that works for the target type. With int and int?, your class defines implicit conversions for those types, so the program will use those.

For object, though, there is no conversion necessary, since your IntValueElement type (and consequently every other type) derives from object. In this case, it is merely inheritance, not an implicit cast, so the program isn't going to go looking for a casting option. (And incidentally, as Nkosi points out in his answer, you can't define an implicit cast from a type to a base type for this very reason.)

If you want the program to cast your value when assigning to an object variable, you have to make it explicit:

object companyID3 = (int)cc.CompanyID;
    // OR
object companyID3 = (int?)cc.CompanyID;

Upvotes: 0

Related Questions