How to write a common function / procedure? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to write a common function / procedure?

dicktsedicktse Posts: 1Member
I'm new to VB .NET. I wanna ask that how to write a common module that share common function / procedures among different solutions?

Comments

  • iwilld0itiwilld0it Posts: 1,134Member
    : I'm new to VB .NET. I wanna ask that how to write a common module that share common function / procedures among different solutions?
    :

    Solution 1: Create a .vb file and enclose procedures in a module block:

    Module Globals

    Public Sub SomeProc()
    ' Code
    End Sub

    Public Function CheckVal() As Boolean
    ' Code
    End Sub

    End Module

    Note that these procedures are global to the entire project. But to utilize maximum reusibility, you could encapsulate procedures in a Class block.

    Public Class SomeClass

    Public Sub SomeProc()
    ' Code
    End Sub

    Public Function CheckVal() As Boolean
    ' Code
    End Sub

    End Class

    Then you can use these procedures like so ...

    Dim obj As New SomeClass

    Call obj.SomeProc()
    Dim blnResult As Boolean = obj.CheckVal()

    You can simulate Modules with classes, by declaring class procedures as Shared.

    Public Class SomeClass

    Public Shared Sub SomeProc()
    ' Code
    End Sub

    Public Shared Function CheckVal() As Boolean
    ' Code
    End Sub

    End Class

    In this case you do not need to create an instance of the class as above. You can use the procedures anywhere in the project like so.

    Call SomeClass.SomeProc()
    Dim blnResult As Boolean = SomeClass.CheckVal()

    There is also another code block that can host procedures. Structures are similar to classes except classes are reference types and Structures are value types.

    Public Structure SomeStruct

    Public Shared Sub SomeProc()
    ' Code
    End Sub

    Public Shared Function CheckVal() As Boolean
    ' Code
    End Sub

    End Structure

    However, your best bet is to use Classes with shared methods so that you can share functions throughout the project and still support encapsulation and code reuse. Also classes are reference types meaning that if you pass a class object as an argument to a routine, you are only passing a 32 bit pointer. In the case of the structure a copy is created of the entire structure before passing into a routine. This is usually more overhead.

    Modules are good for routines that are specific to the project and will never be reused in any other project.

    Hope this gets u started.

Sign In or Register to comment.