Let's simplify this a little:
private void MyMethod(ref object o)
{
}
private void MyButton_Click(object sender, EventArgs ew)
{
OleDbDataAdapter da = new OleDbDataAdapter();
MyMethod(ref da);
...
Why do you get an error saying:
cannot convert from 'ref System.Data.OleDb.OleDbDataAdapter' to 'ref object'
Think about it: o is a reference to an object, so you can very legitimately put any type of value into it inside the method:
private void MyMethod(ref object o)
{
o = "hello";
}
If the system allowed you to do that, what effect would it have?
Unpleasant, that's what. Because
o
is a
ref
, any changes to it are reflected in the calling method parameters variables -= in this case,
da
gets set to a string. So when control goes back to the button click event and the code tries to use the data in the DataAdapter, it finds a string and everything starts to fall over.
As a result, you can't cast a ref parameter - the type must match exactly.
If you need to pass different object types as ref parameters, then consider using generics instead.