最近新学C#,按照Visual c# 2010从入门到精通里的实例写程序,只写了一部分就报错了,看了几次和书上写的一样,请高手指点,谢谢。代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using System.Windows.Shapes;namespace Drawing
{
    class Square:IDraw, IColor
    {
        private int sideLength;
        private int locX = 0, locY = 0;
        private Rectangle rect = null;
        public Square(int sideLength)
        {
            this.sideLength = sideLength;
        }        void IDraw.SetLocation(int xCoord, int yCoord)
        {
            this.locX = xCoord;
            this.locY = yCoord;
        }        void IDraw.Draw(System.Windows.Controls.Canvas canvas)
        {
            if (this.rect != null)
            {
                canvas.Children.Remove(this.rect);
            }
            else
            {
                this.rect = new Rectangle();
            }
            this.rect.Height = this.sideLength;
            this.rect.Width = this.sideLength;
            canvas.SetTop(this.rect, this.locY);
            canvas.SetLeft(this.rect, this.locX);
            canvas.Children.Add(rect);
        }
    }
}错误提示:
错误 1 无法使用实例引用来访问成员“System.Windows.Controls.Canvas.SetTop(System.Windows.UIElement, double)”;请改用类型名来限定它

错误 2 无法使用实例引用来访问成员“System.Windows.Controls.Canvas.SetLeft(System.Windows.UIElement, double)”;请改用类型名来限定它

解决方案 »

  1.   

    函数原型是
    public static void SetTop(
    UIElement element,
    double length
    )
    静态方法不属于特定实例
      

  2.   

    http://msdn.microsoft.com/zh-tw/library/system.windows.controls.canvas.settop.aspx
    这是个静态的方法
      

  3.   

    静态方法不可实例化,可通过“类名+静态方法”直接访问,Square.SetTop();
      

  4.   

    Visual c# 2010 都出来了呀?是不是正版的哟!
      

  5.   

    Canvas.SetTop(this.rect, this.locY);//大写
      

  6.   


    System.Windows.Controls.Canvas.SetLeft(this.rect, this.locX);
    System.Windows.Controls.Canvas.Children.Add(rect);