通过两种方法引用 StreamReader

发布于 2024-10-25 12:59:37 字数 1084 浏览 1 评论 0原文

对 OO 很陌生,所以请友善。

我创建了一种方法,当单击 Button1 时,打开一个文件对话框并将内容读入流读取器 sr 中;

public void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            label1.Text = openFileDialog1.FileName;

            StreamReader sr = new StreamReader(label1.Text);
            String strNumVertices = sr.ReadLine();
            label2.Text = strNumVertices;
        }
    }

其他代码在 Form1_Paint 方法中运行。

public void Form1_Paint(object sender, PaintEventArgs e)

        perspectiveMatrix = new Gmatrix("perspective");
        translationMatrix = new Gmatrix("translation");
        scalingMatrix = new Gmatrix("scaling");

        perspectiveMatrix.initAsPerspectiveMatrix(300);

        scalingMatrix.initAsScalingMatrix(10, 10, 10);

        translationMatrix.initAsTranslationMatrix(150, 50, 1200);

       String strNumVertices = sr.ReadLine();
        label1.Text = strNumVertices;

我的问题是,如何从 Form1_paint 方法中的 button1_click 方法引用流读取器 sr?

Quite new to OO so please be kind.

I have created a method which when button1 is clicked, opens a file dialog and reads the contents into a stream reader sr;

public void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            label1.Text = openFileDialog1.FileName;

            StreamReader sr = new StreamReader(label1.Text);
            String strNumVertices = sr.ReadLine();
            label2.Text = strNumVertices;
        }
    }

The other code runs in the Form1_Paint method.

public void Form1_Paint(object sender, PaintEventArgs e)

        perspectiveMatrix = new Gmatrix("perspective");
        translationMatrix = new Gmatrix("translation");
        scalingMatrix = new Gmatrix("scaling");

        perspectiveMatrix.initAsPerspectiveMatrix(300);

        scalingMatrix.initAsScalingMatrix(10, 10, 10);

        translationMatrix.initAsTranslationMatrix(150, 50, 1200);

       String strNumVertices = sr.ReadLine();
        label1.Text = strNumVertices;

My question is, How do I reference stream reader sr from the button1_click method in the Form1_paint method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一袭白衣梦中忆 2024-11-01 12:59:38

使其成为表单类上的一个字段。这会将范围从方法更改为整个表单。上一篇文章的警告仍然有效。

StreamReader sr;
public void button1_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        label1.Text = openFileDialog1.FileName;

        sr = new StreamReader(label1.Text);
        String strNumVertices = sr.ReadLine();
        label2.Text = strNumVertices;
    }
}

public void Form1_Paint(object sender, PaintEventArgs e)

    perspectiveMatrix = new Gmatrix("perspective");
    translationMatrix = new Gmatrix("translation");
    scalingMatrix = new Gmatrix("scaling");

    perspectiveMatrix.initAsPerspectiveMatrix(300);

    scalingMatrix.initAsScalingMatrix(10, 10, 10);

    translationMatrix.initAsTranslationMatrix(150, 50, 1200);

    if (sr != null) {
       String strNumVertices = sr.ReadLine();
       label1.Text = strNumVertices;
    }

Make it a field on the form class. This changes the scope from the method to the entire form. The warnings from the previous post are still valid.

StreamReader sr;
public void button1_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        label1.Text = openFileDialog1.FileName;

        sr = new StreamReader(label1.Text);
        String strNumVertices = sr.ReadLine();
        label2.Text = strNumVertices;
    }
}

public void Form1_Paint(object sender, PaintEventArgs e)

    perspectiveMatrix = new Gmatrix("perspective");
    translationMatrix = new Gmatrix("translation");
    scalingMatrix = new Gmatrix("scaling");

    perspectiveMatrix.initAsPerspectiveMatrix(300);

    scalingMatrix.initAsScalingMatrix(10, 10, 10);

    translationMatrix.initAsTranslationMatrix(150, 50, 1200);

    if (sr != null) {
       String strNumVertices = sr.ReadLine();
       label1.Text = strNumVertices;
    }
雪落纷纷 2024-11-01 12:59:38

为什么不直接存储从文件中读取的数据并重新使用它,而不是再次读取文件呢?我假设这些方法位于同一个类(以及对象的同一个实例)中,但如果情况并非如此,则有一些方法可以解决这个问题。

private string StrNumVertices { get; set; }


