What do we mean by module? RRS feed

  • Soru

  • What do we mean by module in vb.net?

    I have been working on C# so you can tell me the equivalent meaning in C# 



    19 Ağustos 2020 Çarşamba 05:22

Tüm Yanıtlar

  • Hi Manoj,
    VB.NET Module mean a static class in C#.NET:

    static class MyModule { ... }

    in VB.NET

    Module MyModule ... End Module

    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    19 Ağustos 2020 Çarşamba 06:18
  • From Prepare Visual Basic for Conversion to C# :


    No Modules

    A Module statement in VB defines a reference type in which you declare events, variables, properties, and procedures to be called. There’s no equivalent of this in C#. The closest you could come to in C# is a class with all shared properties and methods. A module can’t be instantiated as an object and can't use inheritance. This makes modules basically worthless and should be avoided at all costs. To summarize; just use classes.


    and MSDN definition :

    Module Statement

    19 Ağustos 2020 Çarşamba 06:39
  • Modules are mainly for language extension methods although many VB.NET developers use them rather than classes because they usually don't want to learn classes.

    Example in C# for language extensions

    public static class GenericExtensions
        public static bool Between<T>(this IComparable<T> sender, T minimumValue, T maximumValue) => 
            sender.CompareTo(minimumValue) >= 0 && sender.CompareTo(maximumValue) <= 0;
        public static bool BetweenExclusive<T>(this IComparable<T> sender, T minimumValue, T maximumValue) => 
            sender.CompareTo(minimumValue) > 0 && sender.CompareTo(maximumValue) < 0;

    Then in VB.NET

    Public Module GenericExtensions
        Public Function Between(Of T)(sender As IComparable(Of T),
                                      minimumValue As T, maximumValue As T) As Boolean
            Return sender.CompareTo(minimumValue) >= 0 AndAlso
                   sender.CompareTo(maximumValue) <= 0
        End Function
        Public Function BetweenExclusive(Of T)(sender As IComparable(Of T),
                                               minimumValue As T, maximumValue As T) As Boolean
            Return sender.CompareTo(minimumValue) > 0 AndAlso
                   sender.CompareTo(maximumValue) < 0
        End Function
    End Module

    Then there is working with My namespace e.g. create a new custom namespace.

    In C# (which does not have My namespace)

    public static class Dialogs
        public static bool Question(string text)
            return (MessageBox.Show(
                MessageBoxDefaultButton.Button2) == DialogResult.Yes);

    To use the above point to the class via a using static e.g. someNamespace.Classes.Dialogs

    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        if (_hasChanges)
            if (!Question("There are unsaved changed, do you want to leave without saving?"))
                e.Cancel = true;

    While in VB.NET

    Namespace My
        Partial Friend Class _Dialogs
            ''' <summary>
            ''' Ask question with NO as the default button
            ''' </summary>
            ''' <param name="text"></param>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Function Question(text As String) As Boolean
                Return (MessageBox.Show(text, My.Application.Info.Title,
                                        MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                        MessageBoxDefaultButton.Button2) = MsgBoxResult.Yes)
            End Function
        End Class
        Friend Module Special_Dialogs
            Private instance As New ThreadSafeObjectProvider(Of _Dialogs)
            ReadOnly Property Dialogs() As _Dialogs
                    Return instance.GetInstance()
                End Get
            End Property
        End Module
    End Namespace


    Private Sub UpdateCheckButton_Click(sender As Object, e As EventArgs) _
        Handles UpdateCheckButton.Click
        If My.Dialogs.Question("Update current record?") Then
            Dim updatesTable = CType(_customersBindingSource.DataSource, DataTable).
            If updatesTable IsNot Nothing Then
            End If
        End If
    End Sub

    Other usages for modules would be to declare delegates and API declarations. But even delegates can be done in a class, no different than when a developer uses a module when a class is the better choice.

    namespace CancellationToken.Classes
        public class DelegatesSignatures
            public delegate void MonitorHandler(MonitorArgs args);


    Namespace Classes
        Public Class DelegatesModule
            Public Delegate Sub OnIterate(args As MonitorProgressArgs)
        End Class
    End NameSpace

    Example for API (we could separate the API from the procedure too)

    Imports System.Runtime.InteropServices
    Imports System.Windows.Forms
    Namespace Modules
        Public Module CueBannerTextCode
            <DllImport("user32.dll", CharSet:=CharSet.Auto)>
            Private Function SendMessage(hWnd As IntPtr, msg As Integer, wParam As Integer, <MarshalAs(UnmanagedType.LPWStr)> lParam As String) As Int32
            End Function
            Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (hWnd1 As IntPtr, hWnd2 As IntPtr, lpsz1 As String, lpsz2 As String) As IntPtr
            Private Const EM_SETCUEBANNER As Integer = &H1501
            Public Sub SetCueText(control As Control, text As String)
                If TypeOf control Is ComboBox Then
                    Dim editHWnd As IntPtr = FindWindowEx(control.Handle, IntPtr.Zero, "Edit", Nothing)
                    If Not editHWnd = IntPtr.Zero Then
                        SendMessage(editHWnd, EM_SETCUEBANNER, 0, text)
                    End If
                ElseIf TypeOf control Is TextBox Then
                    SendMessage(control.Handle, EM_SETCUEBANNER, 0, text)
                End If
            End Sub
        End Module
    End Namespace

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    19 Ağustos 2020 Çarşamba 11:41
  • A 'Module' in VB is just VB's way of declaring a C# static class (a class which only has static members and cannot be instantiated or inherited).

    This C# example:

    public static class MyModule
    	public static void Method()

    converts most naturally to the following VB:

    Public Module MyModule
    	Public Sub Method()
    	End Sub
    End Module

    However, if for some reason you think 'Module' is evil, you would have to do the following in VB:

    Public NotInheritable Class MyModule
    	Private Sub New()
    	End Sub
    	Public Shared Sub Method()
    	End Sub
    End Class
    This is rather awkward - why not use the VB version of a static class, i.e., a Module ?

    Neither of these VB versions is very object-oriented, but neither is the C# static class.

    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    19 Ağustos 2020 Çarşamba 13:25
  • Dave has already explained it very simple. But trying to tell it even simpler. 

    Every program needs a fixed starting point. 

    In C# that is the Static class Program and in VB the module Main (which can be replaced by a static class with whatever name). 

    (In some programs the module is hidden in the application framework in VB)

    Beside that you can misuse it in whatever way to mimic VB6 behaviour. 


    19 Ağustos 2020 Çarşamba 23:17
  • 19 Ağustos 2020 Çarşamba 23:56