4

During the design of a new application I was wondering if using a module with properties is considered to be a bad practice.

Some example code:

Module modSettings

   public property Setting1 as string

   public property DatabaseObject as IDatabaseObject

End Module

The code above is just an example to emphasize my question. In the past, this structure was used a lot in VB6. In the past I used it as well in my .NET projects.

But nowadays with buzzwords like Dependency Injection, Testability and Separation of Concerns the above structure smells bad. I can't really describe why, but it just feels wrong. I must admit I'm not very familiar with the keywords above, yet.

So I'm wondering whether the above code really is a bad practice. If so, what would you use a Module for?

4 Answers 4

9

Centro is right that a Module (or a NotInheritable Class with Shared members) is the closest equivalent to a C# static class. So technically, nothing is wrong with it as it's just one of VB's ways of creating this type of class. For example, you cannot say Public Shared Class Settings in VB as you cannot put the Shared keyword on a class.

On its own I wouldn't call it bad practice if a specific circumstance calls for a Module, but otherwise a Module (or other static class equivalents) likely is not the design choice you want for having loosely coupled, testable code. Additionally, while a NotInheritable Class with Shared members is more descriptive than just saying Module, there is at least one circumstance where a Module must be used instead.

When would you need to use Modules in VB.Net? If you want to take advantage of extension methods, then it's your only option since as mentioned, you cannot create a shared (static) class in VB.Net, neither can you use extensions on NotInheritable Classes. You must use a module as follows:

Imports System.Runtime.CompilerServices

Public Module StringExtensions
    <Extension()> _
    Public Function Remove( _
                        ByVal input As String, _
                        ByVal subStrings As String()) As String
        Return String.Join("", input.Split(subStrings, StringSplitOptions.None)).Trim()
    End Function
End Module

In C# you can't use modules and must use static classes as follows:

public static class StringExtensions
{
    public string Remove(this string input, string[] subStrings)
    {
        return string.Join("", input.Split(subStrings, StringSplitOptions.None)).Trim();
    }
}   
Sign up to request clarification or add additional context in comments.

3 Comments

"sealed static class" is not possible in C# since a static class in C# is sealed by default, so IMO "equivalent to a C# static class" sounds more precise.
Thanks for your clear explanation! So basically you'd only use Modules (in VB.NET) for extension methods?
@Rhapsody, unless there is something else out there that requires using modules (and I can't think of any), then yes, you are correct.
3

The keyword Module in VB.NET came from VB 6. Actually it is compiled as NotInheritable Class with making all members static, in C# it is as sealed class with static members.

I think it is a bad practice since it is not quite clear that it is actually a class with static members.

3 Comments

Unclear? Only until you read the documentation. There's no other way to create a static class in VB.Net.
@MarkJ Yes, there's no way to simply (without simulation via NotInheritable Class) create a static class in VB.NET. Anyway it would be better to have in VB.NET something like Shared Class than Module to make it clear it's a class, though static.
+1 as I agree that NotInheritable Class is preferable to Module for descriptiveness, except of course for situations where a Module is your only option as exemplified in my answer.
1

There is nothing wrong with properties. In fact, to implement binding you need properties and to implement the INotifyPropertyChanged interface. I think I would prefer classes over modules though, but I'm a C# guy mainly.

3 Comments

But you've got Modules as well in C# :-) My question is more about the usage of a Module for application-wide objects/properties.
In C# module is a shortcut to a sealed class with all static members. I would never use the Module key word in C# as that would be a hack way of doing things. In VB.Net, Module is a viable option though I would do this with class implementation.
There is no module keyword in C#, at least per the specification
1

Although it is possible to do this I must admit I have never seen it done in practise. In my opinion it is easier to read in a class as this declaration is explicit and using a class allows you to convert to a class later on if the need arises. So I can't think of any reason why you would want to do this over a class with a shared member

1 Comment

Because, like in the example, the DatabaseObject is now a single instance which can be used in any part of the application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.