Select Case > 0

General FreeBASIC programming questions.
Dinosaur
Posts: 1357
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Select Case > 0

Postby Dinosaur » Nov 18, 2008 0:37

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
jevans4949
Posts: 1156
Joined: May 08, 2006 21:58
Location: Crewe, England

Postby jevans4949 » Nov 18, 2008 0:59

Code: Select all

select case myvariable
case is >0
   ...
case else
    ...
end select


... but a simple IF MYVARIABLE>0 THEN would suffice
Dinosaur
Posts: 1357
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Postby Dinosaur » Nov 18, 2008 1:24

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 ?

Code: Select all

Select Case (Expression)
   Case Varbl1
      'do whatever
   Case Varbl2
      'do whatever
      :
      :
      :
   Case Varbl30
End Select
McLovin
Posts: 82
Joined: Oct 21, 2008 1:15
Contact:

Postby McLovin » Nov 18, 2008 1:32

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:

Code: Select all

If Varbl1 > 0 Then

ElseIf Varbl2 > 0 Then

ElseIf Varbl3 > 0 Then
.
.
.
ElseIf Varbl20 > 0 Then

End If



----
Fogell
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Postby stylin » Nov 18, 2008 2:01

Dinosaur, regarding Select Case, you can find the first non-zero value by doing,

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


Hope this helps. For other uses, check out,
http://www.freebasic.net/wiki/KeyPgSelectcase
Dinosaur
Posts: 1357
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Postby Dinosaur » Nov 18, 2008 2:03

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: 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
[/code]

Regards and thanks for your suggestions.
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Postby 1000101 » Nov 18, 2008 2:11

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


The first "case" of what?

If you mean something like this:

Code: Select all

Select Case X = 0


There is something like that:

Code: Select all

If X = 0 Then
Dinosaur
Posts: 1357
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Postby Dinosaur » Nov 18, 2008 2:40

Hi all

The first "case" of what?
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.
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
Regards
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Postby stylin » Nov 18, 2008 2:49

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
McLovin
Posts: 82
Joined: Oct 21, 2008 1:15
Contact:

Postby McLovin » Nov 18, 2008 3:03

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
Dinosaur
Posts: 1357
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Postby Dinosaur » Nov 18, 2008 3:15

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
counting_pine
Site Admin
Posts: 6228
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Nov 18, 2008 17:48

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.
jevans4949
Posts: 1156
Joined: May 08, 2006 21:58
Location: Crewe, England

Postby jevans4949 » Nov 19, 2008 1:00

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

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
Dinosaur
Posts: 1357
Joined: Jul 24, 2005 1:13
Location: Hervey Bay
Contact:

Postby Dinosaur » Nov 19, 2008 3:47

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.

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


Regards

Return to “General”

Who is online

Users browsing this forum: No registered users and 3 guests