3

I have a table with some ids, and i want in default.aspx to open a certain page.aspx with a form, depending on the id.

What i have now is:

if(id_table ==1) {
response.redirect("PageBla.aspx");
}

if(id_table==2) {
response.redirect("Page21231.aspx");
}

if(id_table==6) {
....
}
 etc etc....

this is simple if i have small number of ids to check. But i will have dozens of ids to check. Is there any programming pattern or any other method of doing this without dozens of ifs or switchs/cases?

Thanks in advance

EDIT: "=" was substituted with "==".

6 Answers 6

5

It would be quite easy to have a lookup containing the ID's and urls. It could be in a database for flexibility, but you could also just throw them in a dictionary now, and add the database part later, if you find you need it.

You could declare the lookup as a field:

private static readonly Dictionary<int, string> redirectLookup = new Dictionary<int,string> {
    {1, "PageBla.aspx"},
    {2, "Page21231.aspx"},
    // .....
    {6, "somepage6.apx"}

};

And in your redirect logic:

string redirect;
if (redirectLookup.TryGetValue(id_table, out redirect)) 
    Response.Redirect(redirect);
else
    // some default action when that ID was not mapped.
1
  • Why was this downvoted ? Please leave a comment when downvoting.
    – driis
    Commented Jan 22, 2012 at 17:59
5

Just create a simple array of the URLs like this:

string[] urls = {"PageBla.aspx", "Page21231.aspx"};
response.redirect(urls[id_table]);

If you have a more complex use case, another option is to use Convention over configuration. You can do it like that:

  1. Your tables will have string IDs.
  2. Your redirect code will be as simple as:

    Response.Redirect(tableId + ".asxp");
    
6
  • What happens with your code when id_table == 3? Array indexing is sequential, the id_table variable is not. The use of dictionary, as demonstrated in other responses, is the correct solution.
    – Abbas
    Commented Jan 22, 2012 at 16:43
  • Why do you assume that it is not sequential? The code shown is just a sample code? In case it is, the Dictionary is an overkill. Commented Jan 22, 2012 at 17:53
  • The code shown is obviously sample code very incorrectly representing the desired outcome. The fact that he didn't put ... between element 2 and 6 does not mean he didn't meant to. Just as the fact he used '=' in the 'if' statement does not mean he didn't mean '=='. Commented Jan 23, 2012 at 0:15
  • The assumption is to think that the values of the table_id will be in neat, natural number order EVEN when the OP has clearly indicated otherwise. What you have proposed is a potentially erroneous solution when better and safer solutions have already been posted. That’s the reason for my -1.
    – Abbas
    Commented Jan 23, 2012 at 0:20
  • Please refer to YAGNI and Over-Engineering in Wikipedia. Commented Jan 23, 2012 at 1:10
3

Use a Dictionary<K,V>, instead, like, mor or less, a pseudocode:

var dic = new Dictionary<int, string> { {1, "PageBla.aspx"}, {2, "Page21231.aspx"}..}

and after in code:

 response.redirect(dic[id_table]);
1

you can use The Factory Design Pattern that doesn't reduce ifs statements but it encapsulate it.

Update

You can combine the other answers with this pattern to get well written code

1

Keep links in a Dctionary:

    Dictionary<int, string> links = 
        new Dictionary<int, string>()
    {
        { 1, "One.aspx" },
        { 2, "Two.aspx" },
        { 3, "Three.aspx" }
    };

and use the like:

    Response.Redirect(links[id_table]);
0

Another option is to store the page in the table, select the page to redirect to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.