如何在VB.NET中的For循环中调用模块的多个subs?
我的代码是:
Private Sub BtnLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLog.Click
Dim LoggerString As String
For i = 1 To 5
LoggerString = "DataLoggerCh" & i & "()"
Call LoggerString.ToString()
Next
End Sub
SubsDataLoggerCh1到DataLoggerCh5是模块的一部分,如下所示:
Module ModuleLogger
Sub DataLoggerCh1()
myConnection.ConnectionString = connstring
myConnection.Open()
Dim InsertString As String
InsertString = "Insert into Table_Channel1 ([Log_Date],[Log_Time],[Test_Desc],[Current_1],[Current_2])values(?,?,?,?,?)"
Dim cmd As OleDbCommand = New OleDbCommand(InsertString, myConnection)
cmd.Parameters.Add(New OleDbParameter("Log_Date", CType(Today(), Date)))
cmd.Parameters.Add(New OleDbParameter("Log_Time", CType(TimeOfDay(), String)))
cmd.Parameters.Add(New OleDbParameter("Test_Desc", FormMain.Test_Desc_Ch1))
cmd.Parameters.Add(New OleDbParameter("Current_1", LoggerValCh1(0)))
cmd.Parameters.Add(New OleDbParameter("Current_2", LoggerValCh1(1)))
Try
cmd.ExecuteNonQuery()
cmd.Dispose()
myConnection.Close()
Catch ex As Exception
MsgBox(ex.Message)
myConnection.Close()
End Try
End Sub
End Module
但是当我点击按钮时,相应的潜艇不会被调用。
如何实现?。
答案 0 :(得分:2)
这是错误的方法(你需要反思,但要立即忘记)。而是只提供一个带参数的方法,例如:
Private Sub BtnLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLog.Click
For i = 1 To 5
SaveChannelLog(i)
Next
End Sub
这如何解决您的问题?它不是。你还需要重构其他东西。我建议实现一个封装所有nessesary信息的自定义类,如下所示:
Public Class LoggerInfo
Public Sub New(tableName As String, description As String, loggerValCh As List(Of Int32))
Me.New(tableName, description, DateTime.Now, loggerValCh)
End Sub
Public Sub New(tableName As String, description As String, logTime As DateTime, loggerValCh As List(Of Int32))
Me.TableName = tableName
Me.Description = description
Me.LoggerValCh = loggerValCh
Me.LogTime = DateTime.Now
End Sub
Public Property LoggerValCh As List(Of Int32)
Public Property TableName As String
Public Property LogTime As DateTime ' you don't need LogDate because you get it via LogTime.Date
Public Property Description As String
End Class
(类可以通过其他属性或方法扩展......)
现在,您可以将此类的实例存储在可由通道编号访问的集合中,例如Dictionary(Of Int32, LoggerInfo)
。示例初始化:
Public Property ChannelLoggers As New Dictionary(Of Int32, LoggerInfo) From {
{1, New LoggerInfo("Table_Channel1", "Channel 1 Logger", New List(Of Int32))},
{2, New LoggerInfo("Table_Channel2", "Channel 2 Logger", New List(Of Int32))}
}
您应该在代码中的某处设置LoggerValCh
- 值,您可以通过此属性访问它们。然后,您将拥有编写单一,清晰且可读的方法所需的一切:
Sub SaveChannelLog(channel As Int32)
Dim loggerInfo As LoggerInfo = Nothing
Dim validChannel = ChannelLoggers.TryGetValue(channel, loggerInfo)
If Not validChannel Then
'log this and return ...'
Return
End If
Dim sql = $"Insert into {loggerInfo.TableName}([Log_Date],[Log_Time],[Test_Desc],[Current_1],[Current_2])values(?,?,?,?,?)"
Using con As New OleDbConnection("your connection string here")
Using cmd As New OleDbCommand(sql, con)
cmd.Parameters.Add(New OleDbParameter("Log_Date", OleDbType.Date)).Value = loggerInfo.LogTime.Date
cmd.Parameters.Add(New OleDbParameter("Log_Time", OleDbType.Date)).Value = loggerInfo.LogTime
cmd.Parameters.Add(New OleDbParameter("Test_Desc", OleDbType.VarChar)).Value = loggerInfo.Description
cmd.Parameters.Add(New OleDbParameter("Current_1", OleDbType.Integer)).Value = loggerInfo.LoggerValCh(0)
cmd.Parameters.Add(New OleDbParameter("Current_2", OleDbType.Integer)).Value = loggerInfo.LoggerValCh(1)
con.Open()
Dim numInserted = cmd.ExecuteNonQuery()
End Using
End Using
End Sub
我已使用Using
- 语句进行连接,建议用于各种reasons。
答案 1 :(得分:1)
如果它们都具有相同的签名,您可以将它们写入一个委托数组并迭代它。
尝试这样的事情
private Delegate Sub DelegateDataLogger
private dataLogger as DelegateDataLogger() = {addressof DataLoggerCh1, addressof DataLoggerCh2, ...}
Private Sub BtnLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLog.Click
For i = 1 To dataLogger.length-1 step 1
dataLogger(i).Invoke()
Next
End Sub
答案 2 :(得分:1)
我会这样做:
Private Sub BtnLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles BtnLog.Click
Dim Logger() As Action = _
{ _
AddressOf DataLoggerCh1, AddressOf DataLoggerCh2, AddressOf DataLoggerCh3, _
AddressOf DataLoggerCh4, AddressOf DataLoggerCh5 _
}
For i = 1 To 5
Logger(i - 1).Invoke()
Next
End Sub
但是,再说一遍,只要调用每种方法都会更清洁。