I know the title is confusing so I’ll try to explain better. Here’s basically what I want to do inside a method:
if (record["id"] != DBNull.Value) _id = Convert.ToInt32(record["id"]);
else id = -1;
I want this to work for multiple types that I have stored in my database. (So if it’s a string it converts it to a string and so forth). Any way to do this is fine, and I was trying to do it with a method. I got this far but C# won’t automatically convert int to object. ideas?
private void Load(ref object var, object obj, object def)
{
if (var is int)
{
var = Convert.ToInt32(obj);
}
}
int _id;
Load(ref _id, record["id"], -1);
Just to clarify, my error is “cannot convert from ref int to ref object”.
Thanks for any help.
You can use
Convert.ChangeType()and make your method generic:Now you can just use it like this (simplified example, not sure what you need
deffor):