Visual Basic,打开文件,我的代码出了什么问题?

时间:2009-06-26 15:39:51

标签: vb.net openfiledialog

if语句中的'reader'显示“Expression is not a method”,我做错了什么?

由于

 Dim reader As New CSVReader

        OpenFileDialog2.Filter = "CSV File (*.csv)|*.csv"
        OpenFileDialog2.RestoreDirectory = True
        If OpenFileDialog2.ShowDialog() = DialogResult.OK Then

            reader(OpenFileDialog2.FileName)
            reader.DisplayResults(DataGridView1)
            'Return OpenFileDialog2.FileName
        Else
        End If

我只是简单地移动了Dim并且它有效。

OpenFileDialog2.InitialDirectory = "a:"
OpenFileDialog2.Filter = "CSV File (*.csv)|*.csv"
OpenFileDialog2.RestoreDirectory = True
If OpenFileDialog2.ShowDialog() = DialogResult.OK Then
    Dim reader As New CSVReader(OpenFileDialog2.FileName)
    reader.DisplayResults(DataGridView1)
    'Return OpenFileDialog2.FileName
Else
End If

由于

2 个答案:

答案 0 :(得分:0)

您错过了reader(OpenFileDialog2.FileName)中的方法名称。

答案 1 :(得分:0)

在这一行:

reader(OpenFileDialog2.FileName)

您正在尝试在已构造的对象上调用构造函数。这是不可能的,所以当你试图将读者对象称为函数时,VB编译器会解释这个。

在你拥有文件名之前不要声明读者,以便在实际构建时可以将名称传递给构造函数,如此

    OpenFileDialog2.Filter = "CSV File (*.csv)|*.csv"
    OpenFileDialog2.RestoreDirectory = True
    If OpenFileDialog2.ShowDialog() = DialogResult.OK Then

        Dim reader As New CSVReader(OpenFileDialog2.FileName)
        reader.DisplayResults(DataGridView1)
        'Return OpenFileDialog2.FileName
    Else
    End If