SUMTEC -- There's a thing in my bloglet.

But it's not only one. It's many. It's the same as other things but it exactly likes nothing else...

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  207 随笔 :: 19 文章 :: 1637 评论 :: 12 Trackbacks
MS的代码是这样的:

public static Form ActiveForm
{
      
get
      
{
            IntSecurity.GetParent.Demand();
            IntPtr ptr1 
= UnsafeNativeMethods.GetForegroundWindow();
            Control control1 
= Control.FromHandleInternal(ptr1);
            
if ((control1 != null&& ((control1 as Form) != null))
            
{
                  
return ((Form) control1);
            }

            
return null;
      }

}

 

为什么需要首先进行control1 != null 的判断呢?我怎么记得null as Form == null的,也就是说,难道不能够这么写吗?
public static Form ActiveForm
{
      
get
      
{
            IntSecurity.GetParent.Demand();
            IntPtr ptr1 
= UnsafeNativeMethods.GetForegroundWindow();
            Control control1 
= Control.FromHandleInternal(ptr1);
            
if ((control1 as Form) != null)
            
{
                  
return ((Form) control1);
            }

            
return null;
      }

}

还在好多地方看到一些问题,不一一列出来了。
posted on 2004-07-17 15:43 Sumtec 阅读(410) 评论(2)  编辑 收藏 所属分类: .NET 技术内幕

评论

#1楼  2004-07-18 10:56 hyifeng [未注册用户]
 如果考虑null as Form == null,
就一句 return (control1 as Form); 就够了。

这可能和微软的编码规范有关。
  回复  引用    

#2楼  2004-07-20 20:06 polarlm [未注册用户]
我觉得是不是一个效率问题。

  回复  引用    


标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      


相关链接: