class 方法 "does not contain definition for and no extension method"
class method "does not contain definition for and no extension method"
我正在尝试学习 C# 教程,但我被困在 class 方法上,它说它找不到方法,即使它在那里。很明显我错过了什么,但是什么?
我认为这与在方法 'DistanceTo' 中找不到 class '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;
}
}
}
Class 文件:
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 和 drew。
我为此编辑了代码,现在它工作正常:
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;
}
}
}
查看错误(DistanceTo
未找到)并且 属性 X
和 Y
可用,我只能想象您正在使用 Point
structure 由框架提供,您原来的 class 没有在任何地方使用。
我建议您将 class 名称修改为不同的名称,或者指定一个名称空间并将其与该名称空间一起使用。
namespace Verita
{
public class Point
{
private int x, y;
然后在使用的时候:
Verita.Point firstPoint = new Verita.Point(0, 0);
但是现在您将遇到新的错误,因为您没有将 X
和 Y
公开为 public
。
除了 Habib's
答案之外,您的 x
和 y
属性需要设置为 public 才能访问:
public int x { get; set; }
public int y { get; set; }
此外,请检查您尝试访问的属性是否区分大小写。
我正在尝试学习 C# 教程,但我被困在 class 方法上,它说它找不到方法,即使它在那里。很明显我错过了什么,但是什么?
我认为这与在方法 'DistanceTo' 中找不到 class '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;
}
}
}
Class 文件:
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 和 drew。 我为此编辑了代码,现在它工作正常:
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;
}
}
}
查看错误(DistanceTo
未找到)并且 属性 X
和 Y
可用,我只能想象您正在使用 Point
structure 由框架提供,您原来的 class 没有在任何地方使用。
我建议您将 class 名称修改为不同的名称,或者指定一个名称空间并将其与该名称空间一起使用。
namespace Verita
{
public class Point
{
private int x, y;
然后在使用的时候:
Verita.Point firstPoint = new Verita.Point(0, 0);
但是现在您将遇到新的错误,因为您没有将 X
和 Y
公开为 public
。
除了 Habib's
答案之外,您的 x
和 y
属性需要设置为 public 才能访问:
public int x { get; set; }
public int y { get; set; }
此外,请检查您尝试访问的属性是否区分大小写。