471,315 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 1713
Hi,
"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:aS*********************@wagner.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***@videotron.ca> wrote in message
news:aS*********************@wagner.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***@videotron.ca> wrote in message
news:aS*********************@wagner.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(<methodName) 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***@videotron.ca> wrote in message
news:Pv*********************@wagner.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***@videotron.ca> wrote in message
news:aS*********************@wagner.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 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
reply views Thread by rosydwin | 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.