简体   繁体   English

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

[英]class method “does not contain definition for and no extension method”

I am trying to follow a tutorial of C#, but I'm stuck on class methods, It says that it cannot find the method even though it's there. 我正在尝试遵循C#教程,但是我被困在类方法上,它说即使存在,也无法找到该方法。 So clearly I'm missing something but what? 显然我错过了什么,但是呢?

I think it has something to do with not finding the class 'Point' within the method 'DistanceTo' because in the parameter 'Point' is not turning lightblue in Visual Studio. 我认为这与在方法“ DistanceTo”中找不到类“ Point”有关,因为在Visual Studio中参数“ Point”没有变成浅蓝色。

Thank you so much to whomever can help me to continue learning ;) 非常感谢任何可以帮助我继续学习的人;)

Main Code File: 主代码文件:

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;


        }    
    }
}

Class File: 类文件:

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


}

EDIT: Problem Solved! 编辑:问题解决了! Thanks habib and drew. 感谢哈比卜和提请。 I edited the code to this and now it works fine: 我对此进行了编辑,现在可以正常工作:

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


    }

}

Looking at the error ( DistanceTo not found) and property X and Y being available, I can only imagine that you are using Point structure provided by framework, and your original class is not used anywhere. 查看错误(找不到DistanceTo )并且属性XY可用,我只能想象您使用的是框架提供的Point结构 ,而原始类在任何地方都没有使用。

I would suggest you to either modify your class name to something different or specify a namespace and use it with that namespace. 我建议您将您的类名修改为其他名称,或者指定一个名称空间并将其与该名称空间一起使用。

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

and then at the time of using it: 然后在使用它时:

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

But now you will end up with new errors as you don't have X and Y exposed as public . 但是现在由于没有将XY公开public您将面临新的错误。

On top of Habib's answer, your x and y properties require being set to public in order to be accessed: Habib's答案之上,您的xy属性需要设置为public才能访问:

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

Also, please check the case sensitivity of the properties you are attempting to access. 另外,请检查您尝试访问的属性的区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM