Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Switch Statment with a variable number of cases
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
brl1995




PostPosted: Sun Apr 01, 2012 7:23 pm   Post subject: Switch Statment with a variable number of cases

Hi, i was wondering if it would be possible to create a switch statment that has a variable number of cases based on input from the user earlier.

i am using this for a program that creates and handles a number of databases, so if 5 databases were created last time the program ran the switch would have 6 cases (1 for creating a new database and 5 for editing the existing ones). (the tasks preformed by the different cases would be the same)

So is there any way to do this? my only idea was using a for loop and doing somthing like this:

switch (databases)
{
for (i = 0; i <= databaseCount; i++)
{
case (i):
}
}
can someone help me out with this, maybe confirm if my idea would work or not and why as well as any input you may have on the topic would be of great help[/syntax]
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Sun Apr 01, 2012 7:30 pm   Post subject: RE:Switch Statment with a variable number of cases

No, that's not possible. At least, not in Java -- languages that have preprocessors (like C, C++) can do something similar. This is NOT something you should EVER do. There are always easier, more maintainable, and more flexible ways to write your program.

If the tasks performed are the same for each database, then you should have some method that does that thing. It takes the database number. Then your code is:
code:

if ( input < databaseCount ) {
    doDatabaseThing ( input );
} else {
    addNewDatabase();
}
mirhagk




PostPosted: Sun Apr 01, 2012 7:38 pm   Post subject: Re: RE:Switch Statment with a variable number of cases

DemonWasp @ Sun Apr 01, 2012 7:30 pm wrote:
No, that's not possible. At least, not in Java -- languages that have preprocessors (like C, C++) can do something similar. This is NOT something you should EVER do. There are always easier, more maintainable, and more flexible ways to write your program.

If the tasks performed are the same for each database, then you should have some method that does that thing. It takes the database number. Then your code is:
code:

if ( input < databaseCount ) {
    doDatabaseThing ( input );
} else {
    addNewDatabase();
}


Technically it is possible in any language that has code blocks or pointers or lambdas or go statements.

Basically you use a hash map or array, and map the index being the case (i) and the value being the function pointer, lambda, delegate or whatever your language calls it. Might actually be useful in some circumstances, but it doesn't sound appropriate for this problem.
TokenHerbz




PostPosted: Sun Apr 01, 2012 8:09 pm   Post subject: RE:Switch Statment with a variable number of cases

why would you want to modify a data base (unless you mean its not a server) threw java anyways?
DemonWasp




PostPosted: Mon Apr 02, 2012 12:05 am   Post subject: RE:Switch Statment with a variable number of cases

@mirhagk: It's still not possible to have a switch statement with a variable number of cases, where the number of cases changes at run-time. You can emulate it, but generally not write it. Technically, I think it's possible to rewrite your code in most of the managed languages, Java included, but no sane person tries that.

@Token: What?
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: