参考或了解如何将其设置为对象 [英] Ref or out how to set it as object
问题描述
我有一个方法Thad将数据填充到网格并通过ref数据适配器对象返回,以便能够在行更新后更新网格中的数据...
和i疯狂的传递OLEDB或SQLDB数据适配器的方法变量
但是当我使用ref或Out to Data Adapter错误时出现
我尝试过:
这个代码来自方法
枚举ConnectionType
{
SQL,
OLEDB
}
public void Grid(ConnectionType ADOType,GridControl obj,object cn,DataSet DS,ref object DA,ref DataTable DT,String DataTableName,string sqlString,params object [] Parameter)
{
if(ADOType == ConnectionType.OLEDB)
{
DataManagerOLEDB DM_ = new DataManagerOLEDB();
OleDbDataAdapter da =(OleDbDataAdapter)DA;
DM_.SelectFromTable((OleDbConnection)cn,sqlString,DS,out da,
DataTableName,Parameter);
}
else
{
DataManagerSQL DM_ = new DataManagerSQL();
SqlDataAdapter da =(SqlDataAdapter)DA;
DM_.SelectFromTable((SqlConnection)cn,sqlString,DS,out da,
DataTableName,Parameter);
}
DT = DS.Tables [DataTableName];
obj.DataSource = DT;
当像这样的通行方法时
< pre> SurveyVar.fillDxControl.Grid(ConnectionType.OLEDB,grdSurvey,SurveyVar.cnSurvey,SurveyVar.DataSetSurvey,ref SurveyVar.DataAdapterSurvey,ref datSurvey,T002,sqlString,vParameters) ;
数据适配器变量出现错误为
严重性代码描述项目文件行抑制状态
错误CS1503参数5:无法从'ref System.Data.OleDb.OleDbDataAdapter'转换为'ref object'调查F:\ BannaSQLSource \_New Application \ Source Code \SurveyApplication \ Application Caption \forms\frmSurvey.cs 125 Active
让我们稍微简化一下:
< pre lang =c#> private void MyMethod( ref object o)
{
}
private void MyButton_Click( object sender,EventArgs ew)
{
OleDbDataAdapter da = < span class =code-keyword> new OleDbDataAdapter();
MyMethod( ref da);
...为什么会收到错误消息:
无法从'ref System.Data.OleDb.OleDbDataAdapter转换'to'ref object'想一想:o是对象的引用,所以你可以非常合理地在方法中放入任何类型的值:
< pre lang =c#> private void MyMethod( ref object o)
{
o = 你好跨度>;
}如果系统允许你这样做,会有什么影响?
不愉快,就是这样。因为o
是ref
,所以对它的任何更改都会反映在调用方法参数变量中 - =在这种情况下,da
设置为字符串。因此,当控件返回到按钮单击事件并且代码尝试使用DataAdapter中的数据时,它会找到一个字符串,一切都开始失败。
As结果,你不能转换ref参数 - 类型必须完全匹配。
如果你需要将不同的对象类型作为ref参数传递,那么请考虑使用泛型。
I have a Method Thad Fill Data to Grid and return by ref data adapter object to able me to update data in grid after row update ...
and i Mad a Method to Pass OLEDB or SQLDB data adapter Variable
but when I use a ref or Out to Data Adapter error appear
What I have tried:
this a code from Method
enum ConnectionType { SQL, OLEDB }
public void Grid(ConnectionType ADOType, GridControl obj, object cn, DataSet DS, ref object DA, ref DataTable DT, String DataTableName, string sqlString, params object[] Parameter) { if (ADOType == ConnectionType.OLEDB) { DataManagerOLEDB DM_ = new DataManagerOLEDB(); OleDbDataAdapter da = (OleDbDataAdapter)DA; DM_.SelectFromTable((OleDbConnection)cn, sqlString, DS, out da, DataTableName, Parameter); } else { DataManagerSQL DM_ = new DataManagerSQL(); SqlDataAdapter da = (SqlDataAdapter)DA; DM_.SelectFromTable((SqlConnection)cn, sqlString, DS, out da, DataTableName, Parameter); } DT = DS.Tables[DataTableName]; obj.DataSource = DT;
when Pass Method like This
<pre>SurveyVar.fillDxControl.Grid(ConnectionType.OLEDB, grdSurvey, SurveyVar.cnSurvey, SurveyVar.DataSetSurvey, ref SurveyVar.DataAdapterSurvey, ref datSurvey, "T002", sqlString, vParameters);
Error appear on data adapter variable as "
Severity Code Description Project File Line Suppression State Error CS1503 Argument 5: cannot convert from 'ref System.Data.OleDb.OleDbDataAdapter' to 'ref object' Survey F:\BannaSQLSource\_New Application\Source Code\SurveyApplication\Application Caption\forms\frmSurvey.cs 125 Active
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. Becauseo
is aref
, 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.
这篇关于参考或了解如何将其设置为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!