Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 14th, 2009, 07:19 PM
Authorized User
 
Join Date: Jun 2007
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to hericles
Default Calling functions from a variable

Hi guys,
I've looked on the net for the answer to this question but would like some further input.
I'm designing an online game where the character can perform a reasonably large number of various actions, most of which have differing effects. I intend to code up the various effects as functions within a special class and call them as the player opts to perform them.
My question is how best to do this. I hope to be able to store the function name in a variable and call it via that.
From what I've seen so far Reflection is the best option or I can use a Case statement. Essentially I would like some advice on which would work best: reflection, a case statement (potentially up to 100+ options) or using the callByName VB method. From what I've read the callByName method can only call methods, not functions. Is this true?

Any help would be appreciated. I hope I've explained this adequately.

Thanks,
Hericles
__________________
\"I will prepare and one day my chance will come.\"
 
Old January 14th, 2009, 07:46 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Well, CASE will give BY FAR the best performance. But there is another completely different way to do this.

First of all, there's no difference between methods and functions...just that in VB you can have a method that is a SUB...but the only difference between a FUNCTION and a SUB is that the latter doesn't return a value. In C# and other languages, you would call them both functions and just say the function returns "void".

Anyway...I'm going to assume that all these methods that you will code up
(1) will be methods on the SAME CLASS. Presumably, methods on the PLAYER class.
(2) will either be methods that take no arguments or will all be methods that take the *same* arguments.

If either of those is not true, then you can *NOT* use a "universal" mechanism to call them. You'll at least have to divide them into groups of homogeneous functions (that is, functions on the same class and that use the same kinds of arguments).

You are going to use VB.NET, yes? So look into using DELEGATES. That is, event handlers that can accept an ADDRESSOF a method. Thus you could, instead of storing the method *name* in some variable, store the ADDRESSOF that method.

This is not a trivial task and would take too long to explain here, but at least look into DELEGATES and ADDRESSOF in VB.NET.

If you have a choice, you might consider coding in C#, instead. The C# syntax and methodology for delegates is easier to code and understand, in my opinion.
 
Old January 14th, 2009, 07:54 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Hmmm...but just happened across this:
http://channel9.msdn.com/posts/scobl...-Visual-Basic/

It's only in VB2008, apparently, but it might be exactly what you are looking for.
 
Old January 14th, 2009, 10:01 PM
Authorized User
 
Join Date: Jun 2007
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to hericles
Default

Hi Old Pedant,
Your assumptions both hold (similar subs will be grouped in the same class and a small dataTable will be passed, altered and passed back for every one along with 2 other variables).
Switching to C# for the calling part of the app won't be a problem either (well, a bit of rework).
I didn't think of delegates to be honest. Thanks for the tip:)

Hericles
__________________
\"I will prepare and one day my chance will come.\"





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help calling and using a functions in visual c++ 6 method Visual C++ 0 July 8th, 2006 05:20 PM
Calling PHP functions from ASP Smoobly Classic ASP Basics 4 January 10th, 2005 06:11 PM
Calling functions ejmichaud Access VBA 2 July 16th, 2004 10:03 AM
calling functions with conditions Toka1 Javascript How-To 2 February 5th, 2004 09:29 AM
Calling functions in Windows? jacob C# 0 November 5th, 2003 03:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.