Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a neater way of doing this if/else?

Status
Not open for further replies.

JGKWORK

IS-IT--Management
Joined
Apr 1, 2003
Messages
342
Location
GB
Not doing well today! Can anyone show me a neater way of doing this:


if PlantType <> "" then


if PlantType = 6 then
CatagoryTitle = "Cranes"
elseif PlantType = 7 then
CatagoryTitle = "Excavators"
elseif PlantType = 8 then
CatagoryTitle = "Dumpers"
elseif PlantType = 9 then
CatagoryTitle = "Telehandlers"
elseif PlantType = 10 then
CatagoryTitle = "Compressors"
elseif PlantType = 11 then
CatagoryTitle = "Compressors"
elseif PlantType = 12 then
CatagoryTitle = "Compressors"
elseif PlantType = 13 then
CatagoryTitle = "Compressors"

end if

Maybe with a loop or some thing? Thanks.
 
try a switch block:
Code:
switch(PlantType) {
   case 6:
      CatagoryTitle = "Cranes"
   case 7:
      CatagoryTitle = "Excavators"
   case 8:
      CatagoryTitle = "Dumpers"
   case 9:
      CatagoryTitle = "Telehandlers"
   default:
      CatagoryTitle = "Compressors"
}

-kaht

banghead.gif
 
hmm.... wait, looks like you're using vbscript instead of javascript.... dunno what the vbscript equivalent would be.

-kaht

banghead.gif
 
The Javascript version would be this:
Code:
switch(PlantType) {
   case 6:
      CatagoryTitle = "Cranes"
      break; 
  case 7:
      CatagoryTitle = "Excavators"
      break;
   case 8:
      CatagoryTitle = "Dumpers"
      break;
   case 9:
      CatagoryTitle = "Telehandlers"
      break;
   default:
      CatagoryTitle = "Compressors"
}

And the VBScript version would be this:
Code:
Select Case PlantType
  Case 6
    CatagoryTitle = "Cranes"
  Case 7
    CatagoryTitle = "Excavators"
  Case 8
    CatagoryTitle = "Dumpers"
  Case 9
    CatagoryTitle = "Telehandlers"
  Case Else
    CatagoryTitle = "Compressors"
End Select

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Thanks alot, just out of interest would there be a way of using a loop here?
 
doh, I forgot the breaks

I remember using select/case back when I coded in QBasic

-kaht

banghead.gif
 
As you've noticed, the switch statement doesn't give you any "gain". In fact, it requires more lines of code than an "if/else" statement. Back in the days when C was becoming popular, there was heated discussion about which one was faster and as I recall, "if" beat out "switch" by a nose.

There's always a better way. The fun is trying to find it!
 
Dump the constant information into an array indexed by PlantType. When Title is required, ask the array. In JavaScript something like --
Code:
var aTitle = new Array();
aTitle[6] = "Cranes";
aTitle[7] = "Excavators";
aTitle[8] = "Dumpers";
aTitle[9] = "Telehandlers";
aTitle[10] = "Compressors";
aTitle[11] = aTitle[12] = aTitle=[13] = aTitle[10];

CategoryTitle is available using PlantType --
Code:
CategoryTitle = aTitle[PlantType];
 
cheers, that'll do it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top