2016-07-01 3 views
0

csv 파일을 구문 분석하고 변수를 일련의 배열에 삽입하는 while 루프가 있습니다. 이것은 내 메인 양식의 버튼에 의해 호출됩니다.while 루프에 의해 생성 된 변수 저장

이 변수는 동일한 하위에있는 차트와 별도의 양식에있는 데이터 격자에 사용됩니다.

처음 버튼을 클릭하면 모든 것이 정상적으로 작동하지만 두 번째 클릭하면 별도의 양식에있는 DataGrid가 채워지지 않고 변수가 손실됩니다. '나는 while 루프에서 그들이 배열은 그러나 때문에, 변수가 공공 만드는 시도하고 건설 한

frmNumericChart.DataGridView1.Rows.Add(freq, dBu, dbnorm, ScaleFactor) 

, 내가 할 수있는 :

그래서 CSV 파일을 구문 분석 while 루프 내에서,이가 그것들을 공개적으로 접근 가능하게 만드는 것처럼 보입니다. (지면 관계 상 편집 됨)입니다

내 코드

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

    Dim sFile As String = strFileName 
    ' get the minimum and maximum frequencies 
    Dim Lines As Collections.Generic.IEnumerable(Of String) = File.ReadLines(strFileName) 
    Dim Line0 As String = Lines.FirstOrDefault 
    Dim LineN As String = Lines.LastOrDefault 
    Dim lowestFreq As String() = Line0.Split(New Char() {","c}) 
    Dim highestFreq As String() = LineN.Split(New Char() {","c}) 
    Dim lowFreq As String = lowestFreq(0) 
    Dim highFreq As String = highestFreq(0) 
    Dim refFrequencyX = Lines(18) 
    Dim refFreq As String() = refFrequencyX.Split(New Char() {","c}) 
    Dim ScaleFactor As String = refFreq(1) 

    Using sr As New StreamReader(sFile) 
     While Not sr.EndOfStream 
      Dim sLine As String = sr.ReadLine() 
      If Not String.IsNullOrWhiteSpace(sLine) Then 
       sData = sLine.Split(","c) 
       arrName.Add(sData(0).Trim()) 
       arrValue.Add(sData(1).Trim()) 
      End If 

      Dim freq As Decimal = sData(0) 
      Dim dBu As Decimal = sData(1) 
      Dim voltage As Decimal = sData(1) 
      ' dbnorm - normalise output to 0dBu ref 1kHz 
      Dim dbnorm = Math.Round(dBu - ScaleFactor, 4) 
      frmNumericChart.DataGridView1.Rows.Add(freq, dBu, dbnorm, ScaleFactor) 
      Chart1.Series(0).Points.AddXY(freq, dbnorm) 

     End While 
    End Using 

' (chart is constructed here) 

end sub 

어떻게 이러한 배열 변수는 글로벌 만들 수 있습니까?

내 허구의 코드를 용서해주십시오. 취미로 배우고 내가 배우는대로 배우고 있습니다.

+0

[양식의 다른 변수 및 객체 참조] (http://stackoverflow.com/q/33248704/1070452) – Plutonix

+0

전혀 도움이되지 않습니다. 해당 페이지에서 주어진 예제를 따르려고하면 나를 위해 오류가 발생합니다. – Tony

답변

0

공용 클래스를 모듈에 추가하여이를 해결했습니다.

'Public Class'안에는 코드 내의 아무 곳에서나 액세스하려는 잠수함에 대한 'Public Shared Sub'가 있습니다.

예 :

Public Module Module1 

<various public variables here> 

Public Class MyData 

     Public Shared Sub debugTempFile() 
      ' DEBUG 
      Dim mytempFolder As String = Path.GetTempPath() 
      Dim MyOutFile As String = mytempFolder + "outfile.txt" 
      Dim myfile As System.IO.StreamWriter 
      myfile = My.Computer.FileSystem.OpenTextFileWriter(MyOutFile, True) 
      myfile.WriteLine(debugdata) 
      myfile.Close() 
      ' END DEBUG 
     End Sub 
End Class 
End Module 

이 공공 하위 어디서나 호출 문을 사용하여 호출 할 수 있습니다 :

Call MyData.debugTempFile() 

나는이 자신과 같은 다른 초보자 사용의 희망.

관련 문제