请教: 关于C#中的拷贝构造函数该如何实现?

rouser 2003-03-09 02:39:27
写一Child Form中的一个查询,为保证与主Form的一致,需要内部的一个DataSet由其MdiParent中一个已形成DataSet拷贝构造.但不知道在C#中怎么实现呢?
谢谢
...全文
209 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
rouser 2003-03-10
  • 打赏
  • 举报
回复
Thanks to all
I see.

Now,deal with first problem pls.
rouser 2003-03-10
  • 打赏
  • 举报
回复
j在调用前赋i的值
j=i;
one.testOut(j,out i);
timmy3310 2003-03-10
  • 打赏
  • 举报
回复
out 的参数必须在离开Statement之前赋值

编译器会检查所有可能的执行路径,保证参数在离开方法之前赋值。

你把Try取掉之后,这个方法只有一个执行路径,所以不会报错

但是加上Try之后,i只在try里面被赋值,在Catch之中没有赋值,也就是说一旦抛出异常,这个方法返回的i是没有被赋值的,这是编译器不允许的。所以你的代码编译通不过。

修改的方法是在catch块中也对i进行赋值。
dragontt 2003-03-10
  • 打赏
  • 举报
回复
to rouser(明灭由心) ;
报错:
The out parameter 'i' must be assigned to before control leaves the current method

但是我将try{}catch()结构去掉就没事了.

是因为,这里你的变量i是一个返回变量
必须在你的程序中对它的值进行设定
而,在你的代码中对i的操作
是嵌入在try{}catch{}中,这种结构中编译器不认为你的程序段一定会对i付值
因此编译不过
就好象
你不能再程序段中使用如下结构
if( true )
string s = "stet";
s = "...";
编译器同样会认为你的s变量是没有声明的
rouser 2003-03-10
  • 打赏
  • 举报
回复
不过根据你的思路
临时的解决方法可以改写testOut为
testOut(int j,out int i)
{
i=j;

// 下面一样

谢谢你
帖完了,我会结分给你
rouser 2003-03-10
  • 打赏
  • 举报
回复
在i被代入one.TestOut之前是需要经过运算的
所以必须在外部初始化
你的做法可以使程序运行上不错
但是,这显然不是解决之道
对吗?

public class COne
{
public COne()
{
//
// TODO: Add constructor logic here
//
}

public string testOut(out int i)
{
try
{
i=5;
// Other statements use i here...
}
catch(Exception ee)
{
return ee.Message;
}

return "";
}
}
我现在testOne

COne one=new COne();
int i=0;
// Statements use i here...
if(one.testOut(out i)=="")
MessageBox.Show(i.ToString());
报错:
The out parameter 'i' must be assigned to before control leaves the current method

但是我将try{}catch()结构去掉就没事了.
Why?
tuzi98 2003-03-10
  • 打赏
  • 举报
回复
public class COne
{
public COne()
{
//
// TODO: Add constructor logic here
//
}

public string testOut(out int i)
{
i= 0;//在这里初始化
try
{
i=5;
}
catch(Exception ee)
{
return ee.Message;
}

return "";
}
}
我现在testOne

COne one=new COne();
//int i=0;根据out参数的性质,这里i不用初始化的
int i;

if(one.testOut(out i)=="")
MessageBox.Show(i.ToString());
rouser 2003-03-10
  • 打赏
  • 举报
回复
Hi,Anybody can help me here?
rouser 2003-03-09
  • 打赏
  • 举报
回复
Push!
rouser 2003-03-09
  • 打赏
  • 举报
回复
我加try{]catch()纯粹是为了测试
lbx1979 2003-03-09
  • 打赏
  • 举报
回复
public string testOut(out int i)
{
try
{
i=5;
return "";
}
catch(Exception ee)
{
return ee.Message;
}

这样行吗
lbx1979 2003-03-09
  • 打赏
  • 举报
回复
我觉得你没有必要try catch
rouser 2003-03-09
  • 打赏
  • 举报
回复
再加一个问题
看下面的例程
public class COne
{
public COne()
{
//
// TODO: Add constructor logic here
//
}

public string testOut(out int i)
{
try
{
i=5;
}
catch(Exception ee)
{
return ee.Message;
}

return "";
}
}
我现在testOne

COne one=new COne();
int i=0;
if(one.testOut(out i)=="")
MessageBox.Show(i.ToString());
报错:
The out parameter 'i' must be assigned to before control leaves the current method

但是我将try{}catch()结构去掉就没事了.
Why?

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