Eliminando o IF quando possível

Eliminando o IF quando possível no Visual Basic

Dicas de Visual Basic
Dicas de Visual Basic

 

Se você atribui true ou false para uma variável (ou propriedade), após testar certas condições, poderia fazê-lo sem o IF. Veja:

If (age > 18 and sex = “M”) and (NecessitaSeContigente = true ) Then ServicoMilitar = true

Pode substituir por:

ServicoMilitar = (age > 18 and sex = “M”) and (NecessitaSeContigente)

Outro exemplo:

IF (age > 25 and Category = “M1”) or (age > 35 and Category = “C1”) or _

(Age > 45 and Category = “P1”) then ExecuteDemissao

Poderia ser:

Dim condicao as Integer ‘boolean

condição = (age > 25 and Category = “M1”) or (age > 35 and Category = “C1”)_ or (Age > 45 and Category = “P1”)

If condicao Then ExecuteDemissao

Posts Similares