Folding a select case into a single call? (solved)

New to FreeBASIC? Post your questions here.
elsairon
Posts: 207
Joined: Jul 02, 2005 14:51

Folding a select case into a single call? (solved)

Post by elsairon »

I have in a program a large select case which activates a function based on a users keypress. Something like this;

Code: Select all

select case key
  case key_a
     call sub_a()
  case key_b
     call sub_b()
  case key_c
     call sub_c()
  ' etc....
end select
It could easily be re-written as (another example):

Code: Select all

if key = key_a then 
  call sub_a()
elseif key = key_b then
   call sub_b()
elseif key = key_c then
   call sub_c()
' etc...
end if

My question. Is there another way to do this?

Can I do something like

Code: Select all

call sub correct_key(key)
and have the program branch to the correct sub, such as.

Code: Select all

sub correct_key(key_a) 
' that points to 
sub key(key_a)

' and

sub correct_key(key_b)
' that points to 
sub key(key_b)
without having yer another switch statement, instead using
Key as an index into a table of pointers to the Subs or something?

With my little programming experience I am not sure what this is called or if FreeBASIC can do it.

If there is another post on this topic or a tutorial I'll read them, if I knew
what they were called!

Thanks in advance. Elsairon
Last edited by elsairon on Sep 11, 2006 14:04, edited 1 time in total.
Oz
Posts: 586
Joined: Jul 02, 2005 14:21
Location: Waterloo, Ontario, Canada
Contact:

Post by Oz »

no matter how you break your code down, you will need to do an if/then/else or select case for that sort of thing. I understand what you are talking about, because I once pondered about it, but concluded that my thoughts were completely insane and almost pointless

I'd suggest doing this:

Code: Select all

Declare Sub HandleKeyboard( key As String * 1 )

'' ...

Sub HandleKeyboard( key As String * 1 )
  Select Case key
      case "a"
        '' Call the function for letter "a"
      case "b"
        '' Do whatever for letter "b"
  End Select
End Sub
Oz~
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Re: Folding a select case into a single call?

Post by stylin »

elsairon wrote:... using Key as an index into a table of pointers to the Subs or something?
Sure thing.

Code: Select all

const key_count as uinteger = 3

sub proc_a () : /' ... '/ : end sub
sub proc_b () : /' ... '/ : end sub
sub proc_c () : /' ... '/ : end sub

'' initialize an array of sub ptrs
dim procs(key_count - 1) as sub () = _
{ @proc_a, @proc_b, @proc_c }

/' ... '/

'' call the appropriate sub, assuming key is an
'' integral type with values from 0 to key_count - 1
procs(key)()
edit: small little change to the wording of a comment
Last edited by stylin on Sep 12, 2006 15:25, edited 2 times in total.
elsairon
Posts: 207
Joined: Jul 02, 2005 14:51

Exactly what I was looking for!

Post by elsairon »

This will help with the huge switches I'll use, making my code orders of magnitude more clear and concise, thanks stylin!
Stormy
Posts: 198
Joined: May 28, 2005 17:57
Location: Germany
Contact:

Post by Stormy »

Wow, I've never seen such a nice hack for a long time. Very nice, stylin. ^^
Eclipzer
Posts: 432
Joined: Oct 01, 2005 10:50
Location: Maryland
Contact:

Post by Eclipzer »

Stylin, could you explain your code syntax? I've never seen code written like this before.

Code: Select all

sub proc_a () : /' ... '/ : End Sub
sub proc_b () : /' ... '/ : End Sub
sub proc_c () : /' ... '/ : End Sub
I know the ":" symbol indicates a new line of code, but what about that slash ("/") symbol? What is that doing?

Code: Select all

Dim procs(key_count - 1) As Sub () = _
{ @proc_a, @proc_b, @proc_c }
Alright, what does the "_" do in this piece? I'm guessing it acts as a continuation of the previous line?

Thanks in advance!

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

Post by stylin »

Eclipzer wrote:

Code: Select all

