2

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;

    }
}

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

1 Comment

thanks @Nkosi, I know casting is one of the way, but I was curious to know is there any other way ..
0

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;

7 Comments

If you don't mind can you provide me the link or how do I use explicit operator for my scenario. I have tried with explicit but did not get success.
@NeerajKumarGupta Define "did not get success".
this code I added in IntValueElement Class public static explicit operator object(IntValueElement dt) { return (int)dt; } but it gives compiler error "user-defined conversions to or from a base class are not allowed"
@NeerajKumarGupta Both mine and Nkosi say that exact operation is not allowed. You can't define conversion operators for a base class, and object is every class's base class.
Ok, I got your point. actually I thought that you have written in your comment "you have to make it explicit:" so my assumption was that it still possible by using "explicit operator" that I had already tried :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.