Select Case > 0
Select Case > 0
Hi all
Does anyone have any ideas how to do that.
I have about 30 cases all of which are Zero except for one.
But I dont know the value.
Regards
Does anyone have any ideas how to do that.
I have about 30 cases all of which are Zero except for one.
But I dont know the value.
Regards
-
- Posts: 1186
- Joined: May 08, 2006 21:58
- Location: Crewe, England
Code: Select all
select case myvariable
case is >0
...
case else
...
end select
Hi all
Jevans4949 you misinterpret me.
I want Select Case to find the one Varbl that is not Zero, regardless of the value.
For the purpose of clarity I put Select Case > 0, which by the way would be a nice option to have in Select Case. But as we dont, what expression means the same thing ?
Jevans4949 you misinterpret me.
I want Select Case to find the one Varbl that is not Zero, regardless of the value.
For the purpose of clarity I put Select Case > 0, which by the way would be a nice option to have in Select Case. But as we dont, what expression means the same thing ?
Code: Select all
Select Case (Expression)
Case Varbl1
'do whatever
Case Varbl2
'do whatever
:
:
:
Case Varbl30
End Select
Is it practical for you to configure your 30 variables as a 30 element array of your variable type (e.g. Integer?) That way you could simply iterate the array with a simple For/Next and test for > 0.
If that is not practical then I think another solution would be a series of If/ElseIf's:
----
Fogell
If that is not practical then I think another solution would be a series of If/ElseIf's:
Code: Select all
If Varbl1 > 0 Then
ElseIf Varbl2 > 0 Then
ElseIf Varbl3 > 0 Then
.
.
.
ElseIf Varbl20 > 0 Then
End If
----
Fogell
Dinosaur, regarding Select Case, you can find the first non-zero value by doing,
Hope this helps. For other uses, check out,
http://www.freebasic.net/wiki/KeyPgSelectcase
Code: Select all
var a = 0, b = 0, c = 1
select case 0
case is < a : print "a is the first non-zero"
case is < b : print "b is the first non-zero"
case is < c : print "c is the first non-zero"
end select
http://www.freebasic.net/wiki/KeyPgSelectcase
Hi all
Fogell the Variables are already elements of different UDT's and I was trying to avoid the IF , Else If.
I have actually solved the problem a different way, but would really be interested in what the developers think about these:[/code]
Regards and thanks for your suggestions.
Fogell the Variables are already elements of different UDT's and I was trying to avoid the IF , Else If.
I have actually solved the problem a different way, but would really be interested in what the developers think about these:
Code: Select all
Select Case = 0 picks the first case that is 0
Select Case > 0 picks the first case that is NOT Zero
Select Case < 0 picks the first case that is less then Zero
Regards and thanks for your suggestions.
The first "case" of what?Dinosaur wrote:I have actually solved the problem a different way, but would really be interested in what the developers think about these:Code: Select all
Select Case = 0 picks the first case that is 0 Select Case > 0 picks the first case that is NOT Zero Select Case < 0 picks the first case that is less then Zero
If you mean something like this:
Code: Select all
Select Case X = 0
Code: Select all
If X = 0 Then
Hi all
So why dont we have a case where I can find the first case that is NOT zero.? What expression do I put to find Case c which is NOT zero ?
Regards
in the case of "Select Case > 0", I mean find the first case where the value is larger then zero. Stylin, I didnt realise you could do "Select Case 0", which finds the first case that IS Zero.The first "case" of what?
So why dont we have a case where I can find the first case that is NOT zero.? What expression do I put to find Case c which is NOT zero ?
Code: Select all
a = 0
b = 0
c = 100
d = 0
e = 0
Select Case ??
Case a
Print a
Case b
Print b
Case c
Print c
Case d
Print d
Case e
Print e
End Select
Dinosaur, the code i posted does indeed find the first non-zero; it's using Cases of "Is < value", which executes if "0 < value", ie., non-zero and also non-negative, so this won't work as is for finding the first negative value. To do that, you'd catch both less and greater values:
Code: Select all
var a = 0, b = 0, c = 1
select case 0
case is < a, is > a : print "a is the first non-zero"
case is < b, is > b : print "b is the first non-zero"
case is < c, is > c : print "c is the first non-zero"
end select
The Select Case 0 is pretty interesting. I have never done that before. Thanks!
However, I still don't see how the Select Case ideas are any better than a simple If/ElseIf construct. In terms of long term code maintenance, it would be certainly more readable and logical to understand. Having said that, I am very interested in seeing what kind of solution the gurus here come up with.
----
Fogell
However, I still don't see how the Select Case ideas are any better than a simple If/ElseIf construct. In terms of long term code maintenance, it would be certainly more readable and logical to understand. Having said that, I am very interested in seeing what kind of solution the gurus here come up with.
----
Fogell
Hi all
Stylin, my apologies. It does in fact do what you said.
I have never used "Select case 0", and in QB I go back 30 years.
The expression I was looking for is in fact in each Case, and not in the Select Case.
Still, there is a case to be made (no pun intended) for expanding the functionality of Select case. I mean from a programming point of view what is simpler then Select Case > 0, or even Select case > 231. Particularly, now that you have made me aware that Select Case 0 is allowed.
Many thanks for your input.
Regards
Stylin, my apologies. It does in fact do what you said.
I have never used "Select case 0", and in QB I go back 30 years.
The expression I was looking for is in fact in each Case, and not in the Select Case.
Still, there is a case to be made (no pun intended) for expanding the functionality of Select case. I mean from a programming point of view what is simpler then Select Case > 0, or even Select case > 231. Particularly, now that you have made me aware that Select Case 0 is allowed.
Many thanks for your input.
Regards
-
- Site Admin
- Posts: 6323
- Joined: Jul 05, 2005 17:32
- Location: Manchester, Lancs
What you're suggesting isn't really part of the philosophy of Select Case.
Select Case is designed to take a value and compare it against a set of conditions - particularly in Select Case As Const, where the conditions are defined and fixed at compile-time.
What you want Select Case to do is take a condition and match it against a set of values, which it's not designed for.
Select Case is designed to take a value and compare it against a set of conditions - particularly in Select Case As Const, where the conditions are defined and fixed at compile-time.
What you want Select Case to do is take a condition and match it against a set of values, which it's not designed for.
-
- Posts: 1186
- Joined: May 08, 2006 21:58
- Location: Crewe, England
Not sure exactly what your program does, but apparently 30-odd objects have to be checked, and only one can return non-zero.
Rather than your main path checking all 30, can the non-zero event store its identity in some sort of global variable? Or maybe have each of the 30 processes issue a return code and have something like
Rather than your main path checking all 30, can the non-zero event store its identity in some sort of global variable? Or maybe have each of the 30 processes issue a return code and have something like
Code: Select all
happeningevent =0
if process1>0 then
if happeningevent = 0 then
happeningevent = 1
endif
endif
...
if process30>0 then
if haaeningevent = 0 then
happeningevent = 30
endif
endif
select case happeningevent
case 1
consequence1
...
case 30
consequence30
end select
Hi all
jevans4949, we are on the same wavelength.
That is exactly how I solved the problem.
The problem involved a single Filename selection routine, that put about 300 filenames on the screen in the form of buttons.
Then there are about 30 functions that need a filename.(not all the same name). Just a snippet below.
Regards
jevans4949, we are on the same wavelength.
That is exactly how I solved the problem.
The problem involved a single Filename selection routine, that put about 300 filenames on the screen in the form of buttons.
Then there are about 30 functions that need a filename.(not all the same name). Just a snippet below.
Code: Select all
Sub Recipe_Select cdecl (byval userdata as any ptr)
Dim Filename As String
Dim FilePath As String
Dim IPtr As Integer Ptr
IPtr = userdata
FileCount = *IPtr
'------------------------
FilePath = "\ConcHMI\Recipes\"
FileName = FileArray.Name(FileCount)
Select Case Tabs.Origin
Case Tabs.Win1
MStatus.FileName = FileName
PUTMStatus
FILEREAD
Case Tabs.Mixer1
Mixer1Stat.BatchName = Filename
Case Tabs.Mixer2
Mixer2Stat.BatchName = Filename
Case Tabs.Mc11
Machine.MC(1).BatchName = Filename
Case Tabs.MC12
Machine.MC(2).BatchName = Filename