Need your suggestions on how to deal more efficiently with unknown string lengths.
The code below takes a string array with an unknown number of members and chops it up based on <space>.
What I do now is I make an assumption on how many members there could possibly be and have these endless if statements. If I plan for the worst case, I would have 33 of these if try catch blocks.
The code works, but this is just one little piece of functionality, overall there would more than 100 if statements in 500 lines of code.
I have a suspicion things could be improved by using List functionality, but I have yet to find a nice worked out example that I can use to understand how it works. The abstract information from MSDN is no help to me.
What are you ultimately hoping to do with these isstate# variables? When you perform the istates.Split(' ') action, you'll end up with a string array with as many items as there are numbers:
How does one define istatesArr outside of the if statement?
Thanks for your input. This functionality is related to United States Interstates. Each state has a number of InterStates and the overall purpose is to let a user pick which Interstates they are close to. And then use the ones they pick later on in the process
for display and selection purposes. The code from my original post is what needs to happen after the interstates have been selected. I am working towards presenting a form with 33 checkboxes with checkmarks and the interstate behind the checkmark box.
Table InterStates consists out of State as the key (VA, PA, MD, etc) and 33 columns labeled IS1 - IS33.
For instance Maryland has interstates 68 70 81 83 95 and 97, that means that IS1-IS7 are populated with those numbers. Delaware has only three interstates 95 295 and 495, so IS1-IS3 are populated.
Note that I-95 appears in both MD and Deleware, but at different columns.
The values in the string can now be used to query InterStates and find out which column the interstate is in. I don't know yet how to do that but I am back to my original dilemma. I need to query the table n times (n=length of the array)
db.QueryValue("SELECT IS* FROM InterStates WHERE StateID=@0 AND IS*=@1", state, istatesArr[0]);
I am showing the wild card for the column names, just for to illustrate the question here. Do I need to feed the query each individual array member or can I feed it the array in one fell swoop?
I'm a little distracted at the moment to discuss the rest of your post, so I'll get to it in a little bit. But for the time being, check out this post on how to establish a global variable in webmatrix that can be used anywhere in the application:
It seems to me that your database design needs reviewing. You should have a table for all interstates, and a table for all states. Then you should have a bridging table that has an ID, a StateID and an InterStateID. The interstates that belong to a specific
state would go into this. Then you should present the user with a list of states to select from, and based on that selection, you pick the interstates using the bridging table. They should then choose the interstates they are close to, and you record that
information in another table that holds the user id and the interstate id.
Incidentally, how did you arrive at I33 instead of say I32 or I34? If the reason is that you guessed this is a high enough figure for the time being that you should not need another column, that's the warning flag that the database design is not robust or
future proof and it needs rethinking.
You are probably right that my design is not optimal, but it works. Specially now considering that you have shown me how to find an unknown column name.
So what happens if they build another Interstate in NY? Or redesignate a track as an interstate? How many changes would you have to make to database schema or code to accommodate it? If the answer is even one, you should definitely rethink.
Wouldn't your suggestion be well served by your CascadingDropDown list.
Two tables, States and InterStates. States table populated with about 50 states, Secondary table populated with FK matching interStates, less than 30 Interstates per state. User picks the State, code populates "Select Interstate". User picks the Interstate,
one result, no IF statements and pretty much all on the same short page. When a new State or Interstate arrives its only one update to the table and no code changes.
wavemaster
Participant
1279 Points
1124 Posts
need suggestions for code improvements (too many ifs) - unknown string lengths
Jan 30, 2013 03:00 PM|LINK
Need your suggestions on how to deal more efficiently with unknown string lengths.
The code below takes a string array with an unknown number of members and chops it up based on <space>.
What I do now is I make an assumption on how many members there could possibly be and have these endless if statements. If I plan for the worst case, I would have 33 of these if try catch blocks.
The code works, but this is just one little piece of functionality, overall there would more than 100 if statements in 500 lines of code.
I have a suspicion things could be improved by using List functionality, but I have yet to find a nice worked out example that I can use to understand how it works. The abstract information from MSDN is no help to me.
@{ bool skip = false; var istate1 = 0; var istate2 = 0; var istate3 = 0; var istate4 = 0; string istates = "95 83 97 695 "; if (istates.Length > 0) { try { istate1 = int.Parse(istates.Split(' ')[0]); } catch(System.FormatException) { skip = true; } if (!skip) { try { istate2 = int.Parse(istates.Split(' ')[1]); } catch(System.FormatException) { skip = true; } if (!skip) { try { istate3 = int.Parse(istates.Split(' ')[2]); } catch (System.FormatException) { skip = true; } if (!skip) { try { istate4 = int.Parse(istates.Split(' ')[3]); } catch(System.FormatException) { skip = true; } }}} } }AceCorban
Star
12318 Points
2269 Posts
Re: need suggestions for code improvements (too many ifs) - unknown string lengths
Jan 30, 2013 03:23 PM|LINK
What are you ultimately hoping to do with these isstate# variables? When you perform the istates.Split(' ') action, you'll end up with a string array with as many items as there are numbers:
string istates = "95 83 97 695"; String [] istatesArr = istates.Split(' '); //istatesArr.Length equals 4wavemaster
Participant
1279 Points
1124 Posts
Re: need suggestions for code improvements (too many ifs) - unknown string lengths
Jan 30, 2013 05:29 PM|LINK
Nice!
@{ string istates = "95 83 97 695"; if (istates.Length > 0 ) { String [] istatesArr = istates.Split(' '); } }How does one define istatesArr outside of the if statement?
Thanks for your input. This functionality is related to United States Interstates. Each state has a number of InterStates and the overall purpose is to let a user pick which Interstates they are close to. And then use the ones they pick later on in the process for display and selection purposes. The code from my original post is what needs to happen after the interstates have been selected. I am working towards presenting a form with 33 checkboxes with checkmarks and the interstate behind the checkmark box.
Table InterStates consists out of State as the key (VA, PA, MD, etc) and 33 columns labeled IS1 - IS33.
For instance Maryland has interstates 68 70 81 83 95 and 97, that means that IS1-IS7 are populated with those numbers. Delaware has only three interstates 95 295 and 495, so IS1-IS3 are populated.
Note that I-95 appears in both MD and Deleware, but at different columns.
The values in the string can now be used to query InterStates and find out which column the interstate is in. I don't know yet how to do that but I am back to my original dilemma. I need to query the table n times (n=length of the array)
db.QueryValue("SELECT IS* FROM InterStates WHERE StateID=@0 AND IS*=@1", state, istatesArr[0]);
I am showing the wild card for the column names, just for to illustrate the question here. Do I need to feed the query each individual array member or can I feed it the array in one fell swoop?
AceCorban
Star
12318 Points
2269 Posts
Re: need suggestions for code improvements (too many ifs) - unknown string lengths
Jan 30, 2013 05:42 PM|LINK
I'm a little distracted at the moment to discuss the rest of your post, so I'll get to it in a little bit. But for the time being, check out this post on how to establish a global variable in webmatrix that can be used anywhere in the application:
http://forums.asp.net/t/1704045.aspx/1
or if you just want to establish variables with full page scope:
http://blogs.msdn.com/b/rafalkwiek/archive/2010/10/25/creating-web-pages-using-webmatrix-tutorial-4-dynamic-content-basics-variables.aspx
wavemaster
Participant
1279 Points
1124 Posts
Re: need suggestions for code improvements (too many ifs) - unknown string lengths
Jan 30, 2013 08:25 PM|LINK
I thought I knew how to declare a page wide variable, but apparently not.
@{ string[] istatesArr; string istates = "95 83 97 695"; if (istates.Length > 0 ) { istatesArr = istates.Split(' '); } } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title></title> </head> <body> @istatesArr[0]<br /> @istatesArr[1]<br /> @istatesArr[2]<br /> @istatesArr[3]<br /> </body> </html>The members do not exist in the current context.
Making the declaration in AppStart does not work either.
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: need suggestions for code improvements (too many ifs) - unknown string lengths
Feb 03, 2013 08:52 AM|LINK
It seems to me that your database design needs reviewing. You should have a table for all interstates, and a table for all states. Then you should have a bridging table that has an ID, a StateID and an InterStateID. The interstates that belong to a specific state would go into this. Then you should present the user with a list of states to select from, and based on that selection, you pick the interstates using the bridging table. They should then choose the interstates they are close to, and you record that information in another table that holds the user id and the interstate id.
Incidentally, how did you arrive at I33 instead of say I32 or I34? If the reason is that you guessed this is a high enough figure for the time being that you should not need another column, that's the warning flag that the database design is not robust or future proof and it needs rethinking.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
wavemaster
Participant
1279 Points
1124 Posts
Re: need suggestions for code improvements (too many ifs) - unknown string lengths
Feb 03, 2013 01:26 PM|LINK
You are probably right that my design is not optimal, but it works. Specially now considering that you have shown me how to find an unknown column name.
That has to do with the number of interstates per state.
NY has 33 interstates, CA 26
Thanks.
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: need suggestions for code improvements (too many ifs) - unknown string lengths
Feb 03, 2013 08:39 PM|LINK
So what happens if they build another Interstate in NY? Or redesignate a track as an interstate? How many changes would you have to make to database schema or code to accommodate it? If the answer is even one, you should definitely rethink.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
wavemaster
Participant
1279 Points
1124 Posts
Re: need suggestions for code improvements (too many ifs) - unknown string lengths
Feb 04, 2013 04:51 AM|LINK
Eh... they repurposed a race track in Belgium, most of the times at least. That is the last time that ever happened!
Agree, I would have to go through and fix a few things.
NY has no money to build anything anyway.
DMT20601
Member
86 Points
197 Posts
Re: need suggestions for code improvements (too many ifs) - unknown string lengths
Feb 05, 2013 08:51 PM|LINK
Wouldn't your suggestion be well served by your CascadingDropDown list.
Two tables, States and InterStates. States table populated with about 50 states, Secondary table populated with FK matching interStates, less than 30 Interstates per state. User picks the State, code populates "Select Interstate". User picks the Interstate, one result, no IF statements and pretty much all on the same short page. When a new State or Interstate arrives its only one update to the table and no code changes.
Just a thought.
http://www.mikesdotnetting.com/Article/196/WebMatrix-jQuery-Cascading-Dropdown-Lists