【发布时间】: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背景吗?