Skip to content
March 29, 2008 / kiranpatils

Use Predicate for complex Conditional Block[if..else]

yesterday i had completed coding of my block which has too may if..else blocks….which goes too approx. 20-30 lines… it does simple logic as under:

1. Gets Value from Querystring

2.and based on value redirect my page to appropriate page..

for doing the above thing i had written following code[looks like garbage]..

———————————————————————————————————

private void CacheAndTransfertoNext(){

string EntryPoint = Request.QueryString[“EntryPoint”];

if (!String.IsNullOrEmpty(EntryPoint)){

if (_emppresnter.SaveEmployee()){

//Redirect on Next Page Based on EntryPoint Value

if (EntryPoint == “1”){

Response.Redirect(“Page1.asp”);}

else if (EntryPoint == “2”){

Response.Redirect(“page2.asp”);}

else if (EntryPoint == “3”){

Response.Redirect(“page3.asp”);}

else if (EntryPoint == “4”){

Response.Redirect(“page4.asp”);}

else if (EntryPoint == “5”){

Response.Redirect(“page5.asp”);}

else if (EntryPoint == “6”){

Response.Redirect(“page6.asp”);}

else if (EntryPoint == “7”){

Response.Redirect(“page7.asp”);}

}

else

{

Response.Redirect(“Default.aspx”);}

———————————————————————————————————

 i thought that can’t i make it smaller..i got it using predicate..i had accompilished the task in four lines…:)

Here it is:

1. I need to create some classes[which are self explanatory].

URLList.cs

using System;

using System.Collections.Generic;

using System.Text;

using System.Configuration;

public class URLList{

private List<MyUrls> _bookerurl = new List<MyUrls>();public List<MyUrls> BookerUrlList{

get { return _bookerurl; }

set { _bookerurl = value; }}

public URLList(){

_bookerurl.Add(new BookerURL(“1”,“page1.asp”));_bookerurl.Add(new BookerURL(“2”,“page2.asp”));__bookerurl.Add(

new BookerURL(“3”,“page3.asp”));__bookerurl.Add(new BookerURL(“4”,“page4.asp”));__bookerurl.Add(

new BookerURL(“5”,“page5.asp”));__bookerurl.Add(new BookerURL(“6”,“page6.asp”));}

}

using System;

using System.Collections.Generic;

using System.Text;

public class MyUrls

{

public MyUrls(){

}

public MyUrls(string key, string value){

_key = key;

_value = value;

}

private string _key;public string Key{

get { return _key; }

set { _key = value; }}

private string _value;public string Value{

get { return _value; }

set { _value = value; }}

}

——————- Now Lets see my Function now..

private void CacheAndTransfertoNext()

{

string EntryPoint = Request.QueryString[“EntryPoint”];if (!String.IsNullOrEmpty(EntryPoint)){

if (_emppresnter.SaveEmployee()){

 URLList urlist = new URLList();
 MyUrls _nexturl = urlist.BookerUrlList.Find(delegate(MyUrls url) { return url.Key == EntryPoint; });                                        
string nextpageurl = (_nexturl != null) ? _nexturl.Value :  “Default.aspx”;

Response.Redirect(nextpageurl);

}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: