473,395 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1414
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Joe Stevenson | last post by:
Hi all, I skimmed through the docs for Python, and I did not find anything like a case or switch statement. I assume there is one and that I just missed it. Can someone please point me to the...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
18
by: Minti | last post by:
I was reading some text and I came across the following snippet switch('5') { int x = 123; case '5': printf("The value of x %d\n", x); break; }
2
by: MJ | last post by:
Hi We can use the switch statement, or if else statement instead of switch One more method is there which can replace the switch using the function pointer or the array of function pointer. If...
13
by: Michael Griebe | last post by:
Simple question. I am optimizing some C++ code and I'd like to know which is faster (or if there is any difference at all) between using a switch statement or nested else-ifs. I'm partial to...
1
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
5
by: Phuff | last post by:
Hey all, I need some direction help. I have a switch case statement that is seemingly my only option right now, but its too large and not easy to maintain the code. Here goes... I have part...
21
by: aaron80v | last post by:
Hi, A simple question: In ANSI-C, how to specify a string as the expression for switch statement. eg switch (mystr) { case "ABC" : bleh... break;
10
by: anon.asdf | last post by:
Here's a reminder of duff's device: /*************************************/ #include <stdio.h> #define STEP 8 #define MAX_LEN STEP*4+1 #define SOURCE_LEN 28
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.