|
|
Owner/Admin
Posts: 3925
Location: USA: GA, Lawrenceville | Select Case is very useful, but most folks don't realize how flexibly it can be set up. It's not only easier to type and read than a series of if/elseif.../endif clauses, but it also allows for very "compact" conditional combinations, by using commas and "to" ranges.
Here is an example (snapshot, so indents aren't lost) showing lots of variations ... it's an OLang'd & enhanced form of the one given here: https://msdn.microsoft.com/en-us/library/cy37t14y.aspx
Notes:
1. Case statements do NOT have to be in sequential order
2. "To" ranges need to be from min to max (ie increasing).
3. The "To" ranges are continuous, not limited to integer values
4. No "step" parameter is allowed or needed for the To ranges
5. Case Else covers all situations not within the prior ones - put it LAST
6. If ranges overlap, the first Case found is the one that applies
7. The initial IndexValue can be anything that resolves to things the Case's test for
8. Index and cases can be numbers or strings (or dates or booleans, etc)
9. Unlike "To" ranges, comma-lists do not need to be from min to max
Application suggestions:
1. always use orderly indentation to preserve readability
2. be sure that you have all the possible Index values covered
3. sometimes it saves calc time to skip over the select case by wrapping it in an if clause
(SelectCase Example.png)
Attachments SelectCase Example.png (13KB - 4 downloads)
|
|
|
|
Veteran
Posts: 212
Location: USA: TX, Mansfield | I want to use "Select Case" in some code I am trying to write.
Below is a hybrid of Select Case and If, Then, Else
Can this be done with Select Case or do I have to use If statements?
Select Case StateOfVariable
If EMA1 > EMA2 And Low[1] > EMA1 And Low <= EMA1 And Low >= EMA2 Then
StateOfVariable = Long
Else If EMA2 > EMA1 And High[1] < EMA1 And High >= EMA1 And High <= EMA2 Then
StateOfVariable = Short
Else
StateOfVariable = Flat
End If
If low <= EMA2 Then StateOfVariable = Flat Else StateOfVariable = Long
If high >= EMA2 then StateOfVariable = Flat Else StateOfVariable = Short
|
|
|
|
Owner/Admin
Posts: 3925
Location: USA: GA, Lawrenceville | The code you showed is incorrect syntax as-is. Comment out the select case statement.
What you are trying to accomplish is more suitable for if/then/elseif/else/end if, since the conditions are formulae rather than alternative values of a variable.
Also note that “elseif” is a single word. The parser might accept it as two, but potentially it might create issues. Best to be safe. “End if” is two words. |
|
|
|
Veteran
Posts: 212
Location: USA: TX, Mansfield | ' 5=Long; 3=Flat; 1=Short
If EMA1 > EMA2 And Low[1] > EMA1 And Low <= EMA1 And Low >= EMA2 Then
varState = Long
ElseIf EMA2 > EMA1 And High[1] < EMA1 And High >= EMA1 And High <= EMA2 Then
varState = Short
Else
varState = Flat
End If
If Low <= EMA2 Then
varState = Flat
Else
varState = Long
End If
If High >= EMA2 Then
varState = Flat
Else
varState = Short
End IF |
|
|