Answered by:
boolean

Question
-
I have a boolean condition that I want tthe program to recognize changes when they occur. Currently in order for the program to recognize a change I must restart the program.
I have tried using a public timer but that didn't work.
Specifically.....
D1A.Close() D1A = File.OpenText("MEASURE UNITS.TXT") D1B = CStr(D1A.ReadLine) D1A.Close() Dim P7 As String = (D1B) If P7.ToLower.Contains("english") Then ENGLISH = True And METRIC = False And MIXED = False ElseIf P7.ToLower.Contains("metric") Then METRIC = True And ENGLISH = False And MIXED = False ElseIf P7.ToLower.Contains("mixed") Then MIXED = True And ENGLISH = False And METRIC = False End If
I am re-writting the text file "MEASURE UNITS.TXT" labeled (D1B). Depending on this boolean equation different options become available in other parts of the program.
I need the program to re-adjust the boolean equation on the fly.
Can this be done??
- Moved by Jack Zhai-MSFTMicrosoft contingent staff Thursday, May 31, 2012 8:03 AM The VB issue. (From:Visual Studio Debugger)
Wednesday, May 30, 2012 9:25 PM
Answers
-
Cor:
I see my mistake. I didn't notice the difference in the previous posts. I have tried with colons and it functions as it needs to. Thanks, I can move on now.
- Marked as answer by Mark Liu-lxf Friday, June 8, 2012 6:34 AM
Sunday, June 3, 2012 5:14 AM
All replies
-
D1A.Close() D1A = File.OpenText("MEASURE UNITS.TXT") D1B = CStr(D1A.ReadLine) D1A.Close() Dim P7 As String = (D1B) If P7.ToLower.Contains("english") Then ENGLISH = True METRIC = False MIXED = False ElseIf P7.ToLower.Contains("metric") Then METRIC = True ENGLISH = False MIXED = False ElseIf P7.ToLower.Contains("mixed") Then MIXED = True ENGLISH = False METRIC = False End If
I think this is what you meant.- Proposed as answer by Mark Liu-lxf Thursday, May 31, 2012 8:06 AM
Wednesday, May 30, 2012 9:31 PM -
Hi troy,
I am moving your thread into the Visual Basic General Forum for dedicated support. Thanks for your understanding.
Best Regards,
Jack Zhai [MSFT]
MSDN Community Support | Feedback to us
Thursday, May 31, 2012 8:02 AM -
Use in VB for Net the Boolean Operators AndAlso
http://msdn.microsoft.com/en-us/library/cb8x3kfz(v=vs.100).aspx
The And (and Else) are stayed for backwards compatibility the same as the less save way as in VB6
Also avoid the ElseIf mostly it does not what you think you would do and it can easily replaced by the Select Case
http://msdn.microsoft.com/en-us/library/cy37t14y(v=vs.100).aspx
Success
Cor- Edited by Cor Ligthert Thursday, May 31, 2012 9:12 AM
Thursday, May 31, 2012 9:10 AM -
D1A.Close() D1A = File.OpenText("MEASURE UNITS.TXT") D1B = CStr(D1A.ReadLine) D1A.Close() Dim P7 As String = (D1B) If P7.ToLower.Contains("english") Then ENGLISH = True METRIC = False MIXED = False ElseIf P7.ToLower.Contains("metric") Then METRIC = True ENGLISH = False MIXED = False ElseIf P7.ToLower.Contains("mixed") Then MIXED = True ENGLISH = False METRIC = False End If
Actually after having read Cor's comment about elseif, and giving it some more thought, I realized that I don't really know what you are trying to do. For example, this might be what you meant:
ENGLISH = P7.ToLower.Contains("english") METRIC = P7.ToLower.Contains("metric") MIXED = P7.ToLower.Contains("mixed")
It's not clear if you are intending to allow all three. Since you have three boolean variables, I'll assume that any configuration of those variables is valid and the above most accurately represents the conversion from P7 to your three boolean variables.
Although, if it's not valid to be both ENGLIGH and METRIC, but "MIXED" somehow represents the case where both are true, for example, then you should be using an Enum.
Enum Units English Metric Mixed End Enum
On the other hand, if both ENGLISH and METRIC are valid, and NO UNITS is also a valid configuration, then you might consider just two booleans, all configurations of which result in 4 cases. NONE, ENGLISH, METRIC, and BOTH. Or an enum that represents all 4 cases.
Anyway, I hope you find the solution that fits your needs. Feel free to ask for further clarification or assistance.
Thursday, May 31, 2012 12:48 PM -
Use in VB for Net the Boolean Operators AndAlso
http://msdn.microsoft.com/en-us/library/cb8x3kfz(v=vs.100).aspx
The And (and Else) are stayed for backwards compatibility the same as the less save way as in VB6
Also avoid the ElseIf mostly it does not what you think you would do and it can easily replaced by the Select Case
http://msdn.microsoft.com/en-us/library/cy37t14y(v=vs.100).aspx
Success
Cor
Cor,
I've seen you say this several times now, and I think you're mistaken on this. In actual compiled code, they're the same thing.
A while back in a post by Andrew Morton, Rudy had a pretty good write-up; I would encourage you to read it, but in part he said:
"My understanding has always been that each construct is supposed to generate near identical assembly code when given equivalent conditions such as those in my test application. There is no "Switch" statement in assembler, only branching on register comparisons. The "Select" construct is translated into a series of conditional branches in assembler, just as the "If/Then" construct would. This means that as different as the above IL generated by each construct looks, each is supposed to generate near identical binary."
Please call me Frank :)
Thursday, May 31, 2012 2:49 PM -
Use in VB for Net the Boolean Operators AndAlso
http://msdn.microsoft.com/en-us/library/cb8x3kfz(v=vs.100).aspx
The And (and Else) are stayed for backwards compatibility the same as the less save way as in VB6
Also avoid the ElseIf mostly it does not what you think you would do and it can easily replaced by the Select Case
http://msdn.microsoft.com/en-us/library/cy37t14y(v=vs.100).aspx
Success
Cor
Cor,
I've seen you say this several times now, and I think you're mistaken on this. In actual compiled code, they're the same thing.
A while back in a post by Andrew Morton, Rudy had a pretty good write-up; I would encourage you to read it, but in part he said:
"My understanding has always been that each construct is supposed to generate near identical assembly code when given equivalent conditions such as those in my test application. There is no "Switch" statement in assembler, only branching on register comparisons. The "Select" construct is translated into a series of conditional branches in assembler, just as the "If/Then" construct would. This means that as different as the above IL generated by each construct looks, each is supposed to generate near identical binary."
Which one on this Frank?
The AndAlso or my replies about the ElseIf ?
I assume the ElseIf that has not to do with what is translated in code, but what I learned when I was a young programmer. I told the one who told it to me that he was wrong. After the many problems I had with using it which where easily to avoid by simple an Else and an If I had to say to him he was right. I my own programs I've never used them anymore but I've seen many problems with those who used them.
The problem with the ElseIf is that you don't get it nice nested as with seperated Else and if and therefore it is for humans a little bit more difficult to read and follow (be aware that an elseif then can be used as one line).
In the fact of the Select Case it is something else, I tell this because others told this in these forums/newsgroups. I've to say that it is an easy step for clean programming. Before that I never used them.
:-)
Success
Cor
- Edited by Cor Ligthert Thursday, May 31, 2012 3:46 PM
Thursday, May 31, 2012 3:38 PM -
Private Sub IfThen(ByVal colorOfCar As Color) If colorOfCar = Color.AliceBlue Then Job1() ElseIf colorOfCar = Color.AntiqueWhite Then Job2() ElseIf colorOfCar = Color.Aqua Then Job3() Else Job4() End If End Sub
Private Sub SelectCase(ByVal colorOfCar As Color) Select Case colorOfCar Case Color.AliceBlue Job1() Case Color.AntiqueWhite Job2() Case Color.Aqua Job3() Case Else Job4() End Select End Sub
Cor,
As much as anything, I used that link because in that same thread, you argued the same thing. I'm not trying to be pugnacious but I think you're simply wrong.
Tell me this, in the preceding code samples, what is the difference between them?
Please call me Frank :)
Thursday, May 31, 2012 3:47 PM -
Using And (Or, Xor, etc.) are not "for backwards compatibility". AndAlso and OrElse are short-circuited evaluation operators while And, Or, etc. are mathimatical operators. Using these for evaluation (True/False) works with Option Strict Off because False is 0 and True is not 0. With Option Strict On (the default I use), this is more clearly brought to the surface; however, does not remove the fact that And, Or, etc. are useful and possibly even relevant to this specific scenario. Here's an example...
Dim units As UnitFlags = UnitFlags.None 'Dim value As String = "English|MeTriC".ToLower 'P7 'Dim value As String = "English".ToLower 'P7 Dim value As String = "Metric".ToLower 'P7 'Dim value As String = "Mixed".ToLower If value.Contains("mixed") Then units = UnitFlags.English Or UnitFlags.Metric Else units = CType(If(value.Contains("english"), UnitFlags.English, 0) Or If(value.Contains("metric"), UnitFlags.Metric, 0), UnitFlags) End If If (units And (UnitFlags.English Or UnitFlags.Metric)) > (UnitFlags.English Or UnitFlags.Metric) Then MsgBox("Both") ElseIf (units And UnitFlags.English) > 0 Then MsgBox("English") ElseIf (units And UnitFlags.Metric) > 0 Then MsgBox("Metric") Else MsgBox("None") End If
The key points are I've created an Enum (as someone else mentioned) and made this a Flags() type of enumeration. Based on the value being used, it determines what the bit pattern will be... using the Or operator. To evaluate whether or not the value is "both", english, metric or noneoftheabove, you can use the And operator. Notice that if I'm checking for multiple values, you'll need to evaluate to make sure it's equal to the values you are looking for; otherwise, if you are just checking for on or the other, the greater than 0 can be used. (Note: Normally, I would alway check against the value directly instead of using greater than 0; I'm just doing this to show that it could be done.)
Another point regarding "backwards compatibility"... bit operators are something that is critical to a computer program and something that is definately not something to be ignored. Additionally, if these were just for backward compatibility, they would not be available on Silverlight, Metro, Xna or Windows Phone development.
Regarding the ElseIf comment, there is some truth to this; however, to say it in a blanket statement as you did does not really educate appropriately.
Select Case units Case UnitFlags.English Or UnitFlags.Metric MsgBox("Both") Case UnitFlags.English MsgBox("English") Case UnitFlags.Metric MsgBox("Metric") Case UnitFlags.None MsgBox("None") Case Else MsgBox("Have no idea, you must have added another type and not handled it here.") End Select
Using the above code, you could modify the evaluation to use a Select Case statement. In many ways, this may be a lot easier to read and maintain; however, that is up the the reader. The *real* benefit is one of execution. If done using the If,ElseIf,Else model, every evaluation will take place up until a match is found. The Select Case is a little smarter and is basically able to utilize a lookup mechanism (under the right conditions... aka, non-calculated values) in order to jump directly to the matching entry. I've seen as high as a 33% increase in speed under the correct conditions... which is enough for me to keep this in mind. However, you have to keep in mind that a simple if then, elseif, else isn't going to have this improvement since there isn't really a lot of evaluations that can take place... this only matters if you have a lot of potential evaluations (such as an interpreter).
In any case, hope this is food for thought.
Cory Smith http://addressof.com
Thursday, May 31, 2012 4:01 PM -
Using the above code, you could modify the evaluation to use a Select Case statement. In many ways, this may be a lot easier to read and maintain; however, that is up the the reader. The *real* benefit is one of execution. If done using the If,ElseIf,Else model, every evaluation will take place up until a match is found. The Select Case is a little smarter and is basically able to utilize a lookup mechanism (under the right conditions... aka, non-calculated values) in order to jump directly to the matching entry. I've seen as high as a 33% increase in speed under the correct conditions... which is enough for me to keep this in mind. However, you have to keep in mind that a simple if then, elseif, else isn't going to have this improvement since there isn't really a lot of evaluations that can take place... this only matters if you have a lot of potential evaluations (such as an interpreter).
In any case, hope this is food for thought.
Cory Smith http://addressof.com
They both execute "top down"; the first one that matches is used and nothing else is evaluated. Using a breakpoint makes it *appear* to magically just jump to the right one, but behind the scenes - and in the IL - it's the same thing. There's no magic involved! ;-)
I honestly haven't even read the OP's problem or code, I was really commenting to Cor about an age-old argument we've had regarding If/Else versus Select Case, and I'm convinced they're identical (in compiled code).
Others have different views, obviously. :)
Please call me Frank :)
Thursday, May 31, 2012 4:06 PM -
Frank,
If you have another opinion, i cannot discuss it with you, but take a little bit more difficult one.
Select Case colorOfCar Case Color.AliceBlue Select Case colorOfChairs Case Color.Blue If 4sitter then JobF14() Else JofF12() End If End Select Case Color.BlueViolet Job2() Case Color.AntiqueWhite If Diesel Then Select Case colorOfChairs Case Color.Blue JofD1() Case Color.Black JobD2() End Select End If Case Color.Aqua Job3() Case Else Job4() End Select
Do this only with ElseIf. You will see you will end in unreadable spagetthi
However, it is just what I learned from somebody else and was glad to learn. Now I tell it to others to help them not to make the same mistakes I did. There is no need that you agree that with me.
Success
CorThursday, May 31, 2012 4:13 PM -
Haha ... Cor, I won't continue the banter here, and yes I could put that as an If/Else If.
I'm not saying that I would, and you're right it would make longer code; my point was that in compiled code, it's the same thing either way.
We'll move on now. ;-)
Please call me Frank :)
Thursday, May 31, 2012 4:16 PM -
This is tricky scenario since there isn't a "one sized answer fits all"; I'll illustrate by using a few examples:
Private Enum Values Value1 = 10 Value2 = 20 Value3 = 30 Value4 = 50 Value5 = 75 End Enum Private Enum Lookup A B C D E End Enum Sub SelectCaseAltOptimized() Dim value As Lookup = Lookup.A Dim result As Integer = 0 Select Case value Case Lookup.E : result += 1 Case Lookup.B : result += 1 Case Lookup.C : result += 1 Case Lookup.D : result += 1 Case Lookup.A : result += 1 Case Else result -= 1 End Select End Sub Sub SelectCaseAlt() Dim value As Values = Values.Value1 Dim result As Integer = 0 Select Case value Case Values.Value5 : result += 1 Case Values.Value3 : result += 1 Case Values.Value1 : result += 1 Case Values.Value2 : result += 1 Case Values.Value3 : result += 1 Case Else result -= 1 End Select End Sub Sub SelectCaseOptimized() Dim value As Integer = 1 Select Case value Case 1 : value += 1 Case 2 : value += 1 Case 3 : value += 1 Case 4 : value += 1 Case 5 : value += 1 Case Else value -= 1 End Select End Sub Sub SelectCase() Dim value As Integer = 1 Dim multiplier As Integer = 1 Select Case value Case multiplier : value += 1 Case multiplier * 2 : value += 1 Case multiplier * 3 : value += 1 Case multiplier * 4 : value += 1 Case multiplier * 5 : value += 1 Case Else value -= 1 End Select End Sub
The SelectCase example, because each result is having to be "calculated", this pretty much ends up very similar in result to a compiled IF/Then construct (in other words, no real optimizations).
The SelectCaseOptimized example will perform circles around the first one since the evaluation can now be a simple "jump table" (in IL, a "switch"). This is because the values are precalculated and "close together". Note, these values do not have to start at 0 or 1, just be "close together" in order for this to work. If not at 0, a subtraction may be done to determine the real lookup value. If there are gaps, the compiler "may" place filler in order to handle the "jump table" optimization.
One way that you can "guaranty" the evaluations being "close together" is by using an enumeration to evaluate your results. However, this only works if the enumeration is "close together". You can see this will not be optimized using the SelectCaseAlt (using Values enum) and it will be optimized using the SelectCaseAltOptimized (using the Lookup enum).
If the Select Case is compiled to an IL "switch"; there will be a performance difference. If not, so it's a few "if" type evaluations which isn't that bad. This returns me back to the original comment regarding that this only really matters if you have a lot of things to evaluate. For a handful of evaluations, you are not going to notice the difference unless this evaluation set is taking place in your code execution path more than most of the other code in your application.
When a the IL "switch" is executed, it's not evaluated in the same... it's not really "evaluated" at all. It takes the value and "jumps" to the correct offset in the switch list; thus making it potentially a lot faster.
I've found a 33% improvement in a pet project (a clone of gw-basic) where this is the case since the core of the project is determining which keyword needs to be further evaluated and processed. Not to mention, the debugging experience is better since I'm not having to step through all of the possible entries before I get to the one that is the one that matters.
Obviously, things might be a little different from compiler version to compiler version, so this comes with a little bit of a disclaimer. (And the results I reviewed were with VS2010SP1.)
Cory Smith http://addressof.com
Thursday, May 31, 2012 5:40 PM -
I'll have to go with Cor on this one. I'd definitely go with a select case.
Renee
"MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
Thursday, May 31, 2012 7:20 PM -
I'll have to go with Cor on this one. I'd definitely go with a select case.
Renee
So would I, but that wasn't the case that I was proposing.
@Cory--> I'll believe your claims; you say your tests prove it so I won't attempt to dispute that. You might be on to something, but I honestly don't see how. Think about it - how can Select Case work? It has to be doing conditional testing, the same as If/Else uses ... how else could it work.
I still think that it's an arbitrary choice and, like I just said to Renee - I was never disagreeing that from a coding standpoint, I tend to use it (until it gets into anything complex where debugging is easier, frankly, with If/Else). I was saying that in compiled code every indication is that it really is the same thing.
Before this gets into some horribly long thread about this debate which has come up several times over the years, I'll just stand by what I say here until I see something that really indicates otherwise.
I hope nobody took any offense at anything I said.
Please call me Frank :)
Thursday, May 31, 2012 7:28 PM -
@Frank--> It's pretty straight forward. Imagine if you could write something in VB that looked like this:
Dim lookupList As New List(Of Integer) From {1, 2, 3, 4, 5} Dim value As Integer = 3 Dim result As String = Nothing If value > 0 AndAlso value < 6 Then
' <<<<< The following line will not compile >>>>>>>
GoTo lookupList(value) Else GoTo 6 End If 1: result = "1" : GoTo 6 2: result = "2" : GoTo 6 3: result = "3" : GoTo 6 4: result = "4" : GoTo 6 5: result = "5" : GoTo 6 6: ' Either done or skipping the scenarios. Return resultNow, before you try... this code ***will not*** work in VB. The reason being that you can't use a variable as a goto "jump point". However, as you can see, the value is evaluated and it is then applied to the lookup table to determine where it needs to jump to. It doesn't work through all the items in the jump table until it finds it, it just knows that it needs to go to the third one in the list.
When the compiler builds the IL for an "optimized" Select Case, it can attempt to do something very similar to this. The "switch" (IL version, not C# version) is a jump table just like this. The contents are the address points of where it will jump to depending on the "indexer" value that is available. This requires that the items be "close together". If there are gaps, the compiler will make a determination if filling the gaps is better than just abandoning this approach alltogether and going the "if/then" route you are already aware of. However, as you can see, if this can be done, the performance can be improved greatly, especially if you have a lot of Cases to work through. Again, you have to follow some very specific rules to get this to happen; but depending on the circumstances, it's well worth it.
So intead of:
Select Case keyword Case "PRINT" Case "END" ' and so on... Case Else End Select
Modified the approach to use an enum and convert the keyword (string) to a value in the enumerator so I get:
Select Case ToKeywordEnum(keyword) Case Keyword.Print Case Keyword.End ' and so on... Case Else End Select
This turns the string values in to a value between 0 and n where n is the number of keywords. By doing this, a jump table (IL "switch") can be utilized which will now just jump (goto) the correct section of the Select Case bypassing the testing for all items prior to it since it can jump directly.
This is a powerful tool if you a) are aware of it, b) need it and c) know how to code for it.
This is why I am saying it can't be answered in an easy manner; since the answer is really "it depends". If it's not optimized in this manner, them it's going to be either a combination of this way and the "if" way or it will just be like doing it the "if" way. So you really have three different outcomes when using Select Case.
In most cases, I'd recommend using Select Case for anything that is more than If, ElseIf, Else. Once you get past that, use Select Case; however, there are many times that a single line, or three line, or even 6 line If statement makes a perfect choice and should not be denied. They are tools in your tool chest; use them how you want... however, learn all you can about them (over time) so you can use them to their best potential.
Cory Smith http://addressof.com
Thursday, May 31, 2012 8:40 PM -
Cory,
I'm done with this thread - but I still don't buy it. There has to be conditional branching somewhere for it to know what it's to do.
Please read Rudy's post from the link that I posted to Cor earlier and have a look at what he showed.
We're stealing the OP's thread here - none of this has helped him I'm sure (I don't know what the issue even is, again, this was something I replied to Cor on), so I won't reply back, but I'm still not sold that's how it works. You are sold on it and that's fine too.
Thanks for the active participation. :)
Please call me Frank :)
Thursday, May 31, 2012 8:48 PM -
Cor;
Can we get back to my question?
Whether I use case or if/else is not relevent. the fact is that currently I am using if/else.
How can I get the program to recognize the CURRENT state of variable D1B?
Friday, June 1, 2012 1:15 PM -
@troy,
I can only assume that we are missing something in your question as I think, based on your original question, that @Wyck and I have answered your question. I'll even give it yet another go using @Wyck's original answer and format it slightly differently so it looks almost identical to your original code:
D1A.Close() D1A = File.OpenText("MEASURE UNITS.TXT") D1B = CStr(D1A.ReadLine) D1A.Close() Dim P7 As String = (D1B) If P7.ToLower.Contains("english") Then ENGLISH = True : METRIC = False : MIXED = False ElseIf P7.ToLower.Contains("metric") Then METRIC = True : ENGLISH = False : MIXED = False ElseIf P7.ToLower.Contains("mixed") Then MIXED = True : ENGLISH = False : METRIC = False End If
The only difference between your original code and this is that I've replaced the **And** between each of your boolean variables with a ":" (colon) character which turns each of these into separate statements. What you had was attempting to do the following:
ENGLISH = (True And (METRIC = False) And (MIXED = False))
I've placed parens in order to illustrate how VB is going to execute what you've written. You can see that it's going to look at the first = sign and then try to set ENGLISH to the result of everything to the right of it. Looking at your question, it's obvious that this isn't what you were going for.
Additionally, some of the additional comments on this thread were about alternatives to how you could handle these flags depending on if they are mutually exclusive or not.
If this doesn't answer your question, please try to ask it in another way because it's obvious that we are missing something in the original question.
Thanks.
Cory Smith http://addressof.com
Friday, June 1, 2012 1:32 PM -
I think you want to use a FileSystemWatcher component to monitor this text file. The FileSystemWatcher can raise an event for you whenever the text file changes and then your handler for this event can execute the code you've posted.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
- Proposed as answer by Acamar Sunday, June 3, 2012 6:17 AM
Friday, June 1, 2012 4:24 PM -
Well you could have the update value go into d1b. When I did this in V2005 I used polling and I can certainly see why you don't want to do that. The project was to get colors at the cursor position any where on the screen which demanded polling. The solution is rather involved which is why I don't post it here. If you'd like the solution please let me know here.
Renee
"MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
Friday, June 1, 2012 10:40 PM -
Cor:
I see my mistake. I didn't notice the difference in the previous posts. I have tried with colons and it functions as it needs to. Thanks, I can move on now.
- Marked as answer by Mark Liu-lxf Friday, June 8, 2012 6:34 AM
Sunday, June 3, 2012 5:14 AM -
The problem with the ElseIf is that you don't get it nice nested as with seperated Else and if and therefore it is for humans a little bit more difficult to read and follow (be aware that an elseif then can be used as one line).
The problem you have with If/Else is only because you create examples that are too complex for the If/Else to handle conveniently. If you keep the examples simple like the one that OP has posted then there is no issue.
But OP's problem has nothing to do with If/Else or Case or whatever. It is a problem of not being able to identify when the file that is the source of the data for that test has changed. If you are going to raise the difficulties you have with this style of programming whenever anyone posts code that includes an If/Else regardless of the problem that they are describing, it is going to get very tiresome very quickly.
Sunday, June 3, 2012 6:05 AM -
Actually an EVENT is the way to do it if I understand what he wants.
Renee
"MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
Monday, June 4, 2012 5:09 AM