Currently in the process of "self teaching" visual basic so apologies for the basic question
Maybe I am going about this the wrong way but I want to use btnTA_StartLog
to create a file and activate Timer1
to log data in a specified interval. Problem being outlog
is only defined in btnTA_startLog
and not in the other 2 subs. How can I make the recently declared outlog
public and accessible to the other subs?
Public Sub btnTA_StartLog_Click(sender As Object, e As EventArgs) Handles btnTA_StartLog.Click
Dim file As String = GetFileName(False, "csv", "Data Output")
Dim outlog As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(file, True)
Timer1.Start()
btnTA_StartLog.Enabled = False
btbTA_LoggingStop.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
outlog.WriteLine(CStr(DateTime.Now) & "," & CStr(txbTA_Delivered_ml.Text) & "," & CStr(MyTC.Data.Temperature))
End Sub
Private Sub btbTA_LoggingStop_Click(sender As Object, e As EventArgs) Handles btbTA_LoggingStop.Click
Timer1.Stop()
outlog.Close()
End Sub
CodePudding user response:
It has to be at the class level one way or another.
Private outlog As IO.StreamWriter
Public Sub btnTA_StartLog_Click(sender As Object, e As EventArgs) Handles btnTA_StartLog.Click
Dim file As String = GetFileName(False, "csv", "Data Output")
outlog = My.Computer.FileSystem.OpenTextFileWriter(file, True)
Timer1.Start()
btnTA_StartLog.Enabled = False
btbTA_LoggingStop.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
outlog.WriteLine(CStr(DateTime.Now) & "," & CStr(txbTA_Delivered_ml.Text) & "," & CStr(MyTC.Data.Temperature))
End Sub
Private Sub btbTA_LoggingStop_Click(sender As Object, e As EventArgs) Handles btbTA_LoggingStop.Click
Timer1.Stop()
outlog.Close()
End Sub
I used a private field, but it could be a private property. It could be contained in another object for which you have class level reference to.
CodePudding user response:
not a vb.net person so I dont know the exact syntax but the rules are the same for c# which I do know.
Define outlog at the form level, ie outside any functions, in that way all functions can use it.