通过两种方法引用 StreamReader
对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使其成为表单类上的一个字段。这会将范围从方法更改为整个表单。上一篇文章的警告仍然有效。
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.
为什么不直接存储从文件中读取的数据并重新使用它,而不是再次读取文件呢?我假设这些方法位于同一个类(以及对象的同一个实例)中,但如果情况并非如此,则有一些方法可以解决这个问题。
如果它不是对象的同一个实例,那么我会考虑使用单例配置对象(或缓存)并将数据存储在那里。当然,这实际上取决于数据的范围和生命周期——它适用于整个应用程序还是仅适用于这个实例?当然,最好的方法是使其成为如上所述的实例属性,我认为这会起作用,但如果您要重新创建该对象,则必须使用不同的技术。
如果您确实想再次读取该文件(因为数据来自不同的行),则需要重新使用该流,或者再次一次性读取所有数据(如果可能),然后迭代通过您在内部阅读过的内容。
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.
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.
忠告——不要尝试。
如果这样做,您将面临到处打开文件/流的危险。
我建议您在每个方法中打开一个新的 steam 阅读器(或将其抽象为自己的方法)。
注意:
您应该将流的打开包装在
using
语句中,以确保正确处理: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:实际上,在每次绘制运行期间从流中读取数据不应该是最好的主意。也许您想读取该值一次,将其存储在表单的成员变量中,然后在 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?
除非您期望文件在单击按钮和调用绘制方法之间发生更改,否则您不应该再次读取该文件。
与将结果存储在字段中并在绘制方法期间检索结果相比,读取文件在性能方面非常昂贵。反之亦然,具体取决于首先执行的内容。
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.