sub proc_a () : /' ... '/ : End Sub
sub proc_b () : /' ... '/ : End Sub
sub proc_c () : /' ... '/ : End Sub
I know the ":" symbol indicates a "new line" of code, but what about that back slash? What is that doing?
The /' and '/ symbols delimit a multi-line C-like comment; anything in between is ignored by the compiler (and some programers, too).
Alright, what does the "_" do in this piece? I'm guessing it acts as a continuation of the previous line?
And you'd be correct. :)
Eclipzer
Posts: 432
Joined: Oct 01, 2005 10:50
Location: Maryland
Contact:

Post by Eclipzer »

Alright, I don't understand why your code actually works though. Take a look at this:

Code: Select all

  declare sub dummy()
  
  dim subptr as sub
  
  subptr=@dummy
  subptr
  
  sleep

  sub dummy()
    ? "Here!"
  end sub
I get an error when trying to use subptr to call dummy(). What am I missing?

-Eclipzer
Eclipzer
Posts: 432
Joined: Oct 01, 2005 10:50
Location: Maryland
Contact:

Post by Eclipzer »

Ah, I see now! The parenthesis are required. Wow, it's not often that I learn a whole new programming technique. This is really awesome. Heh, I must sound like an overexctied newbie. Time to go code.

-Eclipzer
Oz
Posts: 586
Joined: Jul 02, 2005 14:21
Location: Waterloo, Ontario, Canada
Contact:

Re: Folding a select case into a single call?

Post by Oz »

stylin wrote:
elsairon wrote:... using Key as an index into a table of pointers to the Subs or something?
Sure thing.

Code: Select all

const key_count as uinteger = 3

sub proc_a () : /' ... '/ : end sub
sub proc_b () : /' ... '/ : end sub
sub proc_c () : /' ... '/ : end sub

'' initialize an array of sub ptrs
dim procs(key_count - 1) as sub () = _
{ @proc_a, @proc_b, @proc_c }

/' ... '/

'' call the appropriate sub, assuming key
'' is an integral from 0 to key_count - 1
procs(key)()
That's very classy - didn't even think on that thought-train

i think stylin needs to have a FB-Party!!! :)

Oz~
tunginobi
Posts: 655
Joined: Jan 10, 2006 0:44
Contact:

Re: Folding a select case into a single call?

Post by tunginobi »

stylin wrote:

Code: Select all

const key_count as uinteger = 3

sub proc_a () : /' ... '/ : end sub
sub proc_b () : /' ... '/ : end sub
sub proc_c () : /' ... '/ : end sub

'' initialize an array of sub ptrs
dim procs(key_count - 1) as sub () = _
{ @proc_a, @proc_b, @proc_c }

/' ... '/

'' call the appropriate sub, assuming key
'' is an integral from 0 to key_count - 1
procs(key)()
Sorry, the word "integral" is already taken by calculus. Have a nice "integer" instead. :)
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Post by stylin »

Dunno about calculus, but I referred to the CS version (since it does have a little relevance, here on a programming forum ;p): http://en.wikipedia.org/wiki/Integer_(computer_science)
tunginobi
Posts: 655
Joined: Jan 10, 2006 0:44
Contact:

Post by tunginobi »

I presume you got it from this statement?
These are also known as integral data types.
It's being used as an adjective, meaning (off the top of my head) "core" or "fundamental". Yes, it can relate to integers, but only as an adjective. You're using it as a noun, whose definition has nothing to do with integers.

Then again, English is a silly language to get pedantic over. It seems to have more exceptions than there are rules.
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Post by stylin »

tunginobi wrote:I presume you got ["integral"] from [wikipedia]?
Nah, I usually say 'integral'. I wanted to avoid 'integer' because usually in a programming context that means a distinct type, and I didn't want to give the impression that key was restricted to being an integer. I guess a more correct wording might be "anything convertible to an integer", but that's a little vague, particularly in a beginner's forum code snippet (why you'd use a float for an index into an array of sub pointers is beyond me). Thanks for the heads up. :)
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

I think "integer" would have been OK there, or maybe even just "number." There wasn't too much ambiguity in the context. (Talking about an "integral from a to b" would definitely have meaning in calculus though ;)
Post Reply