-1

I want convert a C# code to vb.net console. This is first time I find this two type code structure.

1.

Namespace ConsoleApp4
    Module Program
        Public Sub Main(ByVal args As String())
         test()
        End Sub

        sub test()
        end sub
    End Module
End Namespace

2.

Namespace ConsoleApp4
    Class Program
        Public Shared Sub Main(ByVal args As String())
         test()
        End Sub

        shared sub test()
        end sub
    End Class
End Namespace

what is the difference of this two type?

Rand Random
  • 7,300
  • 10
  • 40
  • 88
gsmgxm
  • 5
  • 2

2 Answers2

2

Sub Main must be shared to work as entry point of the application. It is automatically shared (or static) in modules. In classes the Shared keyword is required.


A VB module corresponds to a C# static class. Static classes and modules have only static members that can be used without having to create an object. In contrast, a non-static class must be instantiated to access its non-static (C#) or non-shared (VB) members

Module M
    Public Function F(ByVal x As integer) As Integer
        Return x * x
    End Function
End Module

Class C
    Public Function T(ByVal x As Integer) AS Integer
        Return x + 10
    End Function
End Class

With these declarations, you can write

Dim r1 As Integer = M.F(5) ' Or simply F(5)                           '

Dim o As C = New C() ' Must instantiate class, i.e., create an object.'
Dim r2 As Integer = o.T(32)

If you have variables (or properties) in a module, those exist exactly once. You can, however, create many objects from the same class and each object will contain another copy of these variables

Public Class Person
    Public Property FirstName As String
    Public Property LastName As String
End Class

Using this class declaration you can write

Dim list As New List(Of Person)()
list.Add( New Person With { .FirstName = "Terry", .LastName = "Adams"} )
list.Add( New Person With { .FirstName = "Lisa", .LastName = "Jones"} )

For Each p As Person In list
    Console.WriteLine($"Person = {p.FirstName} {p.LastName}")
Next

Now you have two Person objects in the list having different first and last names.

Classes belong to Object-oriented programming (OOP). I suggest you to read some introductions about it, as .NET is mainly based on OOP concepts.

See also:

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • 1
    Another difference between a VB Module and a C# static class is that VB allows you to access a Module member without the Module name qualification. In your example, you could use: Dim r1 As Integer = F(5), for example. – Dave Doknjas Jul 23 '19 at 15:17
  • Thank you.Object-oriented programming is not my question's key point. I just never see a class can with main procedure, and a class can be run. – gsmgxm Jul 24 '19 at 13:21
  • The key point is that `Sub Main` must be shared. It is automatically shared (or static) in modules. in classes the `Shared` keyword is required. – Olivier Jacot-Descombes Jul 24 '19 at 13:40
0

The difference between a Module and a Class is subtle; there is only ever one instance of a Module in all your program's life, and you cannot make multiple instances of it using the New keyword. By contrast you have to make an instance of a Class before you can use it..

In order to run, the .net framework runtime has to be able to find an available Main method without having to create an instance of a Class. This is achieved either by having the Main be kept inside a Module (and thus the Main is available because the module is available without having to instantiate a Class) or having it declared as Shared and be inside a Class (in which case you can conceive that a special thing happens that makes the shared Subs available without a class instance)

It's a hard difference to explain if you're not really well introduced to the concepts of OO programming and what "instances" actually means:

Class Person
  Public Name as String
  Public Sub New(n as String)
    Name = n
  End Sub
End Class

This declares a class of type person. Nothing about it refers to a particular person and it doesn't cause any Person object to exist in the computer memory until you use New:

Dim cj as New Person("Caius Jard")
Dim g as New Person("gxmgxm")

g.Name = "gsmgxm" 'correct a typo! this edits the name in the g object. it leaves cj's name alone

Now two instances of a Person object are in your computer memory, one named for me and one for you. You cannot do this with a Module. If we'd declared Person as Module there would only be one of them in all the entire program, accessed by the reference "Person":

Person.Name = "Caius Jard" 
Person.Name =  "gsmgxm" 'this overwrites my name. The program cannot remember more than one Person

and we couldn't have multiples. Consider that, at the time you launch your program, the runtime finds everything that is declared to be a Module and creates just one of them. This is somewhat vital for all sorts of advanced reasons that I won't get into, and Modules definitely do have their place in the grand scheme of things but thy aren't always incredibly useful in OO programming because more of the time we want more than one instance of a thing so we can model multiple things simultaneously.

So that's a precis on Class vs Module and why they are. In order to call any Sub or Function, you have to be able to call it on something. You have to have a DVD player before you can put a DVD in it and press Play - equally in programming, you have to have something that you can put your Main sub on, so you (or the .net runtime) can refer to it with Program.Main() and execute the instructions of the Sub.. That's how Subs and Functions work - theyre either associated with the special single instance (if it's a Module or a Shared Sub/Function of a Class), or they're associated with some object instance in the computer memory, and calling the Sub/Function acts on the object instance that was referred to:

Class Person
  Public Name as String
  Public Sub SetNameBlank()
    Name = ""
  End Sub
End Class

cj.SetNameBlank() 'the name of the cj object we declared before, is now blank
g.SetNameBlank() 

By specifying the object instance name cj then the Sub name, we establish context - the actions listed in SetNameBlank will be carried out against the cj object, not the g one. If we'd declared SetNameBlank as Shared then they would have taken place in the shared context, not related to either cj or g, and Shared SetNameBlank() could be invoked without cj or g even existing

Going back to the reason you're asking your question right now, what the difference between these two (in the context of a "Main" Sub), the answer is..

..is "not much" from the perspective of getting your app going. Either approach is fine. Your Main method will have to start kicking things off my making object instances of the other Classes you have in your program and getting them to do things. It probably wont make new instances of the class that your Main is in so right now it doesn't really matter whether you put your main in a module or a class - it achieves the same end result that there is a Sub the runtime can call into to start things moving, without needing to create an instance of a Class first

https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/program-structure/main-procedure

Caius Jard
  • 72,509
  • 5
  • 49
  • 80