Question How the XOR works

Crevitz

New member
Joined
Sep 28, 2015
Messages
2
Programming Experience
Beginner
I have this code applied into a checkbox:

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
Label.Font = New Font("Microsoft Sans Serif", 16, Label.Font.Style Xor FontStyle.Strikeout)
End Sub

The result was the label text style become strikeout. The question is how is it possible that the result is strikeout while using Xor as the logical connective, and what is Label.Font.Style means in this case? It might be silly of me but I started learning VB.Net 5 days ago, so I'm glad if someone would enlighten me. Thank you.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
This is one of the more advanced topics of programming you've stumbled on. In short what's going on is it's setting the Font.Style(s) and what's happening is the FontStyle.Strikeout is being "added" to the current Font.Style(s) while keeping the other style(s).
The more technical explanation is that this is what's known as bit operations, the Font.Style property is an integer that holds a value where you can mathematically hold several values and you use bit operations to add or subtract those values from the integer. In this case you're using the Xor to toggle the addition (and subtraction)of the Strikeout based on the changing of the Checkbox's Checked property. Personally I wouldn't use an Xor to just toggle it, I would use an If Else to explicitly set or remove the Strikeout flag from the font:
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    If DirectCast(sender, CheckBox).Checked Then
        'Add the Strikeout
        Label.Font = New Font("Microsoft Sans Serif", 16, Label.Font.Style Or FontStyle.Strikeout)
    Else
        'Remove the Strikeout
        Label.Font = New Font("Microsoft Sans Serif", 16, Label.Font.Style And Not FontStyle.Strikeout)
    End If
End Sub
For more information on the Xor operator: Xor Operator (Visual Basic)
And the Xor gate non-programming specific: XOR gate - Wikipedia, the free encyclopedia
 

Crevitz

New member
Joined
Sep 28, 2015
Messages
2
Programming Experience
Beginner
Thank you really much JuggaloBrotha, I learned a lot. But if I may, why do you prefer to use logical IF rather than Xor to set and remove the strikeout?
 

Herman

Well-known member
Joined
Oct 18, 2011
Messages
882
Location
Montreal, QC, CA
Programming Experience
10+
My guess is to avoid having to explain it like this thread. Code you have to look at for a while or ask questions about is not good code. Although arguably the original code is clear enough for initiates in this particular case.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
The original code is bad in my opinion. I agree with JB. If you're using a CheckBox to control this then you should be testing the Checked property of that CheckBox and deciding what to do based on that. If there was an instance where the style was changed independently of the CheckBox then that original code would end up doing the opposite of what you wanted. Where it would be appropriate to use Xor to toggle would be if you were using a Button and you simply wanted to change the current style on each Click.
 
Top Bottom