2012-10-04 2 views
0

다음 코드가 있습니다. - 시작하기 전에 서브 루틴에있는 일부 변수를 미리 설정/초기화하고 싶습니다. (특히, 제어 파일을 읽고 시작 폴더 검색 경로와 같은 일부 변수를 미리로드합니다.) 어떻게해야합니까?vb.net main은 개인 변수를 설정할 수 없습니다.

공공 Form1 클래스

<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 

최종 클래스

답변

2

VB '공유'C#에서 '정적'유사에 있기 때문에. 그리고 여러분은 객체의 인스턴스에 대한 정적 참조를 만들려고합니다.

즉, openFileDialog1.InitialDirectory = "c:\mypath"은 openFileDialog1 개체 인스턴스를 참조하려고합니다. 그 줄을 Form1의 생성자 (또는 아마도 다른 적절한 이벤트, oninit 또는 winforms에 익숙하지 않은 무언가)로 이동하면 동일한 작업을 수행해야합니다.

MSDN VB Shared Sope

관련 문제