Sub main not found

Harlequin

Member
Joined
Dec 20, 2008
Messages
6
Programming Experience
Beginner
I have VB 2008. I bought the book "Beginning VB 2008 Databases"
where the first excercise is as follows:
VB.NET:
Imports System
Imports System.Linq
Imports System.Collections.Generic
Imports System.Text

Namespace firstApp
    Class program
        Shared Sub main(ByVal args() As String)
            Console.WriteLine("Welcome")
            Console.ReadLine()

        End Sub
    End Class
End Namespace
When I run this I get an error that Sub main was not found in the module.
Any idea as to whats wrong?
Thanks
 
You've left out a fair bit of information there. You can't just "run that". You would have had to create a project, put that code somewhere, etc. My guess is that you created a Console Application project, added that class and deleted Sub Main from the existing module. Is that correct? If so then you need to go to the project properties and change the Startup Object, which will still point to Module1 that no longer contains a Main method.
 
The book reads as follows.
1. Open visual studio 2008
2. click file > new > project, and select visual basic language's Console Application template. I the name text box of the selected project template, type FirstApp and click OK.
3. Now replace the code of Module1.vb with the code below:

Imports System
Imports System.Linq
Imports System.Collections.Generic
Imports System.Text

Namespace firstApp
Class program
Shared Sub main(ByVal args() As String)
Console.WriteLine("Welcome")
Console.ReadLine()

End Sub
End Class
End Namespace

4. Run the applicationby pressing Ctrl + F5. Your results should appear in ..

I follow this and get the error code.

Any ideas?

And thanks for the reply before I'm rather hesitant about trying the next page until I can get this to work.

Harlequin
 
Many thanks for the suggestion.
I found that when I doubled clicked on the 'My Project' in the solution explorer and I changed the start up object from Module1 to either the "Sub Main" or the name of the project the script worked straight away. I have no idea why, but maybe I'll find out in chapter II.
Many thanks.
H
 
The reason is that you were telling the project to use Module1 as the entry point but there was no Main method in Module1. The Main method is always the entry point for an application so if you specify a module of class as the startup object it MUST contain a Main method. I'm afraid the author of your book didn't think that lesson through very well and didn't test their own instructions.
 
Back
Top