私有化后生成 public - Visual Basic

Making public after being Private - Visual Basic

目前正在“自学”visual basic 对基础问题深表歉意

也许我做错了,但我想使用 btnTA_StartLog 创建一个文件并激活 Timer1 以在指定的时间间隔内记录数据。 outlog 的问题仅在 btnTA_startLog 中定义,而在其他 2 个子中没有定义。我如何才能使最近声明的 outlog public 可供其他潜艇访问?

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

不是 vb.net 人,所以我不知道确切的语法,但规则与我知道的 c# 相同。

在表单级别定义outlog,即在任何函数之外,这样所有函数都可以使用它。

它必须以某种方式处于 class 级别。


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

我用了私有字段,但它可能是私有的属性。它可能包含在您具有 class 级别引用的另一个对象中。