public void button1_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        label1.Text = openFileDialog1.FileName;

        StreamReader sr = new StreamReader(label1.Text);
        this.StrNumVertices = sr.ReadLine();
        label2.Text = this.StrNumVertices;
    }
}

public void Form1_Paint(object sender, PaintEventArgs e)

    perspectiveMatrix = new Gmatrix("perspective");
    translationMatrix = new Gmatrix("translation");
    scalingMatrix = new Gmatrix("scaling");

    perspectiveMatrix.initAsPerspectiveMatrix(300);

    scalingMatrix.initAsScalingMatrix(10, 10, 10);

    translationMatrix.initAsTranslationMatrix(150, 50, 1200);

    label1.Text = this.StrNumVertices;

    ...
}

如果它不是对象的同一个实例,那么我会考虑使用单例配置对象(或缓存)并将数据存储在那里。当然,这实际上取决于数据的范围和生命周期——它适用于整个应用程序还是仅适用于这个实例?当然,最好的方法是使其成为如上所述的实例属性,我认为这会起作用,但如果您要重新创建该对象,则必须使用不同的技术。

如果您确实想再次读取该文件(因为数据来自不同的行),则需要重新使用该流,或者再次一次性读取所有数据(如果可能),然后迭代通过您在内部阅读过的内容。

Why not just store the data you've read from the file and re-use it rather than reading the file again? I'm assuming these methods are in the same class (and the same instance of the object), but there are ways around that if that's not true.

private string StrNumVertices { get; set; }


public void button1_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        label1.Text = openFileDialog1.FileName;

        StreamReader sr = new StreamReader(label1.Text);
        this.StrNumVertices = sr.ReadLine();
        label2.Text = this.StrNumVertices;
    }
}

public void Form1_Paint(object sender, PaintEventArgs e)

    perspectiveMatrix = new Gmatrix("perspective");
    translationMatrix = new Gmatrix("translation");
    scalingMatrix = new Gmatrix("scaling");

    perspectiveMatrix.initAsPerspectiveMatrix(300);

    scalingMatrix.initAsScalingMatrix(10, 10, 10);

    translationMatrix.initAsTranslationMatrix(150, 50, 1200);

    label1.Text = this.StrNumVertices;

    ...
}

If it's not the same instance of the object, then I'd consider using a Singleton configuration object (or cache) and storing the data there. It really depends, of course, on the scope and lifetime of the data -- does it apply to the whole application or just this instance? The best way, of course, is to make it an instance property like above, and I assume this will work, but if you're recreating the object you will have to use a different technique.

If you really do want to read the file again (because the data is from a different line), you will need to re-use the stream or, again, read all of the data in one go -- if possible -- then iterate through the items you've read internally.

〃温暖了心ぐ 2024-11-01 12:59:37

忠告——不要尝试。

如果这样做,您将面临到处打开文件/流的危险。

我建议您在每个方法中打开一个新的 steam 阅读器(或将其抽象为自己的方法)。

注意:

您应该将流的打开包装在 using 语句中,以确保正确处理:

using(StreamReader sr = new StreamReader(label1.Text))
{
   String strNumVertices = sr.ReadLine();
   label2.Text = strNumVertices;
}

Word of advice - don't try.

If you do so, you are in danger of having open files/streams all over the place.

I suggest you open a new steam reader in each method (or abstract it away into its own method).

Note:

You should be wrapping the opening of the stream in a using statement, to ensure proper disposal:

using(StreamReader sr = new StreamReader(label1.Text))
{
   String strNumVertices = sr.ReadLine();
   label2.Text = strNumVertices;
}
水波映月 2024-11-01 12:59:37

实际上,在每次绘制运行期间从流中读取数据不应该是最好的主意。也许您想读取该值一次,将其存储在表单的成员变量中,然后在 Paint 方法中访问?

Actually, it should be not the best idea to read from the stream during each paint run. Perhaps you'd like to read the value once, store it in the member variable of your form, and access in the paint method?

沧笙踏歌 2024-11-01 12:59:37

除非您期望文件在单击按钮和调用绘制方法之间发生更改,否则您不应该再次读取该文件。

与将结果存储在字段中并在绘制方法期间检索结果相比,读取文件在性能方面非常昂贵。反之亦然,具体取决于首先执行的内容。

Unless you expect the file to have changed between the button click and the paint method being called then you shouldn't be reading from the file again anyway.

Reading the file is very expensive in terms of performance compared to storing the result in a field and retreiving it during the paint method. Or visa versa depending what executes first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文