As it says on msdn:
Both classes and modules are reference types that encapsulate the items defined within, but they differ in how items are accessed from other procedures.
How is it possible to use a Module inside a Class? How would I access its members and use them?
EDIT #1
I tried to access this module in all possible ways,
Dim memman as MemoryModule
but it gives me an error, Module 'MemoryModule' cannot be used as a type.
From your comment on the question...
Dim memman as MemoryModule
This is incorrect. Modules aren't classes, they can't be instantiated as objects. You can essentially think of a module as being a collection of Shared helper functions. And you'd access those like any other Shared function:
MemoryModule.SomeFunction()
So, for example, if your module looks like this:
Module MemoryModule
Sub PerformAnOperation()
' some function logic
End Sub
End Module
Then any class which can see that module can invoke that function:
MemoryModule.PerformAnOperation()
If logically your "memory module" should be an object capable of separate instances, then it shouldn't be a Module. Instead, you'd want to make it a Class and implement it with instance members instead of Shared members. It's important to structure your code according to the logic and concepts it represents.
Related
This question already has answers here:
VB.NET Brackets () {} [] <>
(4 answers)
Closed 3 years ago.
When I was writing a program that manipulated Active Directory, I found I needed to extend the GroupPrincipal class, and found some code that told me how to do this.
<DirectoryRdnPrefix("CN")>
<DirectoryObjectClass("group")>
Public Class GroupPrincipalEx
Inherits DirectoryServices.AccountManagement.GroupPrincipal
Public Sub New(context As PrincipalContext)
MyBase.New(context)
End Sub
Public Sub New(context As PrincipalContext, samAccountName As String)
...
What are the parts in angle-brackets called? What are they for? Where can I learn more about them.
I'm not asking about something specific to this case, my program works just fine. I just don't know what this language feature is, or does, or when to use it in future cases.
These are Attributes. Attributes can be evaluated by using Reflection.
Attributes
Attributes provide a powerful method of associating metadata, or
declarative information, with code (assemblies, types, methods,
properties, and so forth). After an attribute is associated with a
program entity, the attribute can be queried at run time by using a
technique called reflection.
Reflection
The classes in the System.Reflection namespace, together with
System.Type, enable you to obtain information about loaded assemblies
and the types defined within them, such as classes, interfaces, and
value types. You can also use reflection to create type instances at
run time, and to invoke and access them.
In Access VBA we can use Me.ControlName to refer to a control name. When reading other people's VBA code I have realized that some people do not use the me keyword and simply use ControlName to refer to a control name.
Is one more efficient than the other? For me Me.ControlName is more readable but I am not sure what other people's opinion is.
There are several cases when you need to use Me:
When there are conflicting names:
Dim someControl As Access.Control
Set someControl = Me.SomeControl
Obviously, this example is contrived, but there are real cases where conflicting names are logical
When you need to refer to the form object itself:
In an external module:
Public Sub DoStuffWithForm(someForm As Access.Form)
'Does stuff with form
End Sub
In the form module:
DoStuffWithForm Me
Me is just a general VBA concept. All class modules (and by extension, form modules) use the Me keyword to refer to themselves, but don't need to do that to refer to public properties. It's in a way similar to using Application.Something, you also don't need to do that, but it increases clarity when you do. And only in specific (rare) cases it's required.
In VB, Me is the internal reference to the public interface of a class.
Things that can be referenced by Me in a class:
Public properties, subs and functions
Controls in Access form and report classes
Things that cannot be referenced:
Private members of a class, including variables, properties, subs and functions
There are three uses of Me:
Provide a self-reference when calling functions or subs
Call the right method in case of duplicate names
In the IDE, typing Me. to more quickly select controls -- Thanks to Vlado for that suggestion!
Example:
If there is a Public Foo() defined in a standard module and a Public Foo() in a class, Me.Foo will always call the class method, and plain Foo will do the same.
However, if you delete or rename the class's Foo() method, Me.Foo will throw an error, but all plain Foo references will now call the global Foo() function instead!
As a programmer, I certainly don't want code that magically starts calling a different routine without warning if I edit a class module!
This is the best argument for always using Me to reference public members within class code.
I use Me.ControlName just to make sure when I type that the control is on the form and I do not misspell it. It will appear in intellisense so I could confirm it by pressing TAB.
I have a VB.net program that I received from someone else. I am trying to make modifications to it. The program consists of one main form and 6 classes (all .vb files).
In the main form, I want to call a sub-routine in one of the other modules. What is strange is that if I enter the name of the module followed by a ".", i.e.
QuoteMgr.
I don't see the names of the sub-routines in the module. I only see the Public Const's that are defined.
The sub-routine that I want to call is in a section labelled:
#Region "Methods"
What do I need to do to be able to call one of these methods?
The confusion was because of the wording you used in your original question before you edited it to say "class" instead of "module".
The two terms in VB.net mean entirely different things. A class typically must be insantiated as an object to invoke its methods.
So what you need to do is:
dim qt as new QuoteMgr
qt.Method("foo");
In this case you're creating an instance of QuoteMgr called qt and then invoking its methods. Alternatively you could modify the QuoteMgr class and set the method you're trying to call to "Shared" and then call it by simply going "QuoteMgr.Method" as you were trying before.
A module is more like a free-standing library of methods that can be called by anything in the same project (by default).
Currently in a Win Form application, im using a Global Variable Class which contains variables that are used to to share data. My question is, what other ways are there to achieve this? Best practises? and why?
Globals are bad for many reasons, but probably the most glaring reason is because, ideally speaking, every time you call the same method, passing it the same parameters, it should always do the same thing with the same results. Globals brake that rule. Suddenly your code starts behaving in unpredictable ways because something wasn't initialized properly somewhere else, or some global got modified incorrectly.
The best way I've found to get rid of the need for globals is to adhere to the principles of dependency injection (DI). There is much material on the topic online, but in a nutshell, rather than having classes reach out to create or find their dependencies on their own, they should simply request that the dependencies be provided to them, often in the constructor. Anything that you are accessing via global variables would, by definition, be considered dependencies of the classes that access them. Therefore, instead of, for instance, having a global settings object, like this:
Global settings As New Settings()
And then a class that uses it like this:
Public Class MyClass
Public Sub DoSomething()
If settings.SomethingEnabled Then
' ...
End If
End Sub
End Class
You would instead, do it like this:
Public Class MyClass
Public Sub New(settings As Settings)
_settings = settings
End Sub
Private _settings As Settings
Public Sub DoSomething()
If _settings.SomethingEnabled Then
' ...
End If
End Sub
End Class
This makes your code much cleaner, more flexible, and more reliable. It also makes the code far more testable too, which is a great added benefit.
Data should be shared according to how it is going to be used. If a variable is required across the entire application then it can be seen to have global scope and a global variable concept (e.g. public static shared) may well be appropriate.
Often this is not the case however as global variables should really be avoided (check out here and here for more reasoning)
Data should be encapsulated at the level it is required - for example if a form has data / variables within it that are applicable to it's function but where other forms need to now the value, this would be the ideal case for a public readonly property on the form, which would mask the actual detail of the variable from the rest of the aplication.
What advantage is there, if any, to using Modules rather than classes in VB? How do they differ, and what advantages/disadvantages are there in using modules? In VB or VB.NET, I use both.
(A) Modules
and
(B) Classes with only Shared functions
solve the same problem: Both allow you to logically group a set of functions.
Advantages of using a module:
It allows you to define extension methods.
For someone reading your code, it is immediately obvious that this is not a class representing a group of stateful objects but just a "function container".
Advantages of using a class with shared functions:
It's easy to extend it with instance (= non-shared) variables, functions and properties later on.
So, if you are writing a set of helper functions and want to logically group them (where the concept of a state of this group just doesn't make sense), use a module -- this is exactly what they are here for. On the other hand, if you have a function that conceptually fits to an already existing class, add it as a shared function to that class.
A major difference is that methods in modules can be called globally whereas methods in classes can't. So instead of ModuleName.MyMethod() you can just call MyMethod(). Whether that is an advantage or disadvantage depends on the circumstances.
Module are come earlier and now VB.NET just let it for backward compatibility. Modules and Class are nearly same. You can call Module.Function() directly as it treat as Shared Function in a class. Class you can define Shared Function/Method and additionally can create an instance like Dim c as Class = New Class().
Avoid use of Module, instead use Class. it is good for you to write a better OOP programming.