类方法“不包含定义,也没有扩展方法”

时间:2015-05-15 18:43:46

标签: c# class methods instance

我正在尝试遵循C#的教程,但我坚持使用类方法,它说它找不到方法,即使它在那里。很明显我错过了什么,但是什么?

我认为它与在'DistanceTo'方法中找不到类'Point'有关,因为参数'Point'在Visual Studio中没有变成浅蓝色。

非常感谢能帮助我继续学习的人;)

主要代码文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Verita
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {

            Point firstPoint = new Point(0, 0);
            Point secondPoint = new Point(100, 100);

            Output.Text = firstPoint.X.ToString();
            Output.Text += secondPoint.Y.ToString();

            double distance = firstPoint.DistanceTo(secondPoint);
            Output.Text += distance;


        }    
    }
}

班级档案:

using System;

/// <summary>
/// Summary description for Class1
/// </summary>
public class Point
{
    private int x, y;

    public Point()
    {
        this.x = -1;
        this.y = -1;
    }

    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public double DistanceTo(Point other)
    {
        int xDiff = this.x - other.x;
        int yDiff = this.y - other.y;

        double distance = Math.Sqrt((xDiff * xDiff) + (yDiff * yDiff));
        return distance;
    }


}
编辑:解决了问题!谢谢habib并画了。 我编写了代码,现在它工作正常:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Verita
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {

            Classes.Userpoint firstPoint = new Classes.Userpoint(0, 0);
            Classes.Userpoint secondPoint = new Classes.Userpoint(100, 100);

            Output.Text = firstPoint.x.ToString();
            Output.Text += secondPoint.y.ToString();

            double distance = firstPoint.DistanceTo(secondPoint);
            Output.Text += distance;


        }
    }  
}

namespace Classes
{

    public class Userpoint
    {
        public int x, y;

        public Userpoint()
        {
            this.x = -1;
            this.y = -1;
        }

        public Userpoint(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        public double DistanceTo(Userpoint other)
        {
            int xDiff = this.x - other.x;
            int yDiff = this.y - other.y;

            double distance = Math.Sqrt((xDiff * xDiff) + (yDiff * yDiff));
            return distance;
        }


    }

}

2 个答案:

答案 0 :(得分:1)

查看错误(DistanceTo未找到)和属性X以及Y可用,我只能想象您正在使用框架提供的Point structure,并且您的原始课程不在任何地方使用。

我建议您将类名修改为不同的名称或指定名称空间并将其与该名称空间一起使用。

namespace Verita
{
    public class Point
    {
        private int x, y;

然后在使用它时:

Verita.Point firstPoint = new Verita.Point(0, 0);

但是现在您最终会遇到新的错误,因为您没有将XY公开为public

答案 1 :(得分:1)

Habib's回答之上,您的xy属性需要设置为公开才能被访问:

public int x { get; set; }
public int y { get; set; }

另外,请检查您尝试访问的属性的区分大小写。