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

Posted on 2009-02-17
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

Question by:Navicerts
    LVL 142

    Accepted Solution

    that would be:
    Do While (WeightOne <> WeightTwo) or (WeightOne <> WeightThree)

    Open in new window

    LVL 7

    Author Comment

    That was easy :)  Thank You!
    LVL 44

    Expert Comment

    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


    LVL 7

    Author Closing Comment

    perfect, as always angelIII
    LVL 7

    Author Comment

    Thanks TimCottee!  You will have to settle for a tanks as I already gave points but I appreciate the explanation :)
    LVL 44

    Expert Comment


    No problem, realised I was too slow anyway.


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now