473,549 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

switch statement alternative

Hi,

My program has to be able to call on many classes that differ only by a
number and rather than use a switch statement I was wandering if it is
possible to do it in C# as it is possible in other languages. For example,
if I want to call any one of 100 different classes named clsNo1,
clsNo2,clsNo3, ..... clsNo100 from my calling program I can use the switch
statement but it is obviously quite cumbersome. Is it possible instead of
doing something like this:
class Program
{

static void Main(string[] args)

{

string lcStr="clsNo";
lcStr+="5"; // This is to be able to call the class clsNo5

// and then I would like to be able to execute this
statement, so that it would be the equivalent of calling //
the class clsNo5();
}

}

TIA
Roy
Jan 4 '06 #1
4 1827
Hi,
"Roy Gourgi" <ro***@videotro n.ca> wrote in message
news:aS******** *************@w agner.videotron .net...
Hi,

My program has to be able to call on many classes that differ only by a
number and rather than use a switch statement I was wandering if it is
possible to do it in C# as it is possible in other languages. For example,
if I want to call any one of 100 different classes named clsNo1,
clsNo2,clsNo3, ..... clsNo100 from my calling program I can use the switch
statement but it is obviously quite cumbersome. Is it possible instead of
doing something like this:


You are confused with the concept of a Class and an "instance of a class"
you create instances of a class (also know as objects) and call/use members
from them, unless that the method in question is static that you use the
class name to call it, but this is an exception of the rule.

With that in mind your question gets like , what if I want to create an
instance from one of a number of classes and I do not know which one untill
runtime?
Note that the similarity of the name has no importance at all.

One solution is to use reflection , using one of the CreateInstance methods
you can accomplish this.

but this is only the half part of the matter, the other is what variable
will hold this reference. You do not especify nothing about this.
Do all the classes implement a common interface?
Do they are derived from a common parent?

If not you will have to hold it in an object variable and to be useful at
all you will have to use Reflection to call members

And the big question, why you have so many classes ?

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 4 '06 #2
Roy,

I don't understand what you have in mind by calling a class. Is it creating
class instances, calling static methods or by vlass you meen objects
(instances of a class)?

For the formaer 2 you can use reflection For the latter you can organize the
references to the object in some kind of collections and call their methods.
For calling methods you can have your classes implement some interface or
use reflection again.

I believe you should post more background information on the problem.
--

Stoitcho Goutsev (100)

"Roy Gourgi" <ro***@videotro n.ca> wrote in message
news:aS******** *************@w agner.videotron .net...
Hi,

My program has to be able to call on many classes that differ only by a
number and rather than use a switch statement I was wandering if it is
possible to do it in C# as it is possible in other languages. For example,
if I want to call any one of 100 different classes named clsNo1,
clsNo2,clsNo3, ..... clsNo100 from my calling program I can use the switch
statement but it is obviously quite cumbersome. Is it possible instead of
doing something like this:
class Program
{

static void Main(string[] args)

{

string lcStr="clsNo";
lcStr+="5"; // This is to be able to call the class clsNo5

// and then I would like to be able to execute this
statement, so that it would be the equivalent of calling
// the class clsNo5();
}

}

TIA
Roy

Jan 4 '06 #3
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" <ro***@videotro n.ca> wrote in message
news:aS******** *************@w agner.videotron .net...
Hi,

My program has to be able to call on many classes that differ only by a
number and rather than use a switch statement I was wandering if it is
possible to do it in C# as it is possible in other languages. For example,
if I want to call any one of 100 different classes named clsNo1,
clsNo2,clsNo3, ..... clsNo100 from my calling program I can use the switch
statement but it is obviously quite cumbersome. Is it possible instead of
doing something like this:
class Program
{

static void Main(string[] args)

{

string lcStr="clsNo";
lcStr+="5"; // This is to be able to call the class clsNo5

// and then I would like to be able to execute this
statement, so that it would be the equivalent of calling
// the class clsNo5();
}

}

TIA
Roy

Jan 4 '06 #4
In this case you can use Reflection. Create your string that contains the
name of the methods get the type object from the instance of the type -
obj.GetType(), get the MethodInfo for the particular method
t.GetMethod(<me thodName) and use MethodInfo's Invoke method to call the
method.

Keep in mind that C# is a compiler so you cannot there is no & or Eval
operators.

--
HTH
Stoitcho Goutsev (100)

"Roy Gourgi" <ro***@videotro n.ca> wrote in message
news:Pv******** *************@w agner.videotron .net...
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" <ro***@videotro n.ca> wrote in message
news:aS******** *************@w agner.videotron .net...
Hi,

My program has to be able to call on many classes that differ only by a
number and rather than use a switch statement I was wandering if it is
possible to do it in C# as it is possible in other languages. For
example, if I want to call any one of 100 different classes named clsNo1,
clsNo2,clsNo3, ..... clsNo100 from my calling program I can use the
switch statement but it is obviously quite cumbersome. Is it possible
instead of doing something like this:
class Program
{

static void Main(string[] args)

{

string lcStr="clsNo";
lcStr+="5"; // This is to be able to call the class clsNo5

// and then I would like to be able to execute this
statement, so that it would be the equivalent of calling // the class
clsNo5();
}

}

TIA
Roy


Jan 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
14118
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 appropriate document, or post an example? I don't relish the idea especially long if-else statements. Joe
10
9560
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 'case' code snippet does not. (the code's function is illustrative.) ////////////////////////////////////////// //////// working 'if/else' switch...
18
3429
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
4396
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 any one has any idea how to do it , could guide me MJ
13
7531
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 else-if. I know to put the if statement that is most likely to be true at the top of the else-if chain -so as to minimize checks. I've searched around...
1
510
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){} Inside this method I determine what object I should call the Persist method on using a switch statement (not very OO). switch (otype)
5
4503
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 descriptions (ie. 3/8" X ____" NYLON ALL-THREAD RODS...or ____" x ____" X ____" ____ WRAPPED MULLION) that I need to replace the blank lines on. I do...
21
7705
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
2417
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
11786
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 just curious to find out is it effective to use this method?? or is there is any other alternative method present so that execution time and code size...
0
7532
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7730
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7492
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7823
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6059
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3510
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1957
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.