Using OpenFileDialog and StreamReader

本文关键字:StreamReader and OpenFileDialog Using | 更新日期: 2023-09-27 18:26:02

有两个类,一个覆盖表单(类1),另一个覆盖在表单上显示的内容(类2)。我正试图从类2调用类1中的一个方法,以在文本框中显示某些信息。我一直收到错误:

非静态字段、方法或属性需要对象引用

我以前遇到过这个错误,并且能够克服,但到目前为止,我所尝试的一切都没有对这种情况有所帮助。我正在发布这两个类的代码。

第1类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
namespace Project6
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Stream myStream = null;
        //Create an instance of the open file dialog box
        OpenFileDialog ofd = new OpenFileDialog();
        //Set parameters, filter options, and filter index
        ofd.InitialDirectory = "c:''";
        ofd.Filter = "Text Files (.txt)|*.txt";
        ofd.FilterIndex = 2;
        ofd.Multiselect = false;
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((myStream = ofd.OpenFile()) != null)
                {
                    using (myStream = ofd.OpenFile())
                    {
                        StreamReader reader = new StreamReader(myStream);
                        string studentInformation = "";
                        string[] studentInformationArray = new string[11];
                        studentInformation = reader.ReadLine();
                        while ((studentInformation = reader.ReadLine()) != null)
                        {
                            studentInformationArray = studentInformation.Split(',');
                            Student newStudent = new Student(studentInformationArray);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk.  Original error: " + ex.Message);
            }
        }
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        textBox1.Text = Student.GetName(); //This generates the compiler error
        textBox1.Select(6, 5);
        MessageBox.Show(textBox1.SelectedText);
    }
}
}

第2类:

using System;
using System.Windows.Forms;
namespace Project6
{
class Student
{
    //Initialize variables
    private string[] studentInformationArray;
    //Constructor that accepts the studentInformationArray as an argument
    public Student(string[] studentInformationArray)
    {
        this.studentInformationArray = studentInformationArray;
    }
    public Student()
    {
        string className = studentInformationArray[1];
        string semester = studentInformationArray[2];
        string picture = studentInformationArray[3];
        int project1 = Convert.ToInt32(studentInformationArray[4]);
        int project2 = Convert.ToInt32(studentInformationArray[5]);
        int project3 = Convert.ToInt32(studentInformationArray[6]);
        int project4 = Convert.ToInt32(studentInformationArray[7]);
        int project5 = Convert.ToInt32(studentInformationArray[8]);
        int project6 = Convert.ToInt32(studentInformationArray[9]);
        int midtermExam = Convert.ToInt32(studentInformationArray[10]);
        int finalExam = Convert.ToInt32(studentInformationArray[11]);
    }
    public string GetName()
    {
        string studentName;
        studentName = studentInformationArray[0];
        return studentName;
    }
}

}

Using OpenFileDialog and StreamReader

这是因为OpenFileDialog不是这样使用的。看看MSDN托管的这个例子:

编辑:回答您编辑后的问题。在Student()构造函数中,您总是会得到一个NullReferenceException,因为您从未向studentInformation数组分配过某些内容。所以你可以这样修复:

public Student()
{
    studentInformationArray = new studentInformationArray[12];
    string className = studentInformationArray[1];
    // rest of your code...
}

然而,你应该考虑到studentInformation数组将是一个空数组,除非你给它赋值。由于你有一个接受字符串[]的Student()重载,我建议你要么删除默认构造函数,要么让它用默认信息调用重载的,比如:

public Student(string[] studentInformationArray)
{
     string className = studentInformationArray[1];
     // rest of your code...
     int project1 = int.Parse(studentInformationArray[4]); // always prefer int.Parse() instead of Convert.ToInt32()
     // rest of your code...
     this.studentInformationArray = studentInformationArray;
}
public Student()
{
    string[] defaultInformation = new string[12];
    // add information
    defaultInformation[0] = "some information";
    // ...
    new Student(defaultInformation);
}

需要考虑的更多要点:

  • 类级字段/属性应使用Pascal大小写拼写(每个单词的第一个字母大写,其余字母小写)
  • 尽量不要使用this.variableName=variableName
  • 变量名称不应包含变量的类型,这是多余的

因此,例如,更改以下内容:

private string[] studentInformationArray;

为此:

private string[] StudentInformation;

在回答最后一个关于编译器错误的问题时,您需要参考学生读物才能获得其名称。你可以这样做:

public partial class Form1 : Form
{
    private Student StudentRead;
    // rest of your code...
    // from your read method:
    StudentRead = new Student(studentInformationArray); 
    // and finally:
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        textBox1.Text = StudentRead.GetName();