0

I have an object which creates a line in an sql table. using that object i can create and modify the record. I want to add a sub that would delete the record and than dispose the object. Something like this:

class myRecord
    int recordId
    sub new(con as sqlconnection,col1,col2,col3)
        sqlcommand ("insert into....")
        recordId=.....
    end sub
    sub modify(colname,newvalue)
       sqlcommand("update.... where...")
    end sub
    sub delete()
       sqlcommand(delete from....where recordId....)
      HERE I WANT TO TURN THE OBJECT into null or nothing
    end sub
end class

dim record as myrecord(con,"val1","val2","val3")
myrecord.delete()

can I destroy or turn object to null from within the object?

barak.o
  • 11
  • 2
  • Gut felling, directly within that method, you can't. But could make a shared method in your class called say DeleteRef which takes the instance of myRecord using the Me directive, then in there set it to Nothing. Probably better yet though, Implement IDisposable on your class – Hursey Aug 03 '22 at 20:43
  • If you want to dispose the object then you absolutely should be implementing `IDisposable`. You could then have the object call its own `Dispose` method from within that `Delete` method or you could document it such that the calling code should always call `Dispose` after calling `Delete`. There's no such thing as "turn object to null". An object is an object. You can set a variable to `null` so it no longer refers to that object but the object still exists, until the GC reclaims the memory it occupies. That would obviously have to happen outside the object, as it can't access the variable. – John Aug 04 '22 at 01:01
  • You could sort of do this with `IDisposable`, having the object `Dispose` itself, but I'm not sure that's the best choice---it might be surprising to a user for an object to become disposed without the "owner" of the reference disposing it. Maybe it would be more natural to make the object behave like a nullable where it may or may not be linked to a value. Then you could "clear" or "null" the object on delete (it might be a simple as just having a "Deleted" flag that is checked in routines and accessors). – Craig Aug 04 '22 at 14:12

0 Answers0