当前位置:编程学习 > C#/ASP.NET >>

C#:嵌入的语句不能是声明或标记语句

  private void Form2_Paint(object sender, PaintEventArgs e)
        {
            if(isCustomStyle)
               System.Drawing.Drawing2D.GraphicsPath formShape=
               new System.Drawing.Drawing2D.GraphicsPath ();
            formShape.AddEllipse(0,0,this.Width ,this.Height);
            this.Region =new Region(formShape);
        }
运行时if下面的语句报错,错误 CS1023: 嵌入的语句不能是声明或标记语句
C# --------------------编程问答-------------------- http://blog.csdn.net/zyzqx2009/article/details/5627285 --------------------编程问答--------------------

private void Form2_Paint(object sender, PaintEventArgs e)
{
   if(isCustomStyle)
   {
      System.Drawing.Drawing2D.GraphicsPath formShape=
               new System.Drawing.Drawing2D.GraphicsPath ();
      formShape.AddEllipse(0,0,this.Width ,this.Height);
      this.Region =new Region(formShape);
   }
}
--------------------编程问答-------------------- 明白了,谢谢 --------------------编程问答--------------------

引用 楼主 u011602121 的回复:
  private void Form2_Paint(object sender, PaintEventArgs e)
        {
            if(isCustomStyle)
               System.Drawing.Drawing2D.GraphicsPath formShape=
               new System.Drawing.Drawing2D.GraphicsPath ();
            formShape.AddEllipse(0,0,this.Width ,this.Height);
            this.Region =new Region(formShape);
        }
运行时if下面的语句报错,错误 CS1023: 嵌入的语句不能是声明或标记语句


请搞清楚什么不用{}之后的代码块和用了{}之后的代码块.. 
 
private void Form2_Paint(object sender, PaintEventArgs e)
        {
            if(isCustomStyle)
              {
               System.Drawing.Drawing2D.GraphicsPath formShape=
               new System.Drawing.Drawing2D.GraphicsPath ();
               }
            formShape.AddEllipse(0,0,this.Width ,this.Height);
            this.Region =new Region(formShape);
        }


你那样的写法是相当于这样,你的formShape在if里面声明的,在if外面肯定用不了. 
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,