Question

What is the correct syntax in VB .net for checking that an object has been disposed of?

In my case, the public-declared database connection (dbLocal) is disposed of by a USING block and calling the database connection again triggers an error.

I've tried implementing the .IsDisposed code here but the declaration requires a Get, which i'm not entirely sure how to add.

Once I can check it .isdisposed, what is the correct method to recreate the public object?

DB Declaration:

Public dbLocal As New SQLiteConnection("Data Source=" & Replace(Application.StartupPath, "\", "\\") & "\\database.db;FailIfMissing=True")

USING loop:

Using dbLocal

'Create Command & add parameters
Dim Typecmd As New SQLiteCommand(TypeSQLI, dbLocal)
    With Typecmd.Parameters.Add("@id", DbType.String, 50, "id")
        Typecmd.Parameters.Add("@description", DbType.String, 100, "description")
        Typecmd.Parameters.Add("@sex", DbType.Int16, 1, "sex")
        Typecmd.Parameters.Add("@master", DbType.String, 50, "master")
        Typecmd.Parameters.Add("@size_min", DbType.String, 2, "size_min")
        Typecmd.Parameters.Add("@size_max", DbType.String, 2, "size_max")
        Typecmd.Parameters.Add("@size_half", DbType.Int16, 1, "size_half")
        Typecmd.Parameters.Add("@lining", DbType.String, 2, "lining")
    End With

Dim adapter As New SQLiteDataAdapter()
adapter.InsertCommand = Typecmd

Try
    Dim iRowsInserted As Int32 = adapter.Update(typetable)

    'Output result
    LstProcessed.Items.Add(iRowsInserted & " records added.")
    LstProcessed.TopIndex = LstProcessed.Items.Count - 1

    Catch ex As Exception
        MsgBox("Import error. " & Chr(13) & Chr(10) & "Check syntax of imported file (were there headers?).", MsgBoxStyle.Critical)
End Try

End Using

Ideally the 'IsDispose' function will check if the DB is closed before entering the USING loop.

I've added the IsDisposed declaration, as documented in the MSDN article, resulting in...

Public ReadOnly Property IsDisposed() As Boolean
    Get
        ???
    End Get
End Property
Was it helpful?

Solution 2

You shouldn't need to check for disposal of an object.

You should ideally wrap it in a Using block like this:

Using obj As New foo
    'use your object
End Using

Next time you use it wrap the code in another Using block.

I think your issue is not creating a New object at the start of your Using block. You probably want this:

Using dbLocal As New SQLiteConnection("Data Source=" & Replace(Application.StartupPath, "\", "\\") & "\\database.db;FailIfMissing=True")
    '...
End Using

Here the scope of dbLocal is only for the lifetime of the Using block. Currently you are using a global variable which is being disposed of at the End Using line.

OTHER TIPS

While the OP's problem seems to be their use of a using statement without understanding its implications, readers may want a clear answer to the question actually posed by the title.

The answer is, sadly, one can only tell via try/catch and unfortunately the exception you must catch is resource dependent. Personally I find this to be a failing of the IDisposable interface but what's done is done and as mentioned already, one can't know how long it would be true anyway.

Most disposed objects throw InvalidOperationException but this is only a convention. Unless you have documentation for the object in question you can only be sure by deliberately disposing an instance and trying it to see what is thrown.

In general, if one doesn't know whether an item is disposed, one doesn't know enough about its state to perform any operations that would depend upon its state. Classes which contain notification methods should write them in such a way that they may be safely invoked in any state (an object should simply ignore notifications when it's in a state where it doesn't care about them). In most cases where the pattern:

if (!thing.IsDisposed)
  thing.DoSomething();

would make sense, the pattern should be implemented within the class of thing itself [e.g. because BeginInvoke is often used to notify controls of a change to a property they might be monitoring, Control should (but doesn't) have a TryBeginInvoke method which would silently do nothing if a control isn't able to accept it]. Occasionally (as with Control.BeginInvoke one has to kludge around the absence of such a method, but generally the only time one wouldn't already know whether something had been disposed is if things can get disposed outside of one's control; in that situation, even if IsDisposed returns false, one may not know how long that would continue to be the case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top