a simple button click boolean

opie318

New member
Joined
Dec 15, 2008
Messages
3
Programming Experience
Beginner
My first attempt at a simple button click procedure.. never used programming language before, and it looks like a mess. But I need to know why every time debug I get all my varibles equaling eachother.

What I'm trying to do with this program is two things. The first is to successfully store a table of numbers in a dataset, in the format kind of like a social security number. Six separate memory boxes (##-##-##-##-##-##) Then be able to input numbers and manipulate the database numbers. (i.g see if the input numbers match any of the database numbers.) I don't know if that makes much sense, but that's the idea.

VB.NET:
Option Strict On
Option Explicit On



Public Class Form1

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim isempty As Boolean
        isempty = Txt1.Text = ""
        isempty = Txt2.Text = ""
        isempty = Txt3.Text = ""
        isempty = Txt4.Text = ""
        isempty = Txt5.Text = ""
        isempty = Txt6.Text = ""
        If isempty Then MsgBox("You can't leave them empty!!", MsgBoxStyle.OkOnly, "Press OK if you're a dumbass")
        If isempty Then Txt1.Clear()
        If isempty Then Txt2.Clear()
        If isempty Then Txt3.Clear()
        If isempty Then Txt4.Clear()
        If isempty Then Txt5.Clear()
        If isempty Then Txt6.Clear()


        Dim loto1 As Integer
        Dim loto2 As Integer
        Dim loto3 As Integer
        Dim loto4 As Integer
        Dim loto5 As Integer
        Dim loto6 As Integer


        Dim iswrongnumber As Boolean
        iswrongnumber = loto1 > 47
        iswrongnumber = loto2 > 47
        iswrongnumber = loto3 > 47
        iswrongnumber = loto4 > 47
        iswrongnumber = loto5 > 47
        iswrongnumber = loto6 > 47
        If iswrongnumber Then MsgBox("Your number must be between 1 and 47 *******", MsgBoxStyle.OkOnly, "Press OK if you're a dumbass")
        If iswrongnumber Then Txt1.Clear()
        If iswrongnumber Then Txt2.Clear()
        If iswrongnumber Then Txt3.Clear()
        If iswrongnumber Then Txt4.Clear()
        If iswrongnumber Then Txt5.Clear()
        If iswrongnumber Then Txt6.Clear()

        Dim isequa As Boolean
        isequa = loto1.Equals(loto2)
        isequa = loto1.Equals(loto3)
        isequa = loto1.Equals(loto4)
        isequa = loto1.Equals(loto5)
        isequa = loto1.Equals(loto6)

        isequa = loto2.Equals(loto1)
        isequa = loto2.Equals(loto3)
        isequa = loto2.Equals(loto4)
        isequa = loto2.Equals(loto5)
        isequa = loto2.Equals(loto6)


        isequa = loto3.Equals(loto1)
        isequa = loto3.Equals(loto2)
        isequa = loto3.Equals(loto4)
        isequa = loto3.Equals(loto5)
        isequa = loto3.Equals(loto6)


        isequa = loto4.Equals(loto1)
        isequa = loto4.Equals(loto2)
        isequa = loto4.Equals(loto3)
        isequa = loto4.Equals(loto5)
        isequa = loto4.Equals(loto6)


        isequa = loto5.Equals(loto2)
        isequa = loto5.Equals(loto3)
        isequa = loto5.Equals(loto4)
        isequa = loto5.Equals(loto1)
        isequa = loto5.Equals(loto6)


        isequa = loto6.Equals(loto2)
        isequa = loto6.Equals(loto3)
        isequa = loto6.Equals(loto4)
        isequa = loto6.Equals(loto5)
        isequa = loto6.Equals(loto1)

        If isequa Then MsgBox("Your numbers cannot be equal dick", MsgBoxStyle.OkOnly, "Press OK if you're a dumbass")
        If isequa Then Txt1.Clear()
        If isequa Then Txt2.Clear()
        If isequa Then Txt3.Clear()
        If isequa Then Txt4.Clear()
        If isequa Then Txt5.Clear()
        If isequa Then Txt6.Clear()
    End Sub
End Class
 
Last edited:
Well, let's look at this for a start:
VB.NET:
Dim isempty As Boolean
isempty = Txt1.Text = ""
isempty = Txt2.Text = ""
isempty = Txt3.Text = ""
isempty = Txt4.Text = ""
isempty = Txt5.Text = ""
isempty = Txt6.Text = ""
Now, let's say that Txt1 to Txt5 are all empty but Txt6 has some text in it. What will 'isempty' be after executing that code? It will be False, won't it? It's only going to remember the last value you assigned to it so you basically all you're doing is testing Txt6.

You're doing that same thing all over through that code. Look at your 'isequa' variable. You assign 30 different values to it but the first 29 are, of course, going to be overwritten and the only value you end up testing is the last one.

This is not a programming phenomenon. Let's say that you paint your house red, then you paint it blue, then you paint it green, then you paint it yellow. What colour's your house? It's yellow, isn't it? You can't see any of the other colours because they were painted over. Your variables are essentially the same, except you can't even strip the paint back and see the other colours underneath.
 
Ah I see, that makes sense thanks. I will have to find a way to assign one of the 2 varibles to all the TextBoxes at once..
 
Here one way of doing it:
VB.NET:
Dim OneIsEmpty As Boolean = (Txt1.Text.Trim = String.Empty OrElse Txt2.Text.Trim = String.Empty OrElse Txt3.Text.Trim = String.Empty)
It wont tell you which one is empty, but if one (or all) are empty then the boolean will be true
 
Thanks, that worked out those issues for me flawlessly. Would you happen to know the correct syntax to search for number matches in a database? (if I input 33-44-66-21-56-78 and would what a search inquiry for a total match, or say if I wanted to know if any matched the first two set of numbers. Txt1 and Txt2...)
 
Back
Top