在以下代码段中,由于调试器说在Strings.InStr()函数的以下行正常工作时未分配变量Test1,因此正在跳过Strings.Len()函数或不返回值:
sOPCTagItem = opcTagItem.Cells.Value --> assigns "DO1001_Inp_IOFault"
sBaseTagItemName = Left(sOPCTagItem, InStr(sOPCTagItem, "_") - 1) --> assigns "DO1001"
iTest1 = Len(sOPCTagItem) --> = nothing assigned
iTest2 = InStr(sOPCTagItem, "_") --> = 7
iTest1 = iTest1 - iTest2 --> = nothing assigned
sBaseTagItemExtension = Right(sOPCTagItem, Len(sOPCTagItem) - InStr (sOPCTagItem, "_")) --> = nothing assigned
答案 0 :(得分:1)
这两种方法均适用于您的方案。 它们将取决于您的输入数据以及数据中下划线定界符的一致性。
您可能只是想念Option Explicit On
Private Sub Button18_Click(sender As Object, e As EventArgs) Handles Button18.Click
Debug.Print(getBaseTagItem("DO1001_Inp_IOFault"))
Debug.Print(getBaseTagItemOldSchool("DO1001_Inp_IOFault"))
End Sub
Private Function getBaseTagItem(tagItem As String)
Dim vals() As String = tagItem.Split("_"c)
Return String.Concat(vals(1), "_", vals(2))
End Function
Private Function getBaseTagItemOldSchool(tagItem As String)
Dim sOPCTagItem As String = tagItem
Dim sBaseTagItemName As String = Strings.Left(sOPCTagItem, InStr(sOPCTagItem, "_") - 1)
Dim iTest1 As Integer = Len(sOPCTagItem)
Dim iTest2 As Integer = InStr(sOPCTagItem, "_")
iTest1 = iTest1 - iTest2
Dim sBaseTagItemExtension As String = Strings.Right(sOPCTagItem, Len(sOPCTagItem) - InStr(sOPCTagItem, "_"))
Return sBaseTagItemExtension
End Function
答案 1 :(得分:0)
奇怪的是,它们的返回值是整数,因此什么也不返回。我会尝试使用适当的String方法。
iTest1 = sOPCTagItem.Length
iTest2 = sOPCTagItem.IndexOf("_")
iTest1 = iTest1 - iTest2
sBaseTagItemExtension = sOPCTagItem.Substring(sOPCTagItem.Length - sOPCTagItem.IndexOf("_"))