Problem with a function in VB.net component

Hi,
I am working to create a VB.net component that can take a text list of parameters and read values from that file. I want to then use those values to run the rest of my definition.
I can’t seem to get the function to work for some reason. I’ve gone over it with as minutely as I can and completely rewrote it thinking perhaps that a different structure would somehow eliminate the problem.
Is there something I’m overlooking or is there just a completely better way to do this(I have no doubt there is!)

Much thanks for any input…

Here is my code from the VB.net component and the necessary files

Private Sub RunScript(ByVal x As List(Of Object), ByRef A As Object, ByRef B As Object) 

    A = GetValue(21, x)


  End Sub 

  '<Custom additional code> 
  
  Function GetValue(lineNumber As String, paramList As List(Of Object)) As String
    
    Dim parameter As String = ""
       
    
    For Each i As String In paramList
         
      Dim line As String
      Dim number As String
      Dim commaLocation As Integer
      Dim equalsLocation As Integer
      
      ' put the line into a variable
      line = paramList(i)
      
      ' get the location of the comma and equals sign
      commaLocation = line.IndexOf(",")
      equalsLocation = line.IndexOf("=")
      
      ' put the line number into a variable
      number = line.Substring(0, commaLocation - 1)
      
      If lineNumber = number Then
    
        ' put the parameter into a variable
        parameter = line.Substring(equalsLocation + 1)
        
      End If    
      
    Next
    
    Return parameter 
    
  End Function
    
  '</Custom additional code> 
End Class

testcomponent.txt (64 Bytes)

Component Try 2.gh (19.3 KB)

If your inputs are strings, I would certainly advise that you set the String typehint on the x input.

i is declared as a String, but you’re using it here as an index integer.

You’re comparing strings here, you should ideally be comparing integers.

Since some of your lines do not contains equals symbols, you should probably check for that.

Try the attached. I’m sure there are still corner cases I do not handle properly, but I think it’s enough to get started. Component Try 2.gh (15.2 KB)

NOTE: this code is different from the code in the file, it has several fixes.

Function ParseValues(
  data As List(Of String),
  indices As List(Of Int32),
  names As List(Of String),
  values As List(Of String)) As Integer

  Dim counter As Int32 = 0
  For Each datum As String In data
    If (String.IsNullOrEmpty(datum)) Then
      Continue For
    End If

    Dim commaIndex As Int32 = datum.IndexOf(","c)
    Dim equalsIndex As Int32 = datum.IndexOf("="c)

    If (commaIndex < 0) Then Continue For

    Dim index As Int32
    Dim name As String
    Dim value As String

    If (Not Int32.TryParse(datum.Substring(0, commaIndex), index)) Then
      Continue For
    End If

    If (equalsIndex < 0) Then
      name = datum.Substring(commaIndex + 1)
      value = String.Empty
    Else
      name = datum.Substring(commaIndex + 1, equalsIndex - commaIndex - 1)
      value = datum.Substring(equalsIndex + 1)
    End If

    indices.Add(index)
    names.Add(name)
    values.Add(value)
    counter += 1
  Next

  Return counter
End Function

Ideally though you’d encapsulate the parsing and formatting into a custom made class. You’d create a new type which knows how to (de)serialize itself from and to the text lines, then it’ll be much easier to write code that directly deals with your files.

ParsingUsingClasses.gh (19.5 KB)

Function ParseData(lines As List(Of String)) As List(Of Datum)
  Dim entries As New List(Of Datum)
  For Each line As String In lines
    Dim entry As Datum = Datum.TryParse(line)
    If (entry IsNot Nothing) Then
      entries.Add(entry)
    End If
  Next
  Return entries
End Function

''' <summary>
''' Represents a single datum entry.
''' </summary>
Public Class Datum
  Private ReadOnly _index As Int32
  Private ReadOnly _name As String
  Private ReadOnly _value As String

  ''' <summary>
  ''' Create a new datum.
  ''' </summary>
  ''' <param name="index">Datum index.</param>
  ''' <param name="name">Datum name.</param>
  ''' <param name="value">Optional datum value.</param>
  Public Sub New(index As Int32, name As String, value As String)
    If (String.IsNullOrEmpty(name)) Then Throw New ArgumentNullException()
    If (value Is Nothing) Then value = String.Empty

    _index = index
    _name = name
    _value = value
  End Sub

  ''' <summary>
  ''' Optionally this method could throw exceptions if the line isn't
  ''' formatted correctly, or it could choose to return an error message.
  ''' </summary>
  ''' <param name="line">Line to parsr.</param>
  ''' <returns>Datum instance or null.</returns>
  Public Shared Function TryParse(line As String) As Datum
    If (String.IsNullOrEmpty(line)) Then Return Nothing

    Dim commaIndex As Int32 = line.IndexOf(","c)
    Dim equalsIndex As Int32 = line.IndexOf("="c)

    If (commaIndex < 0) Then Return Nothing

    Dim index As Int32
    Dim name As String
    Dim value As String

    If (Not Int32.TryParse(line.Substring(0, commaIndex), index)) Then
      Return Nothing
    End If

    If (equalsIndex < 0) Then
      name = line.Substring(commaIndex + 1)
      value = String.Empty
    Else
      name = line.Substring(commaIndex + 1, equalsIndex - commaIndex - 1)
      value = line.Substring(equalsIndex + 1)
    End If

    Return New Datum(index, name, value)
  End Function

  Public ReadOnly Property Index As Int32
    Get
      Return _index
    End Get
  End Property
  Public ReadOnly Property Name As String
    Get
      Return _name
    End Get
  End Property
  Public ReadOnly Property Value As String
    Get
      Return _value
    End Get
  End Property

  Public Overrides Function ToString() As String
    If (String.IsNullOrEmpty(Value)) Then
      Return String.Format("{0},{1}", Index, Name)
    Else
      Return String.Format("{0},{1}={2}", Index, Name, Value)
    End If
  End Function
End Class

Thank you very much for all the detailed info. I never had any formal programming training so this gives me a lot to digest. Thanks again!