简体   繁体   中英

Defining “Global Variables” in VB.NET / VS2008

I'm using "GetPrivateProfileString" to successfully read a .ini file. I'm finding myself having to re-suse various variables in multiple procedures such as Form Loading and other various on click events.

I'd like to simply my code so that I don't have to declare the same variables multiple times. I tried moving my code to a Class to get more global access without having to redefine the variables multiple times. A snippet of my code is below and any insight to how I can accomplish this, would be appreciated.

Dim MJBLoaderIni as String
MJBLoaderIni = tbBaseMMInstall.Text & "\MJBLoader\MJBLoader.ini"
Dim ConfigDisplayCustomerNumber As String
Dim DisplayCNumber As String
DisplayCNumber = Space(1)

ConfigDisplayCustomerNumber = ApplicationLog.GetPrivateProfileString("MJBLOADER", "DISPLAYCUSTOMERNUMBER", "$", DisplayCNumber, 255, MJBLoaderIni)

Try to use " Public " variable type and declare it in module level .. like this

You can makeit as Mymodule.vb

Public MJBLoaderIni as String

So your MJBLoaderIni will recognize in your all app in same project

Sample in your Form1 ..

Class Form1
  MJBLoaderIni = tbBaseMMInstall.Text & "\MJBLoader\MJBLoader.ini"
  Dim ConfigDisplayCustomerNumber As String
  Dim DisplayCNumber As String

  Private Sub Form_Load( .. ) handles Form.Load
    DisplayCNumber = Space(1)
    ConfigDisplayCustomerNumber = ApplicationLog.GetPrivateProfileString("MJBLOADER",   "DISPLAYCUSTOMERNUMBER", "$", DisplayCNumber, 255, MJBLoaderIni)
  End Sub
End Class

In your Form2 ..

Class Form2
  Private Sub Button1_Click( .. ) handles Button1.Click

    Msgbox(MJBLoaderIni)  '------> this will show your MJBLoaderIni

  End Sub
End Class

我创建了一个全局变量类,在其中创建了应用程序所需的全局变量,然后在窗体加载时填充所需的值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM