[Webinar] Learn how to a build a cloud-first strategyRegister Now


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

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

Question by:Navicerts
  • 3
  • 2
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 2000 total points
ID: 23658433
that would be:
Do While (WeightOne <> WeightTwo) or (WeightOne <> WeightThree)

Open in new window


Author Comment

ID: 23658441
That was easy :)  Thank You!
LVL 43

Expert Comment

ID: 23658447
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


Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.


Author Closing Comment

ID: 31547725
perfect, as always angelIII

Author Comment

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

Expert Comment

ID: 23658456

No problem, realised I was too slow anyway.


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month20 days, 11 hours left to enroll

864 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