none
VB: How to implement Idisposable in a module - conflict with Form Dispose() RRS feed

  • Question

  • When writing the following 'Using' statement with a vendor object library (dll) referenced in the project, I receive an information message saying "Using operand of type 'atlVendorObject.atlVendor' must implement 'System.IDisposable'".

    How would I do this? If you need more information let me know :)

    Using objVendObj As New atlVendorObject.atlVendor Runtime.InteropServices.Marshal.ReleaseComObject(objVendObj) objDirector = Nothing
    objDirector.Dispose()

    End Using



    SV

    Saturday, September 2, 2017 12:05 AM

Answers

  • I understand what you mean by recast (e.g. CType(objVendor, Integer), but I'm not sure what I would recast to achieve the same purpose I'm attempting to do here.

    'Using / End Using' is simply a shortcut that avoids the need to repeat the variable name each time you reference it, and which ensures disposal of the object when the block completes.   In this case, you do not want to dispose of the object because it doesn't implement iDisposable.  Therefore, change your code so that you don't use 'Using / End Using'.  Instead, just  specify the variable name whenever you need to access a property or method of the object, and don't attempt to dispose it.  The code you posted doesn't refer to a property or method of  the Using operand in any case, so it is difficult to see why you have included it.

    I don't know what you are referring to in the comments about renaming.  If you are attempting to address a specific problem that you think is related to usage of this object, you should set out what that issue is. Indicate which variable you believe the problem is associated with - objDirector or objVendObj.

    Saturday, September 2, 2017 9:58 PM

All replies

  • When writing the following 'Using' statement with a vendor object library (dll) referenced in the project, I receive an information message saying "Using operand of type 'atlVendorObject.atlVendor' must implement 'System.IDisposable'".

    The message is telling you that the object is not disposable, and therefore cannot be used in a 'Using' block.  Re-cast that code to avoid Using.

    Saturday, September 2, 2017 12:45 AM
  • I understand what you mean by recast (e.g. CType(objVendor, Integer), but I'm not sure what I would recast to achieve the same purpose I'm attempting to do here.

    BACKGROUND:

    The Vendor object in the code here operates a portal opened in an IE session. This code is in a VB FORM exe that interacts with this object. If a user closes the IE session or object Portal prior to closing the form interacting with it, the FORM_FormClosed method would call to the PROCESS.GetProcessByName("VendorObject") to see if the VendorObject was running and release the object so the interacting VB FORM exe would not crash or allow the unreleased object to cause issues.

    SITUATION (why change it):

    Windows now renames this object with various abbreviated names even though Windows is configured to allow more than 8 characters in a process name (i.e. task manager image name). As a result,this object can't be properly identified and released by the VB FORM exe because it doesn't know to look for the new process names, resulting in crashes or unreleased objects.

    So, due to this issue, my thoughts were that the "Using" statement would be the best way to do this. And I haven't been to successful with code such as "If objVendor IsNot Nothing Then...."

    Any thoughts with those details either with recasting or other suggestion?


    SV


    • Edited by svMSDN Saturday, September 2, 2017 12:58 PM Clarification
    Saturday, September 2, 2017 12:38 PM
  • Dispose is a method from components to release all resources from that. To say it very simple, Components is the method which is used for the drag and drop from the visual studio toolbox.

    https://msdn.microsoft.com/en-us/library/3cc9y48w(v=vs.110).aspx

    Don't think that it is something which releases memory, that is done by the Garbage Collector which you must not influence.

    Be aware there are tons of messages written by non developers who thought that dispose was something as deconstructing. 

     


    Success
    Cor


    • Edited by Cor Ligthert Saturday, September 2, 2017 2:21 PM
    Saturday, September 2, 2017 1:35 PM
  • I understand what you mean by recast (e.g. CType(objVendor, Integer), but I'm not sure what I would recast to achieve the same purpose I'm attempting to do here.

    'Using / End Using' is simply a shortcut that avoids the need to repeat the variable name each time you reference it, and which ensures disposal of the object when the block completes.   In this case, you do not want to dispose of the object because it doesn't implement iDisposable.  Therefore, change your code so that you don't use 'Using / End Using'.  Instead, just  specify the variable name whenever you need to access a property or method of the object, and don't attempt to dispose it.  The code you posted doesn't refer to a property or method of  the Using operand in any case, so it is difficult to see why you have included it.

    I don't know what you are referring to in the comments about renaming.  If you are attempting to address a specific problem that you think is related to usage of this object, you should set out what that issue is. Indicate which variable you believe the problem is associated with - objDirector or objVendObj.

    Saturday, September 2, 2017 9:58 PM