Dispose Module?

Peter Volz 1,295 Reputation points
2023-07-19T12:18:36+00:00

Hello,

I thought to make a module and collect all my disposers inside, all with the same name but different input data types (overload), something like:

(the inner logic is not important, asking about the general idea/algorithm)

Module ModuleShredder
    Friend Sub Disposer(ByVal MyCertificate As X509Certificate)
        On Error Resume Next
        If MyCertificate IsNot Nothing Then
            MyCertificate.Reset()
            MyCertificate = Nothing
        End If
    End Sub
    Friend Sub Disposer(ByVal MyCollection As X509CertificateCollection, Optional ByVal RenewCollection As Boolean = False)
        On Error Resume Next
        If MyCollection IsNot Nothing Then
            For Each MyCertificate As X509Certificate In MyCollection
                MyCertificate.Reset()
                MyCertificate = Nothing
            Next
            MyCollection.Clear()
            MyCollection = Nothing
        End If
        If RenewCollection = True Then MyCollection = New X509CertificateCollection
    End Sub
    Friend Sub Disposer(ByRef InputMessage As MailMessage)
        On Error Resume Next
        If InputMessage IsNot Nothing Then
            InputMessage.Reset()
            InputMessage.Dispose()
        End If
        InputMessage = Nothing
    End Sub
End Module

As you see all Subs have the same names overloading different data types.

Is this usage correct? I mean any problems might occur or logical usage errors?

Thanks for advise :)

  • oh and I guess I should pass everything as ByRef right? Since going to be fully disposed?
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,778 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 33,446 Reputation points Microsoft Vendor
    2023-07-20T01:47:57.0933333+00:00

    Hi @Peter Volz ,

    If only consider overloading part, your code is fine.

    It is also correct to pass parameters by reference.

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.