Windows Develop Bookmark and Share   
 index > Windows Forms General > sub New() vs Sub Main()
 

sub New() vs Sub Main()

Hi,

I got

No accessible 'Main' method with an appropriate signature was found in '...'.

error when I added a sub main() to my vb.net win app.

I started building my forms without sub main(). It has this sub New()

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

cboSearchType.SelectedIndex = 0

LoadGrid_byCSR()

End Sub

Now I want to add a log in form. I followed a sample code that used a sub main()

Public Sub Main()

' Display the login dialog.

Dim dlg As New frmLogin

If dlg.ShowDialog() = DialogResult.OK Then

' The user correctly logged in.

' Display the main form.

Dim frm As New frmMain

frm.ShowDialog()

End If

End Sub

FrmLogin runs fine on its own, and I have set the project to start with sub main().

I also triedthis

Public shared sub Main()

That got me following error:

Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.

Can a sub New and sub main co-exist? What did I do wrong? Thanks!

hommer  Thursday, June 08, 2006 8:47 PM

Sub Main() is the entry point for your program. Sub New() is the constructor for the form. They can coexist because they aren't related. You just need to understand how they are used.

Put the Sub Main() code in a module and it should work just fine.

Module modMain

Public Sub Main()

' Display the login dialog.

Dim dlg As New frmLogin

If dlg.ShowDialog() = DialogResult.OK Then

' The user correctly logged in.

' Display the main form.

Dim frm As New frmMain

frm.ShowDialog()

End If

End Sub

End Module

Tony

tkerns  Friday, June 09, 2006 1:56 AM
Is this a console or a windows forms application?
liquidweaver  Thursday, June 08, 2006 9:26 PM
win form app.
hommer  Friday, June 09, 2006 12:32 AM

Sub Main() is the entry point for your program. Sub New() is the constructor for the form. They can coexist because they aren't related. You just need to understand how they are used.

Put the Sub Main() code in a module and it should work just fine.

Module modMain

Public Sub Main()

' Display the login dialog.

Dim dlg As New frmLogin

If dlg.ShowDialog() = DialogResult.OK Then

' The user correctly logged in.

' Display the main form.

Dim frm As New frmMain

frm.ShowDialog()

End If

End Sub

End Module

Tony

tkerns  Friday, June 09, 2006 1:56 AM

Tony,

This post is helpful. I have a similar problem in that I have a .NET 2 forms application that I want to be able to run from a command line (with arguments) that allows it to run without the form. (i.e it can run in two modes; interactive and automatic.)

I added the code to the form as above (below) but cannot see how to get the project to start in sub main - advice would be appreciated.

Module modMain

Public Sub Main()

' Display the login dialog.

Dim frm As New frmMain

frm.ShowDialog()

End Sub

End Module

Public Class frmMain

.............................

end class

Paul-I  Friday, January 12, 2007 8:55 AM

You can use google to search for other answers

Custom Search

More Threads

• Locking Width of form from resizing
• How to get selected text from AxWebBrowser
• Explain ChangeUICues Event
• About Box Show
• weird app behavior: No exceptions thrown!
• DatagridviewCell PointToScreen
• Is it possible to drag and drop email directly from outlook 2007 to richtextbox ?
• RichTextBox again
• Problem setting checkboxCell.Value = True on first row of a dataGridView
• posted this once and no replies... PLEASE HELP!