动态声明变量类型,怎么弄?

cuayi 2013-07-18 05:33:16
if (s == "chk")
{
DataGridViewCheckBoxColumn d = new DataGridViewCheckBoxColumn();
}
else
{
DataGridViewTextBoxColumn d = new DataGridViewTextBoxColumn();
}
d.Name = s;
d.SortMode = DataGridViewColumnSortMode.NotSortable;

动态声明变量类型,怎么弄?

d.Name = s;
d.SortMode = DataGridViewColumnSortMode.NotSortable;
这个会出错
...全文
143 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hudsonhuang 2013-07-18
  • 打赏
  • 举报
回复
引用 2 楼 cuayi 的回复:
不同类型也能赋值?
接口 基类
Nomerciness 2013-07-18
  • 打赏
  • 举报
回复
了解下变量的生命周期吧。。
  • 打赏
  • 举报
回复
可以不用赋值,直接DataGridViewColumn d;
  • 打赏
  • 举报
回复
引用 2 楼 cuayi 的回复:
不同类型也能赋值?
看成一样了。。。 外面定义改成 DataGridViewColumn d =new DataGridViewColumn();
linrachel 2013-07-18
  • 打赏
  • 举报
回复
在继承链上找找看
cuayi 2013-07-18
  • 打赏
  • 举报
回复
不同类型也能赋值?
  • 打赏
  • 举报
回复
把定义放到上面,判断里面再赋值 DataGridViewCheckBoxColumn d; if (s == "chk") { d = new DataGridViewCheckBoxColumn(); } else { d = new DataGridViewTextBoxColumn(); } d.Name = s; d.SortMode = DataGridViewColumnSortMode.NotSortable;

110,561

社区成员

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

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

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