【问题标题】:class method "does not contain definition for and no extension method"类方法“不包含定义,也没有扩展方法”
【发布时间】:2015-07-27 19:11:27
【问题描述】:

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

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

非常感谢任何可以帮助我继续学习的人;)

主代码文件:

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;
        }


    }

}

【问题讨论】:

  • @DStanley 它们无论如何都不会被访问,因为它们被设置为private。
  • 您的班级没有您显示的公共X 和Y 属性 - 具体错误是什么?
  • 您还应该将Point 放在命名空间中。
  • 错误 1 ​​'System.Windows.Point' 不包含 'DistanceTo' 的定义,并且找不到接受第一个参数类型为 'System.Windows.Point' 的扩展方法 'DistanceTo' (您是否缺少 using 指令或程序集引用?) C:\Users\Siebe\Documents\Visual Studio 2013\Projects\Verita\Verita\MainWindow.xaml.cs 37 42 Verita 这是错误。它找到实例的 X 和 Y 值,这不是问题。但是当我尝试访问方法 DistanceTo 它给出了这个错误
  • 你来自Java背景吗?

标签: c# class methods instance


【解决方案1】:

在Habib's 答案之上,您的x 和y 属性需要设置为公开才能访问:

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

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

【讨论】:

  • 谢谢!在尝试实施 habib 的建议后,我确​​实遇到了区分大小写的问题,所以感谢您指出这一点:)
  • 没问题!请务必将Habib's 答案标记为正确答案。 :)
【解决方案2】:

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

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

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

然后在使用的时候:

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

但现在您将遇到新错误,因为您没有将 X 和 Y 暴露为 public。

【讨论】:

  • 谢谢,确实是这个问题!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-09
  • 2018-09-24
  • 1970-01-01
  • 2014-03-14
  • 2018-12-13
  • 1970-01-01
相关资源
最近更新 更多