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;
}
}