whytheq
whytheq

Reputation: 35597

Replicate "Program" structure in C# into VB.NET

Why does the structure between console application seem so different between C# and VB.NET?

In C# I often create an instance of the Program class:

namespace ConsoleApplicationX {

    class Program {

        static void Main(string[] args) {

            Program p;
            p = new Program();

        ...
        ...

In a VB.NET example I have Main located in a file called __ZooWork.vb like the following:

Module __ZooWork

    Sub Main()

        ...
        ...

Can I do an equivalent implementation to the C# code above in VB.NET?

EDIT

I've tried the following but it errors:

enter image description here

FURTHER EDIT

Just to confirm a couple of settings - these don't seem to have solved it!...

enter image description here

Upvotes: 0

Views: 509

Answers (2)

p.s.w.g
p.s.w.g

Reputation: 149050

Those are just the templates you can use when starting a new project. Of course you can write the equivalent code in both languages.

Namespace ConsoleApplicationX

    Class Program

        Shared Sub Main()

            Dim p As New Program()
            ...

        End Sub

    End Class

End Namespace

Upvotes: 4

Servy
Servy

Reputation: 203829

You probably shouldn't be creating an instance of the type your entry point is in to begin with. While it's certainly possible in either language, it's not good design in either.

Just have your Main method create an instance of some other type, not the type that Main itself is in, and use that for your program logic.

As for the error:

Program [...] does not contain a static 'Main' method suitable for an entry point

That's because your Main method isn't of the proper signature. It should be:

Shared Sub Main()

End Sub

The Shared is important (it's the equivalent of static in C#; it indicates that you do not need an instance of that type to call the method, which is important for the entry point as it won't have an instance of the type before the program is started.

Upvotes: 1

Related Questions