devxlogo

Simplifying the Condition-Part of an If statement

Simplifying the Condition-Part of an If statement

When you write an If statement such as:

 If Category = "CM" or Category = "M2" or Category = "P1" or             Category = "ZZ" thenProcessEmployeeEndif

it can be simplified by:

 dim ValidValues as stringValidValues = "CM M2 P1 ZZ"     ' don't forget to insert any ' categories between P1 and ZZif (instr(1, ValidValues, Category)) > 0 thenProcessEmployeeendif

Not only does this version not require you to go on scrollinghorizontally while writing the code but is faster as well, basedon simple tests I conducted using VB3 on a 486DX-66. Note thatI have used a space to separate categories in ValidValues string.You may use any separator, such as semicolon, comma, etc. If youdo not use a separator, the ValidValues string will become “CMM2P1ZZ”and you might get incorrect results and make the ValidValues stringless readable.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist