0

With the following code segment the Strings.Len() function is being skipped or not returning a value since the debugger says the variable Test1 is not assigned while the following line with Strings.InStr() function works properly:

   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
C C
  • 1
  • 4
  • 1
    Have you tried using `.Length` and `.Split` in place of len left and right? – Andrew Mortimer Dec 20 '18 at 19:00
  • 1
    Do you have a `Function Len(s As String) As [Not the Right Type]` somewhere? It would override the standard one. Or is `iTest1` *something* that actually doesn't like Integers? What is it? Could you post the Type of those variables/fields and how they're declared? – Jimi Dec 20 '18 at 19:09
  • Thanks Jimi, yes the Len function is overloaded (+15) and I used (s As String). iTest1 & ITest2 at dimensioned as Integer – C C Dec 20 '18 at 19:19
  • Thanks Andrew, .Length worked but I now see the issue is not the functions Len or .Length, but the variable names Test1, & sBaseTagItemExtension. Apparently as I watch the variables they are flagged as error BC30451: 'iTest1' is not declared. It may be inaccessible due to its protection level. When I changed iTest1 assignment to iTest2 = Len(sOPCTagItem) it worked. My issue is that all variables are DIM'd as either String or Integer, so I don't know what the issue is asking me to correct. – C C Dec 20 '18 at 19:41
  • 1
    @CC If you use [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) then Visual Studio can show you where there are problems with the types of variables. – Andrew Morton Dec 20 '18 at 20:17

2 Answers2

1

Both of these methods work for your scenario. They will depend on your input data and the consistency of underscore delimiters in your data.

You may just be missing 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
Andrew Mortimer
  • 2,380
  • 7
  • 31
  • 33
  • Thanks to all - the "Option Strict On" worked. Issue was not related to functions, but to DIM block where I was assigning values along with DIM assignments. When I fixed the 2 variables in the Watch list were now pre-assigned as "Nothing" – C C Dec 20 '18 at 20:38
0

It's weird that they return nothing since their return value is an integer. I would try using the proper String method.

    iTest1 = sOPCTagItem.Length
    iTest2 = sOPCTagItem.IndexOf("_")
    iTest1 = iTest1 - iTest2
    sBaseTagItemExtension = sOPCTagItem.Substring(sOPCTagItem.Length - sOPCTagItem.IndexOf("_"))
the_lotus
  • 12,668
  • 3
  • 36
  • 53