Global variable and Sub Main()
-
Hello I am new to .net. I need to know the following information. 1.How to declare global variables. 2.where to declare ( in VB6 used to declare those in Modules what about 2005?) 3.How to make the application Run the Sub Main() function?( I saw in VS2005 My Project Tool and it shows only forms which are in the projects and it didnt show the module which i have added which contains a main function and i have tried to add a class and tried the same and still no luck. 5.How to end the application Frmmdiform.END? Thanks for the reply in advance Regards Help in need is the help indeed
-
Hello I am new to .net. I need to know the following information. 1.How to declare global variables. 2.where to declare ( in VB6 used to declare those in Modules what about 2005?) 3.How to make the application Run the Sub Main() function?( I saw in VS2005 My Project Tool and it shows only forms which are in the projects and it didnt show the module which i have added which contains a main function and i have tried to add a class and tried the same and still no luck. 5.How to end the application Frmmdiform.END? Thanks for the reply in advance Regards Help in need is the help indeed
percyvimal wrote:
1.How to declare global variables.
Global variables don't exist in the .NET Framework. They're also bad practice. You can simulate something close by declaring your variables as Shared in another class:
Public Class HelpersLibrary
Public Shared myGlobalVariable As Integer
End Classpercyvimal wrote:
2.where to declare ( in VB6 used to declare those in Modules what about 2005?)
Modules are an outdated concept. They're only there for backwards compatibility with VB6.
percyvimal wrote:
3.How to make the application Run the Sub Main() function
You actually have to turn off the "Enable Application Framework" option in My Project before
Sub Main
will show up in the list. After that, all you have to do is supply aSub Main
in either a Module or a Class. For a Module, add a new one to your project, then put something like the following code in it:Module MainStartupCode
Public Sub Main()
' Your startup code begins here
' To launch a form and start your Windows GUI:
Application.Run(New mainForm)
End Sub
End ModuleOr, to support command line arguments:
Module MainStartupCode
Public Sub Main(ByVal cmdArgs() As String)
' Your startup code begins here
' To launch a form and start your Windows GUI:
Application.Run(New mainForm)
End Sub
End ModuleNow, you don't have to have a Module to supply a
Sub Main
. You can do it with a Shared member Sub of a class:Public Class Form1
<STAThread()> _
Public Shared Sub Main()
' Same as above examples...
End Sub
' the rest of your form code goes here...
End ClassDon't forget to go back into MyProject and select
Sub Main
< from the Startup object dropdown.percyvimal wrote:
What happened to 4?
percyvimal wrote:
5.How to end the application Frmmdiform.END
Me.Close()
Dave Kreskowiak Microsoft MVP - Visual Basic
-
percyvimal wrote:
1.How to declare global variables.
Global variables don't exist in the .NET Framework. They're also bad practice. You can simulate something close by declaring your variables as Shared in another class:
Public Class HelpersLibrary
Public Shared myGlobalVariable As Integer
End Classpercyvimal wrote:
2.where to declare ( in VB6 used to declare those in Modules what about 2005?)
Modules are an outdated concept. They're only there for backwards compatibility with VB6.
percyvimal wrote:
3.How to make the application Run the Sub Main() function
You actually have to turn off the "Enable Application Framework" option in My Project before
Sub Main
will show up in the list. After that, all you have to do is supply aSub Main
in either a Module or a Class. For a Module, add a new one to your project, then put something like the following code in it:Module MainStartupCode
Public Sub Main()
' Your startup code begins here
' To launch a form and start your Windows GUI:
Application.Run(New mainForm)
End Sub
End ModuleOr, to support command line arguments:
Module MainStartupCode
Public Sub Main(ByVal cmdArgs() As String)
' Your startup code begins here
' To launch a form and start your Windows GUI:
Application.Run(New mainForm)
End Sub
End ModuleNow, you don't have to have a Module to supply a
Sub Main
. You can do it with a Shared member Sub of a class:Public Class Form1
<STAThread()> _
Public Shared Sub Main()
' Same as above examples...
End Sub
' the rest of your form code goes here...
End ClassDon't forget to go back into MyProject and select
Sub Main
< from the Startup object dropdown.percyvimal wrote:
What happened to 4?
percyvimal wrote:
5.How to end the application Frmmdiform.END
Me.Close()
Dave Kreskowiak Microsoft MVP - Visual Basic
Thank you very much Mr Dave That is great explanation.I am still in the world of VB6 and so need to crawl to get into the world of .NET and people like you i hope will take me in the right direction. Sorry for the Numbering Error.
"You actually have to turn off the "Enable Application Framework" option in My Project "
This helped me a great deal for i was scratching my head why submain was not listing in the dropdown. Regarding No3:Now, you don't have to have a Module to supply a Sub Main. You can do it with a Shared member Sub of a class: Public Class Form1 _ Public Shared Sub Main() ' Same as above examples... End Sub ' the rest of your form code goes here...End Class
According to your example Form1 --should be the start up form?Could you able to explain how it works.I assume my Sub Main will be triggered first where whatever i wrote as code will be executed and then formLoad event? Me.Close()= ME.END in VB6 ie does it free up the application from memory? You have suggested that using global variable is not such a good practice and i have read in some article also that it is not a OOPS concept to have global variables. So what is the best and alternative way. Do i need to create a class with get and set parameter in a class to store that values? I would like you hear from you what is the best method to be followed to keep global variables? Thanks and Regards Help in need is the help indeed -
Thank you very much Mr Dave That is great explanation.I am still in the world of VB6 and so need to crawl to get into the world of .NET and people like you i hope will take me in the right direction. Sorry for the Numbering Error.
"You actually have to turn off the "Enable Application Framework" option in My Project "
This helped me a great deal for i was scratching my head why submain was not listing in the dropdown. Regarding No3:Now, you don't have to have a Module to supply a Sub Main. You can do it with a Shared member Sub of a class: Public Class Form1 _ Public Shared Sub Main() ' Same as above examples... End Sub ' the rest of your form code goes here...End Class
According to your example Form1 --should be the start up form?Could you able to explain how it works.I assume my Sub Main will be triggered first where whatever i wrote as code will be executed and then formLoad event? Me.Close()= ME.END in VB6 ie does it free up the application from memory? You have suggested that using global variable is not such a good practice and i have read in some article also that it is not a OOPS concept to have global variables. So what is the best and alternative way. Do i need to create a class with get and set parameter in a class to store that values? I would like you hear from you what is the best method to be followed to keep global variables? Thanks and Regards Help in need is the help indeedpercyvimal wrote:
According to your example Form1 --should be the start up form?
A Shared sub can be called without an instance of the class that contains it. Once the Sub Main is created inside ANY public class in your app, you can set it to the be Startup object in the MyProject properties.
Me.Close
will close the form that it shows in. If this is the only form in your app, then it will send the WM_CLOSE message to itself. Or, you could replace it withApplication.Exit
and it will close your entire application, no matter what form it's called in. And, yes, all the (managed) memory your app uses will be freed. Dave Kreskowiak Microsoft MVP - Visual Basic -
Hello I am new to .net. I need to know the following information. 1.How to declare global variables. 2.where to declare ( in VB6 used to declare those in Modules what about 2005?) 3.How to make the application Run the Sub Main() function?( I saw in VS2005 My Project Tool and it shows only forms which are in the projects and it didnt show the module which i have added which contains a main function and i have tried to add a class and tried the same and still no luck. 5.How to end the application Frmmdiform.END? Thanks for the reply in advance Regards Help in need is the help indeed
Another method. Create a class like below Public Class clsGlobal Public Shared Caterory As String 'Global variable for current programm-mode Audio or Video Public Const const_SqlExtAudio As String = "SELECT Media_Extensions.MediaExtension, =" + _ "Category.Category FROM Category INNER JOIN Media_Extensions ON Category.ID = " + _ "Media_Extensions.bez_Catgory WHERE (((Category.Category)='Audio'));" End Class to access the variable e.g. Caterory : clsGlobal.Caterory = "Test"
-
Another method. Create a class like below Public Class clsGlobal Public Shared Caterory As String 'Global variable for current programm-mode Audio or Video Public Const const_SqlExtAudio As String = "SELECT Media_Extensions.MediaExtension, =" + _ "Category.Category FROM Category INNER JOIN Media_Extensions ON Category.ID = " + _ "Media_Extensions.bez_Catgory WHERE (((Category.Category)='Audio'));" End Class to access the variable e.g. Caterory : clsGlobal.Caterory = "Test"
Thank you very much mr Dave and Cosma, with your help i am getting into .net now and really appreciate your nice explanations and very useful information. with regards Help in need is the help indeed