Results 1 to 14 of 14

Thread: Can someone explain to me why this works?

  1. #1

    Thread Starter
    Banned
    Join Date
    Oct 2014
    Posts
    5

    Can someone explain to me why this works?

    Code:
            Label1.Visible = CheckBox1.Checked = True
    That will show Label1 only if CheckBox1.Checked = True. I don't understand why that is, though. The way I read is the Visible property of Label1 is equal to CheckBox1.Checked = True.

    It makes way more sense to me if it's written like this:
    Code:
            If CheckBox1.Checked = True Then
                Label1.Visible = True
            Else
                Label1.Visible = False
            End If

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Can someone explain to me why this works?

    What your code is saying is that -

    Label1.Visible is Equal to the evaluation of CheckBox1.Checked = True, which will always be true !!

    All you need is

    Label1.Visible = CheckBox1.Checked

    This way you are saying set Label1.Visible Equal to the Checkbox checked state which could be True or False.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Can someone explain to me why this works?

    Label1.Visible is a boolean property, either set to True or False.

    CheckBox1.Checked is a boolean property, either set to True or False.

    So, if you want .Visible to be True when .Checked is True, you just set the value of .Visible to the value of .Checked.

    You can create the expression, comparing a boolean to True, by using "= True", but that is redundant.
    The result of the expression will be the same as the value being compared to True.
    If .Checked is True, the expression is returning the boolean evaluation of True = True, which is True.
    If .Checked is False, the expression is returning the boolean evaluation of False = True, which is False since False is not equal to True.
    So, comparing a boolean value to True is not necessary since the result of the expression is the same as the boolean value itself.

    In your earlier thread, you asked the reverse case, where the button is visible (true) when the checkbox is "not" checked (.Check = False).
    Since you wanted to set .Visible to True when .Checked was False, you have to invert the result of the expression.
    So, you can invert the expression using Not.
    Label1.Visible = Not CheckBox1.Checked 'Not Checked (when False) returns True, Not Checked (when True) returns False

    You can also invert the boolean by using an expression that compares it to False instead of True.
    Label1.Visible = CheckBox1.Checked = False

    CheckBox1.Checked = False is evaluated first.

    If CheckBox1.Checked is False, then you're evaluating if False = False which is a True statement, so True is returned.
    If CheckBox1.Checked is True, then you're evaluating if True = False, which is not a True statement, so False is returned.

    So by comparing to False, if .Checked is False .Visible is set to True and if .Checked is True, .Visible is set to False.

    In VB (or BASIC in general), only the first "=" in a statement is an assignment operation. All other "="s in the line are comparison operations.
    If you are familiar with C, then that may have been a confusion factor since "=" is always an assignment, and "==" is used for comparison.
    In C,
    Label1.Visible = CheckBox1.Checked = True
    would be setting CheckBox1.Checked to True, and then setting Label1.Visible to True, so both values would always end up being True, and never False.
    BASIC doesn't work that way.
    Last edited by passel; Oct 7th, 2014 at 11:31 AM.

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Can someone explain to me why this works?

    Let's change CheckBox1.Checked into something else. Let's say you have an Integer named myNumber.
    Code:
    Dim myNumber As Integer
    Now this integer is set to a value somehow in your program, it might be read from a file or the user inputted a value that is stored in this variable, it doesn't matter. But now for some reason you want a Label to be visible if myNumber is equal to 3. You could of course do that in this manner:
    Code:
    If myNumber = 3 Then
      Label1.Visible = True
    Else
      Label1.Visible = False
    End If
    However the If statement by itself is either True or False, in other words myNumber is either equal to 3 or it's not. So you could actually do this:
    Code:
    Label1.Visible = (myNumber = 3)
    Now I put in the parenthesis for it to be easier to read, but you don't really need them. What this actually read like is: Set the visible property of Label1 to whatever value (myNumber = 3) is. Well (myNumber = 3) is either True or False. You see in VB you can't assign a value doing the above, you're not giving myNumber the value 3, you're checking if the value is 3. Assignments (changing the value of a variable) must be stated on it's on line.
    Code:
    myNumber = 3
    The above gives myNumber the value 3 instead of checking if the value is 3. But assignment is only valid for the first equal sign on a line. All other equal signs check the value. Which is also true for the first equal sign in an If statement.

  5. #5
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Can someone explain to me why this works?

    If you have trouble following it just use the if else method which probably be a lot easier to understand particularly if you are new to programming.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Can someone explain to me why this works?

    I'd be careful about such constructs... it works that way in VB, but other languages would execute that differently. It's not necessarily a construct I'd use (i'd use the format that NSA posted)... or in the case of what Joacim posted, I'd use the parens for sure. But it works that way in VB because VB uses the = for both equality and assignment... and it makes the assumption that the first is an assignment, and everything is a comparison after that. So if you do this:
    Code:
     Dim A as Integer
    Dim B as Integer
    Dim C as Integer
    
    A = B = C = 25
    And expect A, B, and C to all be 25... it won't work.
    Depending on settings, B & C will remain 0, A's value is likely to be -1. Actually, if you have you settings right in the IDE, you should get an implicit conversion error.
    It'll compare C to 25, which it isn't so it returns False, compares that to B, which will be a default value of 0, resulting in a True statement and assign that to A.

    End result, you won't get what you think you should be getting.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Can someone explain to me why this works?

    Quote Originally Posted by NeedSomeAnswers View Post
    Label1.Visible is Equal to the evaluation of CheckBox1.Checked = True, which will always be true !!
    ... sorry but HOW will that always be true??

    Basically you set visible to either true or false ... CheckBox1.Checked = True ... will equal true when checked ...
    therefore Label1.Visible=CheckBox1.Checked = True ... will be visible when checked...

    Also ... you can just ditch the =True bit anyway as this is implied as .Checked returns a Boolean of true or false.
    You can do this in an if statement too:
    If CheckBox1.Checked Then...

    Kris

  8. #8
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Can someone explain to me why this works?

    sorry but HOW will that always be true??
    Looking back at my post my explanation may have slightly unclear probably because i was trying to be brief.

    What i was trying to say was that basically CheckBox1.Checked = True is always true, so if you evaluate Label1.Visible = CheckBox1.Checked = True you are only ever setting the Label Visible state if the Checkbox is true, but you are doing nothing if the checkbox checked state is false!
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Can someone explain to me why this works?

    Quote Originally Posted by NeedSomeAnswers View Post
    Looking back at my post my explanation may have slightly unclear probably because i was trying to be brief.

    What i was trying to say was that basically CheckBox1.Checked = True is always true, so if you evaluate Label1.Visible = CheckBox1.Checked = True you are only ever setting the Label Visible state if the Checkbox is true, but you are doing nothing if the checkbox checked state is false!
    No, you're setting the Visible state regardless of the value of CheckBox1.Checked is. CheckBox1.Checked is either true or false so the Visible property is set to either true or false.

  10. #10
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Can someone explain to me why this works?

    No, you're setting the Visible state regardless of the value of CheckBox1.Checked is. CheckBox1.Checked is either true or false so the Visible property is set to either true or false.
    I am not sure i understand Joacim, if you read my posts again how is what you have said different to my answer?
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  11. #11
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Can someone explain to me why this works?

    Quote Originally Posted by NeedSomeAnswers View Post
    Label1.Visible is Equal to the evaluation of CheckBox1.Checked = True, which will always be true !!
    This is the line people are jumping on... it's innacurate...Specifically "CheckBox1.Checked = True, which will always be true !!" ... A Checkedbox checked value can be either TRue OR False... if it is unchecked, then it is False... making the line CheckBox1.Checked = True a false statement, resulting in a False value, which then gets assigned to the Visible property.

    The rest of your post, about simply setting the Visible value to the Checked value is fair and accurate... but it's the above assertion that the checked comparison will always be true that is the problem.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Can someone explain to me why this works?

    Quote Originally Posted by NeedSomeAnswers View Post
    I am not sure i understand Joacim, if you read my posts again how is what you have said different to my answer?
    What you said was:
    Quote Originally Posted by NeedSomeAnswers View Post
    What i was trying to say was that basically CheckBox1.Checked = True is always true
    No it's not! If CheckBox1.Checked is false then the statement CheckBox1.Checked = True (it's a statement and not an assignment) is false and the Visible state is set to False. If it always would be true then this If statement should also always be true, but it isn't.
    Code:
    If CheckBox1.Checked = True Then

  13. #13
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Can someone explain to me why this works?

    Argh Crap ... now i get it.

    I switch quite a lot between VB.Net projects and C# projects, and in C# my statement above is True, i didn't realise it was different in VB.Net.

    Well you learn something new everyday
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  14. #14
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Can someone explain to me why this works?

    That's because in C# = is an assignment operator, while the == is the comparison/equality operator. Which goes back to what I was saying earlier about certain constructs working differently in different languages.

    In C# the construct should have been
    Label1.Visible = Checkbox1.Checked == True

    but really the comparison against true is really jsut over kill (which we all agree about.)

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width