简体   繁体   中英

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. 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.

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.

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 .

On top of Habib's answer, your x and y properties require being set to public in order to be accessed:

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

Also, please check the case sensitivity of the properties you are attempting to access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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