我是C#/ Visual Studio的初学者,需要在这里向正确的方向推动。通过验证将文本字符串转换为属性0
我有一个主Window.xaml.cs文件,其中我有一个TextBox可以接收名字,还有两个按钮“Set”来更新一个名为Student的类,该类包含一个私有属性和一个“清除”按钮,它工作正常。
我无法弄清楚如何从文本框中将我的字符串放到我的Student类中,并且在添加更多属性之前需要获取此字符串。
任何帮助将受到感谢。
主窗口代码如下
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnSet_Click(object sender, RoutedEventArgs e)
{
}
private void btnClear_Click(object sender, RoutedEventArgs e)
{
txtFirstName.Clear();
}
我的学生类代码如下
namespace StudentRecordsSystem
{
public class Student
{
private string FirstName
{
get
{
throw new System.NotImplementedException();
}
set
{
// check that string is not empty
if (string.IsNullOrEmpty(value) == true)
throw new ArgumentOutOfRangeException("Please enter a first name");
}
}
嘿,你最好在这里发帖之前得到一些教程。这不是你如何实现一个类,这只是其中一个不正确的事情。我不想粗鲁,但先查看一下:https://www.google.com.br/search?q=C%23+tutorial – Tico
Tico是对的,但这没有帮助 - 我明白了。查找可见性(私有,公共),属性和字段变量以及对象生命周期。 – Kobor42
由于您使用的是WPF,我建议先以适当的WPF方式开始。看看我的答案。一旦你习惯了,它就容易多了。 – Bijan