vb.net main 不允许设置私有变量[关闭]
Posted
技术标签:
【中文标题】vb.net main 不允许设置私有变量[关闭]【英文标题】:vb.net main will not allow private variables to be set [closed] 【发布时间】:2012-09-24 23:30:41 【问题描述】:我有以下代码 - 我想在开始之前预设/初始化子程序中的一些变量。 (具体来说,我会读取一个控制文件并预加载一些变量,比如起始文件夹搜索路径)。我该怎么做?
公开课表1
<STAThread()> _
Shared Sub Main()
Dim mainWindow As Form1 = New Form1()
MessageBox.Show("Hello! I'm exectuing!")
' This next line generates an error.
' I don't know how to set this variable in Main.
' How do I set up variables, perhaps from a control file ?
openFileDialog1.InitialDirectory = "c:\mypath"
Application.Run(mainWindow)
End Sub
Private Sub FindButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles FindButton.Click
' Displays an OpenFileDialog so the user can select a Cursor.
Dim openFileDialog1 As New OpenFileDialog()
' Filter by All Files
openFileDialog1.Filter = "All Files|*.*"
openFileDialog1.Title = "Process a File"
If openFileDialog1.ShowDialog() = DialogResult.OK Then
If openFileDialog1.CheckPathExists Then
If openFileDialog1.CheckFileExists Then
' do stuff with the file here
Else
StatusLabel.Text = "Path does not exist"
End If
Else
StatusLabel.Text = "openFileDialog1.ShowDialog error"
End If
End Sub
结束类
【问题讨论】:
【参考方案1】:因为在 VB 中,“共享”类似于 C# 中的“静态”。而且您正试图对对象的实例进行静态引用。
即openFileDialog1.InitialDirectory = "c:\mypath"
正在尝试引用openFileDialog1 对象实例。将该行移到 Form1 的构造函数(或者可能是其他更合适的事件,oninit 或我不熟悉 winforms 的东西),它应该完成同样的事情。
MSDN VB Shared Sope
【讨论】:
以上是关于vb.net main 不允许设置私有变量[关闭]的主要内容,如果未能解决你的问题,请参考以下文章