VB.NET Do While x <> y <> z


I have some code that sends a print command to a scale and receives back a string (with a weight in it).  Because the program will be used for weighing live animals I want to continue to receive weights until I get 3 in a row that are the same (until the scale is stabilized).  To do this I made a Do While loop and compare the values.  My problem is that if the variables WeightOne = xx and WeightTwo = xx and WeightThree = xx (where "xx" is a number greater than 0) the loop does not stop!  However, if the variables are WeightOne = 0 WeightTwo = 0 and WeightThree = 0 it recognizes them as equal and exits the loop.

I do not know why it will not recognize any other values other than "0" as being equal?  If I output the variables to a msgbox("'"& WeightOne &"'") I get the following (and the loop keeps going)....

WeightOne = '25'
WeightTwo = '25'
WeightThree = '25'
Imports System
Imports System.Text
Imports System.Drawing
Imports System.IO.Ports
Imports System.Windows.Forms
Imports System.IO.Ports.SerialPort
Public Class Main
    Dim WeightValue As String
    Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SartoriusQC.DataReceived
        Dim TempWeight As String = SartoriusQC.ReadExisting
        If Len(TempWeight) > 2 Then
            WeightValue = ConvertWeight(TempWeight)
        End If
    End Sub
    Private Sub DataXferbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataXferbtn.Click
        Dim Counter As Integer = 0
        Dim WeightArray(30), WeightOne, WeightTwo, WeightThree As Integer
        WeightOne = 1
        WeightTwo = 2
        WeightThree = 3
        Do While WeightOne <> WeightTwo <> WeightThree
            SartoriusQC.Write(Chr(27) & "P")
            Call Delay(0.5)
            WeightArray(Counter) = WeightValue
            If Counter > 1 Then
                WeightOne = WeightArray(Counter)
                WeightTwo = WeightArray(Counter - 1)
                WeightThree = WeightArray(Counter - 2)
            End If
            MsgBox("'" & WeightOne & "'")
            MsgBox("'" & WeightTwo & "'")
            MsgBox("'" & WeightThree & "'")
            If Counter < 30 Then
                Counter = Counter + 1
                Counter = 0
            End If
        If Counter > 1 Then
            WeightOne = WeightArray(Counter)
            TextBox1.Text = WeightOne
            WeightTwo = WeightArray(Counter - 1)
            TextBox2.Text = WeightTwo
            WeightThree = WeightArray(Counter - 2)
            TextBox3.Text = WeightThree
        End If
    End Sub
    Private Sub Openbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Openbtn.Click
    End Sub
    Private Sub Closebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Closebtn.Click
    End Sub
    Private Sub ExitApp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitApp.Click
    End Sub
    Private Function ConvertWeight(ByVal WeightValue As String)
        Dim Position, Positiong As Integer
        Position = InStr(1, WeightValue, "+")
        Positiong = InStr(1, WeightValue, "g")
        Position = WeightValue.Length
        WeightValue = WeightValue.Substring(1, Positiong - 5)
        WeightValue = Trim(WeightValue)
        Return WeightValue
    End Function
    Sub Delay(ByVal dblSecs As Double)
        Const OneSec As Double = 1.0# / (1440.0# * 60.0#)
        Dim dblWaitTil As Date
        dblWaitTil = Now.AddSeconds(OneSec).AddSeconds(dblSecs)
        Do Until Now > dblWaitTil
    End Sub
End Class

Open in new window

Who is Participating?
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
that would be:
Do While (WeightOne <> WeightTwo) or (WeightOne <> WeightThree)

Open in new window

NavicertsAuthor Commented:
That was easy :)  Thank You!
TimCotteeHead of Software ServicesCommented:
Hello Navicerts,

Because it is not doing what you think. What you are getting is let's say assume the values you show:

If 25 <> (25 <> 25) then

Which evaluates as

If 25 <> (0) then ' as 25<> 25 is false so equivalent to zero which is then calculated as 25 <> 0 which is true so the loop continues.

What you mean is:

If Not (WeightOne = WeightTwo And WeightTwo = WeightThree) Then

Or the converse approach

If (WeightOne <> WeightTwo) Or (WeightTwo <> WeightThree) Then


The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

NavicertsAuthor Commented:
perfect, as always angelIII
NavicertsAuthor Commented:
Thanks TimCottee!  You will have to settle for a tanks as I already gave points but I appreciate the explanation :)
TimCotteeHead of Software ServicesCommented:

No problem, realised I was too slow anyway.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.