What are Modules in VB.NET and what are its advantages? [duplicate] - vb.net

This question already has answers here:
Classes vs. Modules in VB.NET
(8 answers)
VB.NET What is the purpose of a class or module?
(7 answers)
Closed 9 years ago.
I have been new to Visual Basic. Why exactly do we use a module in VB.NET?
What would be a small example of a module and calling it one of the form?

A Module in VB.Net is essentially a grouping a methods and fields. These members can be accessed without qualification anywhere that the module itself is accessible. This mechanism is actually how many of the standard VB operators ChDir, ChDrive, CurDir are implemented. There is a module in the VB runtime named FileSystem which defines all of these operations
The main advantages / differences between Module and Class are the following
It's a declarative grouping of functions that aren't associated with instances of an object
It's the only way to define extension methods in VB.Net
No need for a redundant qualifier on every usage of a project helper method
No need to protect a module from accidental instantiation by the developer. It's by definition not creatable

Related

What are these things called / for? <something("string")> [duplicate]

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.

Class appears to be used without an instance. It is possible? VB.net [duplicate]

This question already has answers here:
Objects implicitly instantiated in vb.net?
(2 answers)
Why is there a default instance of every form in VB.Net but not in C#?
(2 answers)
Closed 7 years ago.
Short version: A class is being used without a variable of it's type being instantiated. How?
I have a huge legacy program which was converted from VB6 to VB.net.
It is compiling and many aspects work, but there is a problem related to an MDI (Multiple-Document Interface) display. I have placed other test forms under the parent MDI form, and they display correctly. The form in question does not display. (And, of course, it is the MOST important, and a most intricately complex form. I wish I could re-write it, but there is no possibility of that.)
There is a class, MDI1, which is used over 7,000 times in the code by many, many other classes. The MDI1 class is used extensively in the form which does not display. Wherever MDI1 is used, it is always referred to as simply MDI1 or Namespace.MDI1 . As far as I can tell, it is never instantiated as an object. It is as though it is a singleton, somehow, but I see nothing making it one.
The header for the class follows:
Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class MDI1
Inherits System.Windows.Forms.Form
Dim MDI_Activated As Boolean
Public Sub New()
MyBase.New()
InitializeComponent_Renamed()
End Sub
...
Can anyone tell me what may be going on here?
Every place where it is used that I've tried to check in Visual Studio by right clicking and selecting "go to definition" takes me right back to the class definition (line 4 of the above code), and never to a variable of type
MDI1. I have searched the entire source (using both Visual Studio and grepwin outside of Visual Studio) and can find no variable instantiated with type MDI1.
I don't understand how the calls to the class are working, without a variable of that type.
Help would be greatly appreciated.
I am using Visual Studio 2010 Professional, the latest version to which I have access.
I am assuming that you are not aware of Default Form Instance in VB.NET and hence the surprise. Its a common problem since this is missing in C#.
For winforms application in VB.NET, each form has a default instance and can be referred to simply by its form name (class name). You can ofcourse create your own instances like any other class and choose not to use the default instance.
This feature is particularly useful for legacy applications that have been migrated from VB6 to VB.NET since default form instance was a norm in VB6.
You can read more about Default Form Instances here:
https://msdn.microsoft.com/en-us/library/ms233839.aspx
And
https://msdn.microsoft.com/en-us/library/aa289529(v=vs.71).aspx

Is it possible to define a C function at runtime? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Dynamically creating functions in c
Here is an example of what I'd like to do:
void attribute((constructor)) someFunction() {
// Would be nice to define C function "someFunction2" somehow here.
}
I know class_addMethod allows adding C functions to Objective-C classes during runtime.
Is it possible to add C function to C main space?
Please, don't tell me I'm wrong if I'm thinking about this way of doing things - I am interested in it rather for educational purposes.
No. A C function consists of a name and a body. The compiler transforms the body to a binary piece of executable code that will be mapped to some address when a process is created from the executable. The name is used by the static and dynamic linkers as an alias to this address.
At runtime both concepts aren't really of much interest. The executable is loaded and names are resolved, so there's little use in creating them dynamically.
On iOS it would even be impossible to create new function implementations as the kernel disallows to make memory executable.

How to have 2 objective C class with same name in a same project? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the best way to solve an Objective-C namespace collision?
I was using 2 open source projects in an application with different use --- the issue was that both project had same class name with different implementations.
As per my understanding objective C don't have namespace option to handle scope --- as of now I am renaming the one of the class and its usage to make it work in my project.
Is there any alternative solution then renaming? I feel like objective C is missing namespace.
Renaming is the correct way. There is a reason why Apple recommends to prefix your classes with some uppercase letters. This should prevent exactly this situation. Same for method names in class extension, or "private" methods.

overloads in vb.net [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
“Overloads” keyword in VB.NET
what exactly overloads signifies in vb.net coz even if i am not writing the overloads in derived class function its working in the similar manner when i am writing it.
Function overloading allows more than one function to be defined with the same name in the same class, as long as the arguments are different. Not to be confused with function overriding where functions have the same number, type of arguments its the implementations in the derived classes that differs.
I believe that the overloads keyword in vb.net is completely optional, and is there only to support better readability of the code.
There is an all-or-nothing rule, however, so if you use it for one method, you have to use it for all overloading methods.