Answered by:
what am i doing wrong with this VB9 code

Question
-
Here's what I tried:
' now let's blink the lblSum color! Dim intCounter As Integer Do While intCounter < 20 If lblSum.ForeColor = Color.Turquoise = True Then lblSum.ForeColor = Color.OrangeRed = False Else lblSum.ForeColor = Color.Turquoise = True End If Me.Refresh() System.Threading.Thread.Sleep(250) intCounter = (intCounter + 1) Loop
VB threw an error about overload and not being able to respond to a bol question. Fair enough. Through trial and error I dropped the = True and = False syntax and my lblSum blinked color.
Now the question is:' now let's blink the lblSum color! Dim intCounter As Integer Do While intCounter < 20 If lblSum.ForeColor = Color.Turquoise = True Then lblSum.ForeColor = Color.OrangeRed Else lblSum.ForeColor = Color.Turquoise End If Me.Refresh() System.Threading.Thread.Sleep(250) intCounter = (intCounter + 1) Loop
I've added = True to the first line of the selection and didn't throw an error. How come? (like I said it works with ALL = T and = F removed)Friday, September 18, 2009 4:16 PM
Answers
-
In the line you removed the =True, you were trying to do an assignment in an inappropriate way.
You want to set the ForeColor property on the label, and its type is Color, not boolean. I realize that the syntax makes sense to you, but it does not work that way with the compiler. (In C, you can assign like types, but...even C won't let you assign two different types to a regular variable.)
True / False can be problematic in practice. False is (I believe) always zero. True is simply Not False, which is all ones, or -1 when viewed as an integer. In VB, you can safely use booleans without " = True/False", namely...
If a then
The " = True" is implied, and False will evaluate to zero, making the logic work properly.- Edited by jinzai Friday, September 18, 2009 6:10 PM clarify
- Proposed as answer by Levi Domingos Sunday, September 20, 2009 6:08 PM
- Marked as answer by Jeff Shan Wednesday, September 23, 2009 6:50 AM
Friday, September 18, 2009 6:08 PM
All replies
-
A statement like this
lblSum.ForeColor = Color.OrangeRed = False
doesn't make any sense, because you are trying to combine color strucutures and boolean (true/false) values
This will work for you:
For intCounter As Integer = 0 To 20 lblSum.ForeColor = If(lblSum.ForeColor = Color.OrangeRed, Color.Turquoise, Color.OrangeRed) System.Threading.Thread.Sleep(250) lblSum.Refresh() Next
however you may want to look into a background worker or spin up a seperate thread versus sleeping the main UI thread and needing to call Refresh to update the UI.
Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.comFriday, September 18, 2009 6:05 PMModerator -
In the line you removed the =True, you were trying to do an assignment in an inappropriate way.
You want to set the ForeColor property on the label, and its type is Color, not boolean. I realize that the syntax makes sense to you, but it does not work that way with the compiler. (In C, you can assign like types, but...even C won't let you assign two different types to a regular variable.)
True / False can be problematic in practice. False is (I believe) always zero. True is simply Not False, which is all ones, or -1 when viewed as an integer. In VB, you can safely use booleans without " = True/False", namely...
If a then
The " = True" is implied, and False will evaluate to zero, making the logic work properly.- Edited by jinzai Friday, September 18, 2009 6:10 PM clarify
- Proposed as answer by Levi Domingos Sunday, September 20, 2009 6:08 PM
- Marked as answer by Jeff Shan Wednesday, September 23, 2009 6:50 AM
Friday, September 18, 2009 6:08 PM -
You can't use an expression as a variable. You need an additional variable to represent the state of the lamp.
Static IsTurquoise as Boolean Dim intCounter As Integer Do While intCounter < 20 If IsTurquoise = True Then lblSum.ForeColor = Color.OrangeRed IsTurquoise = False Else lblSum.ForeColor = Color.Turquoise IsTurQuoise = True End If Me.Refresh() System.Threading.Thread.Sleep(250) intCounter = (intCounter + 1) Loop
Friday, September 18, 2009 11:13 PM -
thanks jinzai-
You're right my logical thinking was "if not one color true then..."
kleinma said it succcinctly-"doesn't make any sense"
thanks all!Sunday, September 20, 2009 5:10 PM