【问题标题】:vb.net correct error implement System.IDisposablevb.net 纠正错误实现 System.IDisposable
【发布时间】:2020-06-12 19:30:47
【问题描述】:

我只是迷失了试图纠正这个错误
错误 BC36010“使用”“布尔”类型的操作数必须实现“System.IDisposable”

只有当我从下面的代码中删除 If End If 设计并实施 Using
时,才会出现错误 使用 Using End Using 的原因是垃圾清理
那么问题是如何实现 System.IDisposable ?

Public Sub haveFILE()
    'Dim path As String = "C:\Users\Me\source\repos\TestForms\TestForms\Resource\"
    Using Not My.Computer.FileSystem.FileExists(path & "Check.txt") Then
        tbHaveDB.Text = "File NOT Found"
        ' Create or overwrite the file.
        Dim fs As FileStream = File.Create(path & "Check.txt")
        fs.Close()
    End Using
End Sub

【问题讨论】:

    标签: vb.net try-catch idisposable


    【解决方案1】:

    错误是关于using

    Using Not My.Computer.FileSystem.FileExists(path & "Check.txt") Then
    End Using
    

    My.Computer.FileSystem.FileExists(path & "Check.txt") 返回布尔值。只是不要使用using。在这种情况下使用if thenUsing 是当你创建 IDisposable 对象时

    您的代码在不同的块之间看起来很混乱。你有一半 if 使用一半。我想,应该是这样的

    if Not My.Computer.FileSystem.FileExists(path & "Check.txt") Then
        tbHaveDB.Text = "File NOT Found"
        ' Create or overwrite the file.
    else
         using fs As FileStream = File.Create(path & "Check.txt")
             ' write to stream here
             fs.Close()
         End Using
    End If
    

    【讨论】:

    • 我如何知道何时使用 IDisposable 对象
    • @Vector 任何可以调用o.Dispose() 的对象都应该实现IDisposable。这是 .net 中的常见模式
    • 谢谢 需要阅读关于一次性的对象是这里的对象是文件系统还是其他的东西
    • @Vector 您可以使用File.WriteAllTextFile.WriteAllBytes 一次创建文件。大多数情况下不需要打开流
    • 我看到了WriteAllText,但没有使用它找到了这个资源sodocumentation.net/vb-net/topic/3204/disposable-objects我会让你回到ESPN在家工作?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多