使用单独的类中的get和set访问表单元素,无法正常工作 [英] Accessing form elements using get and set from a separate class, not working properly

查看:78
本文介绍了使用单独的类中的get和set访问表单元素,无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问与另一种形式相关的类中的文本框详细信息.方法displayDetails有效,但是如果我尝试使用btnSubmit_Click中的代码创建类似的方法并采取类似的方法,则不会显示任何内容.基本上,有没有一种方法可以在Student中创建一个公共方法来替换btnSubmit_Click中的代码-有人告诉我{get; Set;}是要走的路.下面的代码:

I''m trying to access textbox details inside a class that relate to another form. The method displayDetails works but if I try and create a similar method with the code that is in btnSubmit_Click and take a similar approach, nothing gets displayed. Basically, is there a way to create a public method in Students to replace the code in btnSubmit_Click - I had been told {get; Set;} was the way to go. Code below:

//Form1.cs - ManageStudentsWin
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ManageStudentsWin
{
    public partial class Form1 : Form
    {
        public int StudentID
        {
            get { return Convert.ToInt16(txtStudentID.Text); }
            set { txtStudentID.Text = value.ToString(); }
        }
        public string StudentFirstName
        {
            get { return txtStudentFirstName.Text; }
            set { txtStudentFirstName.Text = value; }
        }
        public string StudentLastName
        {
            get { return txtStudentSecondName.Text; }
            set { txtStudentSecondName.Text = value; }
        }
        public string StudentCourseCode
        {
            get { return txtCourseCode.Text; }
            set { txtCourseCode.Text = value; }
        }
        public int CurrentYear
        {
            get { return Convert.ToInt16(txtYear.Text); }
            set { txtYear.Text = value.ToString(); }
        }
        public Form1()
        {
            InitializeComponent();
        }


        Students s1 = new Students();
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            //Want to call a method instead of having to put code here
            s1.StudentID = StudentID;
            s1.StudentFirstName = StudentFirstName;
            s1.StudentLastName = StudentLastName;
            s1.StudentCourseCode = StudentCourseCode;
            s1.CurrentYear = CurrentYear;
            
        }

        private void btnDisplay_Click(object sender, EventArgs e)
        {
            s1.displayDetails(s1);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}


//Students.cs - ManageStudentsWin
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace ManageStudentsWin
{
    class Students
    {
        public string StudentFirstName;
        public int StudentID;
        public string StudentLastName;
        public string StudentCourseCode;
        public int CurrentYear;
        
       
        public void displayDetails(Students s1)
        {
            MessageBox.Show("Your ID is: " + StudentID + "\nYour First Name is: " + StudentFirstName + "\nYour Last Name is: " + StudentLastName + "\nYour Course Code is: " + StudentCourseCode + "\nYour year is: " + CurrentYear);
        }
    }
   
}

推荐答案

从UI与代码的其他部分(尤其是与UI的分离)的思想出发,开发这种东西要好得多.表格代表的数据.在最简单的情况下,您可能需要一个数据模型,该模型用于填充UI,并且UI可以用于更新数据.数据模型可以只是内存中的对象或对象图.数据应为主要数据(因为该模式更有可能在其他软件中使用,但是UI可以更改;最好是能够快速更改ID),因此表单应知道"数据,但数据对用户界面一无所知".

这样,您就不应该公开(甚至是内部)和表单控件-这总是很糟糕的.您可以(同样,在最简单的情况下)可以使用PopulateUpdate之类的表单方法.您将对数据对象(或对象图中的节点)的引用作为此类方法的参数传递给表单,但表单引用不会传递到任何地方.

—SA
It''s much better the develop such thing starting from the idea of separation of UI from other parts of the code, in particular, from the data a form represents. In the simplest case, you may need to have a data model which is used to populate the UI, and UI can be used to update the data; and the data model can be just an object or an object graph in memory. The data should be primary (because the schema is more likely to be used in other software, but the UI can change; and it''s the best to be able to change the ID quickly), so the form should "know" data, but the data should "know" nothing about the UI.

This way, you should not make public (or even internal) and form controls — this is always bad. You can have (again, in the simplest case), the form methods like Populate and Update. You pass a reference to data object (or a node in your object graph) to the form as the argument of such methods, but the form reference is not passed anywhere.

—SA


几件事:

1)
在这部分代码中:
Few things:

1)
In this part of code:
private void btnDisplay_Click(object sender, EventArgs e)
{
    s1.displayDetails(s1);
}


您正在为学生类调用displayDetails(s1)方法,但是在此过程中,您永远不会使用s1的属性(StudentId,StudentName等...).


2)
看看这部分代码:


you''re calling displayDetails(s1) method for Students class, but inside this procedure you''re never use the properties of s1 (StudentId, StudentName and so on...).


2)
Have a look at this part of code:

public partial class Form1 : Form
   {
       public int StudentID
       {
           get { return Convert.ToInt16(txtStudentID.Text); }
           set { txtStudentID.Text = value.ToString(); }
       }


而这个:


and this one:

class Students
    {
        public int StudentID;



如果要将StudentID用作类Students的属性,则StudentID的声明应位于Students类内部,而不是在Form1中.因此,将这些属性移到正确的模块中:



If you want to use StudentID as a property of class Students, the declaration of StudentID should be inside of Students class, not in the Form1. So, move these properties in to right module:

private int iStudentID; //first redeclared variables
public int StudentID(int value)
{
    get { return iStudentID); }
    set { iStudentID = value; }
}


然后:


and then:

Students s1 = new Students();
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            //Want to call a method instead of having to put code here
            s1.StudentID = Convert.ToInt16(txtStudentID.Text);
            s1.StudentFirstName = txtStudentFirstName.Text;
            s1.StudentLastName = txtStudentLastName.Text;
            s1.StudentCourseCode = txtStudentCourseCode.Text;
            s1.CurrentYear = txtCurrentYear.Text;
        }



3)
最后,displayDetails函数应如下所示:



3)
Finally, the displayDetails function should looks like:

public void displayDetails()
{
    MessageBox.Show("Your ID is: " + iStudentID.ToString() + "\nYour First Name is: " + sStudentFirstName + "\nYour Last Name is: " + sStudentLastName + "\nYour Course Code is: " + sStudentCourseCode + "\nYour year is: " + iCurrentYear.ToString());
}


其中sStudentLastName变量(和其他变量)是Student类的私有成员.


对不起,我的语言不好...
希望您能理解我的意思.


where sStudentLastName variable (and other) is a private member of Students class.


Sorry for my bad language...
I hope you''ll understand what i mean.


这篇关于使用单独的类中的get和set访问表单元素,无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