Hi there,
I am working on a class that checks if a file is available, if the file is available then we obtain the file lock using file stream and returns, I wish to impliment idisposable, when I do I am get the following error...
'Using' operand of type 'Test_Project_Is_File_Locked.clsFileFunctions.FileLockAttributes' must implement 'System.IDisposable'.
Class is here...
Thanks for looking :-)
BR
Phez
I am working on a class that checks if a file is available, if the file is available then we obtain the file lock using file stream and returns, I wish to impliment idisposable, when I do I am get the following error...
'Using' operand of type 'Test_Project_Is_File_Locked.clsFileFunctions.FileLockAttributes' must implement 'System.IDisposable'.
Code:
Private Sub CheckFile_Click(sender As System.Object, e As System.EventArgs) Handles CheckFile.Click
Dim Myfile As New clsFileFunctions.FileLockAttributes
Myfile.Path = "C:\tmp.xls"
Myfile.MaxRetryCount = 5
Myfile.RetryDelayMs = 1000
Dim myfilefunc As New clsFileFunctions
Using Myfile 'Error occurs here!!!!!!!!!!!!!!!!!!!!
End Using
Myfile = myfilefunc.FileIsLocked(Myfile)
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
End ClassCode:
Option Explicit On
Imports System.IO
Public Class clsFileFunctions
Implements System.IDisposable
'Global objects
Private disposedValue As Boolean = False
Private nameString As String
Private objectCountULong As ULong
Private objectArray() As clsFileFunctions
Structure FileLockAttributes
Dim Path As String
Dim FileStream As FileStream
Dim MaxRetryCount As Int16
Dim RetryDelayMs As Int32
Dim GotLock As Boolean
End Structure
Public Function FileIsLocked(ByVal GetFileLock As FileLockAttributes) As FileLockAttributes
Dim fileObj As New FileLockAttributes
fileObj = GetFileLock
FileIsLocked = GetFileLock
If fileObj.MaxRetryCount = 0 Then fileObj.MaxRetryCount = 1
If fileObj.RetryDelayMs = 0 Then fileObj.RetryDelayMs = 50
For i = 1 To fileObj.MaxRetryCount
Try
fileObj.FileStream = New System.IO.FileStream( _
GetFileLock.Path, _
System.IO.FileMode.Open, _
System.IO.FileAccess.ReadWrite, _
System.IO.FileShare.None)
fileObj.GotLock = True
Catch
fileObj.GotLock = False
End Try
If fileObj.GotLock = True Then Exit For
Threading.Thread.Sleep(fileObj.RetryDelayMs)
Next
Return fileObj
End Function
' This member keeps track of whether the object has been disposed.
Shadows disposed As Boolean = False
' The Dispose method releases the resources held by the object.
' It must be called by the client when the client no longer needs
' the object.
Overloads Sub Dispose() Implements IDisposable.Dispose
If Not disposed Then
DisposeManagedResources()
DisposeUnmanagedResources()
GC.SuppressFinalize(Me)
disposed = True
End If
End Sub
' The Finalize method releases nonmanaged resources in the case
' that the client neglected to call Dispose. Because of the
' SuppressFinalize call in the Dispose method, the Finalize method
' will be called only if the Dispose method is not called.
Protected Overrides Sub Finalize()
DisposeUnmanagedResources()
MyBase.Finalize()
End Sub
Private Sub DisposeManagedResources()
' Call subordinate objects' Dispose methods.
End Sub
Private Sub DisposeUnmanagedResources()
' Release unmanaged resources, such as database connections.
End Sub
Private Sub DoSomething()
' Call the EnsureNotDisposed method at the top of every method that
' needs to access the resources held by the object.
EnsureNotDisposed()
' ...
End Sub
Private Sub EnsureNotDisposed()
' Make sure that the object hasn't been disposed.
' Instead of throwing an exception, this method could be written
' to reacquire the resources that are needed by the object.
If disposed Then
Throw New ObjectDisposedException(Me.GetType().Name)
End If
End Sub
End ClassBR
Phez