Switch statement alternative

  • Thread starter Thread starter Roy Gourgi
  • Start date Start date
R

Roy Gourgi

Hi,

Sorry I though that it was quite trivial what I was asking but I guess not,
I will explain. :)

What I meant is to call methods (and not classes, that was a misnomer sorry)
in a class that differ only by a number. The reason as I am looking at it
right now (that might change later upon closer scrutiny) is that I have many
methods (it is much more than 100's by the way) that are each completely
different in what they do, but are similar only in name and thus I have to
categorize them. So method mNo1 does something completely different than
mNo2 and so on......., but the method name is mnemonic and it categorizes
each method accordingly. The numbers used 1 to 100 for examples are actual
parameters that help me describe the method that will be executed.

In Visual Foxpro for example I can do the following:

lcStr = "mNo5"

and then do this

&lcStr

The ampersand (&) tell VFP to treat it as a statement and not a literal and
it will therefore execute the function named mNo5.


I hope this answers everybody's questions, and so can anybody show me how to
do this exactly?


Thanks
Roy
 
"Roy Gourgi" <[email protected]> a écrit dans le message de [email protected]...

| In Visual Foxpro for example I can do the following:
|
| lcStr = "mNo5"
|
| and then do this
|
| &lcStr
|
| The ampersand (&) tell VFP to treat it as a statement and not a literal
and
| it will therefore execute the function named mNo5.

C# is a compiled language, not an interpreted one. Although you could use
reflection to do this, I would seriously doubt the wisdom of deciding on
method execution based on a choice of hundreds of strings.

You say that the methods are grouped; this would indicate that you could
build classes, one for each group and add methods to that class. If the
methods do the same task differently depending on which group they are in,
then I would suggest that a class hierarchy with an abstract base class and
virtual methods that are overridden in derived classes might be the way to
go.

I would respectfully suggest that you need to seriously redesign your FoxPro
style app to be more object-oriented, thus suiting it more to programming in
C#. Of course, if you need advice on how to do this, then that is what
newsgroups like this are for :-)

Joanna
 
Roy Gourgi wrote:

In Visual Foxpro for example I can do the following:

lcStr = "mNo5"

and then do this

&lcStr

The ampersand (&) tell VFP to treat it as a statement and not a literal and
it will therefore execute the function named mNo5.

Rather than having a string variable, why not have a delegate variable?
Then you can effectively say which method should be executed at one
point, and execute it at a different point. It doesn't help if you want
to do other things (like evaluate an expression etc) but it sounds like
it might help.

As Joanna said though - redesigning in a more OO way may help more in
the long run.

Jon
 
Hi Joanna,

Yes it is true what you are saying but unfortunately the nature of my
program is mathematical and the numbers are quite large.

For example imagine this scenario where it is not 100 methods but 1000's
of methods and so somewhere down the hierchy of all the classes that you
create there will have to be a switch statement and that would not be
pretty.

It would look like this:


lnVar=? this could be 1 to 1000

switch()
case lnVar = 1 execute method1
..
..
case lnVar =1000 execute method 1000

You see what I mean? Any answers?

Roy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top