Click to See Complete Forum and Search --> : "Contest" for elegancy.


Marraco
May 30th, 2008, 02:42 PM
Just for the glory:

¿Is there a more elegant way to rewrite this ugly code?
If Cambios And CambioArchPalmer Then
TextoCambios &= String1
End If
If Cambios And CambioArchSPI Then
TextoCambios &= String2
End If
If Cambios And CambioProcesar Then
TextoCambios &= String3
End If
If Cambios And CambioProcesarPalmer Then
TextoCambios &= String4
End If
If Cambios And CambioProcesarSPI Then
TextoCambios &= String5
End If
If Cambios And CambioLatitud Then
TextoCambios &= String6
End If
If Cambios And CambioLongitud Then
TextoCambios &= String7
End If
If Cambios And CambioPathEjecutablePalmer Then
TextoCambios &= String8
End If
If Cambios And CambioPathEjecutableSPI Then
TextoCambios &= String9
End If
The involved variables are:
Public Cambios As Integer

Public TextoCambios As String

'The constants CambioXXX are binary flags, all different
'Each one have only one bit different of zero
Public Const CambioNinguno As Integer = 0
Public Const CambioArchPalmer As Integer = 1 '1000000000000000
Public Const CambioArchSPI As Integer = 2 '0100000000000000
Public Const CambioProcesar As Integer = 4 '0010000000000000
Public Const CambioProcesarPalmer As Integer = 8
Public Const CambioProcesarSPI As Integer = 16
Public Const CambioLatitud As Integer = 32
Public Const CambioLongitud As Integer = 64
Public Const CambioPathEjecutablePalmer As Integer = 128
Public Const CambioPathEjecutableSPI As Integer = 256
Public Const CambioNºDePuesto As Integer = 512
Public Const CambioIsNothing As Integer = 1024Cambios can have any integer value.
Intention of code is to detect if Cambios have the same bit equal than each CambiosXXX variable; In that case, a text is added to TextoCambios string.

For example, If Cambios = 3 ('1100000000000000)
then, since:
(Cambios And CambioArchPalmer) is true [1100000000000000 And 1000000000000000 = 1000000000000000] <> False
(Cambios And CambioArchSPI) is true [1100000000000000 And 0100000000000000 = 0100000000000000] <> False

must be:
TextoCambios &= String1 & String2