We help IT Professionals succeed at work.

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

Medium Priority
Last Modified: 2012-05-06

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

Watch Question

Billing Engineer
Most Valuable Expert 2014
Top Expert 2009
that would be:
Do While (WeightOne <> WeightTwo) or (WeightOne <> WeightThree)

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


That was easy :)  Thank You!
TimCotteeHead of Software Services

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




perfect, as always angelIII


Thanks TimCottee!  You will have to settle for a tanks as I already gave points but I appreciate the explanation :)
TimCotteeHead of Software Services


No problem, realised I was too slow anyway.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.