使用 Visual Basic 打开文件

发布于 2024-07-26 02:34:07 字数 813 浏览 5 评论 0原文

if 语句中的“读者”正在显示

表达式不是方法

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

The 'reader' within the if statement is showing

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

I moved the Dim and it worked.

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

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

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

发布评论

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

评论(2

只有影子陪我不离不弃 2024-08-02 02:34:07

在这一行:

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

On this line:

reader(OpenFileDialog2.FileName)

You're trying to call a constructor on an object that is already constructed. That's not possible, so the VB compiler is interpreting this as you trying to call the reader object as if it were a function.

Just don't declare the reader until you have the filename, so that you can pass the name to the constructor when you actually construct it, like so

    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
赏烟花じ飞满天 2024-08-02 02:34:07

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

You missed out the method name in reader(OpenFileDialog2.FileName).

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