tags:

views:

66

answers:

3

This questions is for VBers, it's irrelevant in C#.

In VB, when you create a module, all it's fucntions and members are available in the scope without need to type the module name, just like all the VB functions (Rnd, Mid, IIf etc.).

I want to create a module but I should have to explicitly write it's name to access it's members, i.e. it shouldn't be loaded to the scope like a namespace.

Update

For example, I have a Module of extension methods, I don't want all it's members to show up on the scope and in the intellisense.

I want it to be available only by instance.ExtensionMethod().

Any ideas?

A: 

You could just add it to another namespace. I.e. if you want to call Foo.Bar and you have a module called FooModule, put it in a namespace called Foo.

Or... just have a regular class with a bunch of shared methods.

Eric Nicholson
A: 

Just add a namespace around the module:

Namespace MyModule

  Module MyModule

    Sub MyMethod()
    End Sub

  End Module

End Namespace

(Oh, and I'm not a VB:er... ;) )

Guffa
good idea but it will not work. cuz then you will have to write MyModule.MyModule
Shimmy
@Shimmy: It does work. You don't have to write both the namespace and the module. You can if you want to, just like you can use both String and System.String.
Guffa
aaight, i deleted my post, i guess the answer to my question is no cuz if you import the namespace the methods are in the scope, if not, you can't use ex. methods. just for the record: if you use <HideModuleName()> attribute, it doesn't change the picture.
Shimmy
@Shimmy: This is about extension methods? I don't know what you want to accomplish really, but perhaps you should ask about that instead of asking how you do it the way that you think that it should be solved.
Guffa
+2  A: 

If you create a Class instead of a Module then VB.NET will insist you use the class name. For example:

Public MustInherit Class Utils
  Public Shared Function Sqr(ByVal arg As Double) As Double
    Return arg * arg
  End Function
End Class
...
Dim result As Double = Utils.Sqr(42) 'Utils required

It is hardly necessary, but you can prevent anyone from inheriting this class by adding a private constructor.


Update

To avoid extension methods from polluting the global namespace in IntelliSense. I found a rather unexpected workaround for this:

Imports System.Runtime.CompilerServices
Imports System.ComponentModel

<EditorBrowsable(EditorBrowsableState.Never)> _
Module Extensions
  <Extension()> _
  Public Sub Method(ByVal obj As ExampleClass)
  End Sub
End Module
Hans Passant
@nobugz, You were write when I just need extenal functions, but the problem is that extension methods can only be contained in a Module.
Shimmy
@shimmy - hmm, you never mentioned extension methods before. I don't see the relevance, they are supposed to extend classes. You have to use the class object to call them. Like obj.Method(), not class.Method().
Hans Passant
I know but you extension methods must be wrapped in a Module, extension methods are tend to be called instance.Method, and I don't want them to be available in the scope and show up in the intellisense.
Shimmy