471,354 Members | 1,246 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Switch statement alternative

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

Jan 4 '06 #1
3 1361
"Roy Gourgi" <ro***@videotron.ca> a écrit dans le message de news:
Hu*********************@wagner.videotron.net...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 4 '06 #2
Roy Gourgi wrote:

<snip>
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

Jan 4 '06 #3

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

*** Sent via Developersdex http://www.developersdex.com ***
Jan 4 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

26 posts views Thread by Joe Stevenson | last post: by
10 posts views Thread by clueless_google | last post: by
18 posts views Thread by Minti | last post: by
13 posts views Thread by Michael Griebe | last post: by
5 posts views Thread by Phuff | last post: by
21 posts views Thread by aaron80v | last post: by
13 posts views Thread by Satya | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.