日期:2014-05-18  浏览次数:20895 次

一个小问题,关于实例化ListView对象
private void listViewFilesAndFolders_ItemActivate(object sender, EventArgs e)
  {
  // Cast the sender to a ListView and get the tag of the first selected item.
  System.Windows.Forms.ListView lw = (System.Windows.Forms.ListView)sender;
  string filename = lw.SelectedItems[0].Tag.ToString();
   


  }

为什么一定要用System.Windows.Forms.ListView呢,我在代码的顶部已经写了using System.Windows.Forms,为什么不能直接用ListView lw呢?我试过,不可以的。
为什么实例化lw的时候不用new关键字呢?

多谢了,各位

------解决方案--------------------
我这测试可能直接用ListView lw

第二个问题

我们通常是
ListView lw=new ListView();

其中ListView lw 是建立了一个ListView实例的引用lw
new ListView()才是真正建立了一个ListView的实例
中间的"="就是让你的引用 指向真正的实例
这和指针有点像 只不过引用是类型安全且不可运算的指针

你的代码中已经有了ListView的实例 所以就不有new来实例化一个了
只要lw=去引用它就行了