Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Global variable and Sub Main()

Global variable and Sub Main()

Scheduled Pinned Locked Moved Visual Basic
csharphelptutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    percyvimal
    wrote on last edited by
    #1

    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

    D C 2 Replies Last reply
    0
    • P percyvimal

      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

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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 Class

      percyvimal 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 a Sub 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 Module

      Or, 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 Module

      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
      <STAThread()> _
      Public Shared Sub Main()
      ' Same as above examples...
      End Sub
       
      ' the rest of your form code goes here...
      End Class

      Don'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

      P 1 Reply Last reply
      0
      • D Dave Kreskowiak

        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 Class

        percyvimal 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 a Sub 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 Module

        Or, 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 Module

        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
        <STAThread()> _
        Public Shared Sub Main()
        ' Same as above examples...
        End Sub
         
        ' the rest of your form code goes here...
        End Class

        Don'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

        P Offline
        P Offline
        percyvimal
        wrote on last edited by
        #3

        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

        D 1 Reply Last reply
        0
        • P percyvimal

          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

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          percyvimal 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 with Application.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

          1 Reply Last reply
          0
          • P percyvimal

            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

            C Offline
            C Offline
            cosma217
            wrote on last edited by
            #5

            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"

            P 1 Reply Last reply
            0
            • C cosma217

              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"

              P Offline
              P Offline
              percyvimal
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups