#1
  1. I-Team User
    ASP Good Citizen (1000 - 1499 posts)

    Join Date
    May 2006
    Location
    Florida
    Posts
    1,219
    Rep Power
    39

    Inefficient coding?


    how would you write this code?
    is there a shorter way?
    Code:
    if rs("depbakery") = "True" then 
    dept="Bakery"
    else if rs("depdairy") = "True" then
    dept="Dairy"
    else if rs("depdeli") = "True" then
    dept="Deli"
    else if rs("depdrug") = "True" then
    dept="Drug/GM"
    else if rs("depgm") = "True" then
    dept="Drug/GM"
    else if rs("depfrozen") = "True" then
    dept="Frozen"
    else if rs("depgrocery") = "True" then
    dept="Grocery"
    else if rs("dephbc") = "True" then
    dept="HBC"
    else if rs("depmeat") = "True" then
    dept="Meat"
    else if rs("depnaturalfoods") = "True" then
    dept="Natural Foods"
    else if rs("depproduce") = "True" then
    dept="Produce"
    else if rs("depseafood") = "True" then
    dept="Seafood"
    else if rs("depfuelcenter") = "True" then
    dept="Fuel Center"
    else if rs("deplobby") = "True" then
    dept="Lobby"
    else if rs("deployalty") = "True" then
    dept="Loyalty"
    else if rs("depoutside") = "True" then
    dept="Outside"
    else
    dept=""
    end if
    end if
    end if
    end if
    end if
    end if
    end if
    end if
    end if
    end if
    end if
    end if
    end if
    end if
    end if
    end if
    Do one thing every day that scares you
    [eleanor roosevelt]

    www.webooktalent.com
    www.demosales.net

    TopGear Photography
    www.topgearphotography.com
    Mopeds 101
    www.Mopeds101.com

  2. #2
  3. No Profile Picture
    Contributing User
    ASP High Scholar (3500 - 3999 posts)

    Join Date
    Jan 2007
    Location
    Indianapolis, USA
    Posts
    3,559
    Rep Power
    700
    I would change the way your form works. Rather than naming each form element with a different name, give them the same name and use a different value:

    Code:
    <input type="checkbox" name="dept" value="Bakery">
    <input type="checkbox" name="dept" value="Dairy">
    <input type="checkbox" name="dept" value="Deli">
    <input type="checkbox" name="dept" value="Drug/GM">
    Then, you can simply grab the value of the department:

    Code:
    strDept = Request.Form("dept")
    If you page is setup differently, let us know. The code you provided is NOT the most efficient bit of code.
    "You'll never be as perfect as BLaaaaaaaaarche."
  4. #3
  5. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2005
    Posts
    63
    Rep Power
    37
    Is this coming out of a form? The code looks like it's retrieving values from a recordset.
  6. #4
  7. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2925
    two plain Arrays will do the job:
    Code:
    Dim arrFieldNames, arrValues, x
    arrFieldNames = Array("depbakery", "depdairy", "depdeli", ...)
    arrValues = Array("Bakery", "Dairy", "Deli", ...)
    For x=0 To UBound(arrFieldNames)
    	If rs(arrFieldNames(x)) = "True" Then
    		dept = arrValues(x)
    		Exit For
    	End If
    Next
    I've put only three due to time limitations, add the rest of the field names
    and corresponding values and it should work just fine.

    Comments on this post

    • nofriends agrees
  8. #5
  9. I-Team User
    ASP Good Citizen (1000 - 1499 posts)

    Join Date
    May 2006
    Location
    Florida
    Posts
    1,219
    Rep Power
    39
    Thanx i was looking for some new ideas.
  10. #6
  11. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2925
    Originally Posted by JustXtreme
    Thanx i was looking for some new ideas.
    you can also use Dictionary, but it has two drawbacks: first, for those not
    familiar with it it looks complicated and second it will still require you to have
    lots of lines, one for each field.

    the advantage of Dictionary will be that you won't have room for mistake: with
    my code above, if you have less items in one of the arrays by mistake, it will
    crash the whole page with nasty error.
    Last edited by Shadow Wizard; March 25th, 2010 at 10:24 AM.

Similar Threads

  1. Senior C# .Net Dev looking for remote job!
    By Sergeby in forum Programmers for Hire
    Replies: 1
    Last Post: March 1st, 2007, 12:51 PM
  2. Coding system for content/objects
    By Draperyfalls in forum ASP Development
    Replies: 9
    Last Post: June 13th, 2006, 07:15 AM
  3. Coding to send value of checkbox to MSaccess database
    By powerkeong in forum ASP Development
    Replies: 1
    Last Post: April 19th, 2006, 06:38 AM
  4. problem in search coding...
    By Deans20 in forum Programming Help
    Replies: 4
    Last Post: May 11th, 2005, 05:05 PM
  5. i don't know how to create coding for "search"
    By aidaolwitz in forum Microsoft Access Help
    Replies: 5
    Last Post: January 12th, 2005, 02:39 PM

IMN logo majestic logo threadwatch logo seochat tools logo