0

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

2
  • Rename Program class to Zoowork Commented Mar 11, 2013 at 23:10
  • That error is because the startup object for the application is still set to original value. Please read this. Commented Mar 12, 2013 at 16:00

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

8 Comments

If I open up a new Console app and then copy your code into the file Module1.vb then it doesn't debug ....the message 'Sub Main' was not found in 'ConsoleApplication1.Module1'
I've set that to Sub Main but it still does not work - I will add a screenshot to OP so you can see the complete structure
@whytheq Did you change his code from ConsoleApplicationX to ConsoleApplication1? Also, what, if anything, is your root namespace in your project in VB set to? My preference is to clear the root namespace so that it works more like C# does.
@ChrisDunaway mine is called ConsoleApplication1 ...what is the safest way of changing the root namespace?
|
1

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.

1 Comment

+1 thanks Servy. Yep - I was missing Shared in my implementation which was one problem. Also I was copying p.s.w.g Main signature Main(args As String()) which was also causing a problem. In respect of creating an instance of the program from the program I have heard it is ok from some very experienced programmers and I've heard it's not good design from others .....so I guess its a matter of taste.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.