Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 turn string into call for procedure?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
notAllCS




PostPosted: Wed Apr 23, 2008 8:26 pm   Post subject: turn string into call for procedure?

I wondering if there is any way to use a string as a call to a procedure.

Ex.

procedure myProc

end myProc


var myName : string := "myProc"
% somehow turn "myProc" into myProc
myName


Thanks in advance for your help
Sponsor
Sponsor
Sponsor
sponsor
gitoxa




PostPosted: Wed Apr 23, 2008 8:34 pm   Post subject: Re: turn string into call for procedure?

Why would you want to?
Just make an if statement run the procedure when myName = "myProc"
Zampano




PostPosted: Wed Apr 23, 2008 8:38 pm   Post subject: Re: turn string into call for procedure?

I'm supposing you want the user to be allowed to call code manually by passing a string which has the value of the name of a function or procedure. It's relies on the user of the client to know what procedures it's using. I don't think it can be done anyway. Even if it could, I think it would almost definitely be a bad idea.
What is the context where you want to do this?
notAllCS




PostPosted: Wed Apr 23, 2008 8:40 pm   Post subject: RE:turn string into call for procedure?

Oh sorry. I forgot to mention that this is for a story book type game. There will be about 100 pages and so I don't want to have a massive if statement.

I'm hoping to somehow keep track of what the current page is and then allow the user to click on buttons "back" and "next" to 'turn the page'.

Given the integer page number, I'll have to somehow use that in a procedure call or a windowID
Zampano




PostPosted: Wed Apr 23, 2008 8:50 pm   Post subject: Re: turn string into call for procedure?

I'm not exactly sure why you want this. Is it something like this:
code:
if a =  "forward" then
       forward
elsif a = "back" then
       back
end if

I can't think of a premise wherein you might need to perform many different things depending on many different conditions.
notAllCS




PostPosted: Wed Apr 23, 2008 8:55 pm   Post subject: RE:turn string into call for procedure?

What if I want the user to pick a particular page in my 100 page storybook? Is the best way a massive "if"?
CodeMonkey2000




PostPosted: Wed Apr 23, 2008 10:10 pm   Post subject: RE:turn string into call for procedure?

You should probably index your pages into an array. That way you can add/subtract 1 to your index holder, or just make it equal 100 or something. And when you have to draw it, just draw the page at the current index.
Tony




PostPosted: Wed Apr 23, 2008 11:26 pm   Post subject: RE:turn string into call for procedure?

having 100 procedures for 100 pages is probably as good of an idea as having 100 if statements to access them.

Though CodeMonkey is right, you should use an array to index your pages (either data itself, or a reference to a procedure. That is, call procedure_one() with my_array[1]. An additional lookup table will allow you to map string values to integer indexes.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
rdrake




PostPosted: Wed Apr 23, 2008 11:26 pm   Post subject: Re: turn string into call for procedure?

I'd make each page an object, stick it in an array, then keep track of which page number you're on (which would then correspond to the position of that page in the array minus 1 since arrays are indexed from 0 normally). You'll then be able to call a procedure which every one of the objects have (since they come from the same class).

Just in case you're curious, it can be done in other languages like Ruby Smile
Ruby:
irb(main):001:0> class Foo
irb(main):002:1>   def bar
irb(main):003:2>     "foobar"
irb(main):004:2>   end
irb(main):005:1> end
=> nil
irb(main):006:0> f = Foo.new
=> #<Foo:0xb7ce44e4>
irb(main):007:0> f.bar
=> "foobar"
irb(main):008:0> s = "bar"
=> "bar"
irb(main):009:0> f.send(s)
=> "foobar"
irb(main):010:0>
notAllCS




PostPosted: Thu Apr 24, 2008 7:55 am   Post subject: RE:turn string into call for procedure?

Thanks. That will work.
Vermette




PostPosted: Thu Apr 24, 2008 9:12 am   Post subject: RE:turn string into call for procedure?

Just want to pass along that in other languages, doing what notAllCS asked is possible. It falls under the umbrella of metaprogramming. Definitely not necessary in this example, but still an interesting topic! Smile

Here's some information to pique your interest:
http://en.wikipedia.org/wiki/Reflection_%28computer_science%29
notAllCS




PostPosted: Tue Apr 29, 2008 2:24 pm   Post subject: RE:turn string into call for procedure?

Thanks. I knew that it was possible in other languages.

This is what we ended up doing:
currentPic := Pic.FileNew ("pics/gallery/gallery" + intstr (currentScreen) + ".JPG"

and using a picture for each screen
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: