Search code examples
vb.netfileiostreamreader

How to use a value from text file to do a calculation


I am trying to do a calculation using a value stored in my text file to reset my PLC counter. Below is what I have done so far.

 Dim PileValue_txt As String = "C:\test.txt"
 Dim Value As New System.IO.StreamReader(PileValue_txt)
 ValueTextbox1.Text =  Value.ReadToEnd
 Value.Close()

Convert.ToIntCInt32(Value)
LastValue = Value

  If 9999 < LastValue Then 
  CounterValue = 32000 * 1000 'Counter will reset

End if

But It threw an exception on (If 9999 < LastValue)

Error Message: Operator is not defined for type ‘integer’ and ‘system.io.streamreader’

What is wrong with my code?


Solution

  • You have declared Value as a StreamReader And the content are assigned to ValueTextBox1 using ReadLine() method.

    You have to use

    LastValue = CInt(ValueTextbox1.Text)
    

    Instead of

    Convert.ToIntCInt32(Value)
    LastValue = Value
    

    And you can read more about reading text content at this Stackoverflow documentation part