Results 1 to 27 of 27

Thread: [RESOLVED] Avoiding long painful conditionals

  1. #1
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Resolved [RESOLVED] Avoiding long painful conditionals

    Hi guys,
    I have to test for different conditions something like:
    Code:
     If IsFontColour = True And IsFontSize = True And IsWidth = True And IsHeight = True And IsMarginL = True _
                              And IsMarginR = True And IsSpeed = True And IsEasing = True Then
    
                       LongStr = Fcolor & Fsize & width & height & marginL & marginR & speed & easing 
    
             ElseIf IsFontColour = False And IsFontSize = True And IsWidth = True And IsHeight = True And IsMarginL = True _
                              And IsMarginR = True And IsSpeed = True And IsEasing = True Then
    
               LongStr =  Fsize & width & height & marginL & marginR & speed & easing 
            
             ElseIf IsFontColour = True And IsFontSize = False And IsWidth = True And IsHeight = True And IsMarginL = True _
                              And IsMarginR = True And IsSpeed = True And IsEasing = True Then
               LongStr = Fcolor & width & height & marginL & marginR & speed & easing 
    
    
    End If

    Is there a way to write this more efficiently and quicker? I have to test for all possibilities, but i've been there before and
    I know it's very long, painful and easy to get confused and lost in the spaghetti.Is there an easy way to do this?

    Any help is welcome,
    Mike
    Thanks

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,984

    Re: Avoiding long painful conditionals

    Well I don't know whether it's an improvement but here's one quirky way to do it,

    vb.net Code:
    1. Dim a1 As Boolean = False 'obviously these values would normally be set in the program
    2.         Dim a2 As Boolean = False
    3.         Dim a3 As Boolean = False
    4.         Dim a4 As Boolean = True
    5.         Dim a5 As Boolean = True
    6.         Dim a6 As Boolean = True
    7.  
    8.         Dim c = Tuple.Create(a1, a2, a3, a4, a5, a6) ' Tuples! Huh! What are they good for?
    9.  
    10.         If c.Equals(Tuple.Create(False, False, False, True, True, True)) Then
    11.             MsgBox("Yay!")
    12.         ElseIf c.Equals(Tuple.Create(False, True, False, True, False, True)) Then
    13.             MsgBox("Nay")
    14.         Else
    15.             MsgBox("Confused Yet?")
    16.  
    17.         End If

  3. #3
    Lively Member
    Join Date
    May 10
    Posts
    103

    Re: Avoiding long painful conditionals

    Well, one way to do what I think you are doing in your example is to loop through each boolean value, check if it is true, and if it is true add the cooresponding value to the LongStr.

    Example;

    Code:
    Dim booleanArray As Boolean() = {IsFontColour, IsFontSize, IsWidth, IsHeight, IsMarginL, IsMarginR, IsSpeed, IsEasing}
    Dim objectArray As Object() = {Fcolor, Fsize, Width, Height, marginL, marginR, speed, easing}
    Dim LongStr As String = Nothing
    
    For i As Integer = 0 To booleanArray.Length - 1
       If booleanArray(i) = True Then
           LongStr += objectArray(i).ToString
       End If
    Next
    
    MsgBox(LongStr)

  4. #4
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: Avoiding long painful conditionals

    Quote Originally Posted by MotoX646 View Post
    Well, one way to do what I think you are doing in your example is to loop through each boolean value, check if it is true, and if it is true add the cooresponding value to the LongStr.

    Example;

    Code:
    Dim booleanArray As Boolean() = {IsFontColour, IsFontSize, IsWidth, IsHeight, IsMarginL, IsMarginR, IsSpeed, IsEasing}
    Dim objectArray As Object() = {Fcolor, Fsize, Width, Height, marginL, marginR, speed, easing}
    Dim LongStr As String = Nothing
    
    For i As Integer = 0 To booleanArray.Length - 1
       If booleanArray(i) = True Then
           LongStr += objectArray(i).ToString
       End If
    Next
    
    MsgBox(LongStr)
    Hi,
    I tried but cannot get the LongStr printed out. I put in a MsgBox to test and nothing comes out. I even deleted the "objectArray(i).ToString" and wrote "Something is true". Plain string but still the msgbox is empty.

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,984

    Re: Avoiding long painful conditionals

    Quote Originally Posted by MotoX646 View Post
    Code:
    Dim booleanArray As Boolean() = {IsFontColour, IsFontSize, IsWidth, IsHeight, IsMarginL, IsMarginR, IsSpeed, IsEasing}
    Dim objectArray As Object() = {Fcolor, Fsize, Width, Height, marginL, marginR, speed, easing}
    Dim LongStr As String = Nothing
    
    For i As Integer = 0 To booleanArray.Length - 1
       If booleanArray(i) = True Then
           LongStr += objectArray(i).ToString
       End If
    Next
    
    MsgBox(LongStr)
    Huh? Doesn't that give exactly the same result for (forgive the shorthand) aT bT cF, aF bT cT and aT bF cT?

  6. #6
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 11
    Location
    WNY
    Posts
    429

    Re: Avoiding long painful conditionals

    Quote Originally Posted by dunfiddlin View Post
    Well I don't know whether it's an improvement but here's one quirky way to do it,

    vb.net Code:
    1. Dim a1 As Boolean = False 'obviously these values would normally be set in the program
    2.         Dim a2 As Boolean = False
    3.         Dim a3 As Boolean = False
    4.         Dim a4 As Boolean = True
    5.         Dim a5 As Boolean = True
    6.         Dim a6 As Boolean = True
    7.  
    8.         Dim c = Tuple.Create(a1, a2, a3, a4, a5, a6) ' Tuples! Huh! What are they good for?
    9.  
    10.         If c.Equals(Tuple.Create(False, False, False, True, True, True)) Then
    11.             MsgBox("Yay!")
    12.         ElseIf c.Equals(Tuple.Create(False, True, False, True, False, True)) Then
    13.             MsgBox("Nay")
    14.         Else
    15.             MsgBox("Confused Yet?")
    16.  
    17.         End If
    Wow, what a great solution! I had no idea this Tuple object existed until now. It's a great one to because I can see this being extremely useful for forms that display data for editing, either pulled from a database or some sort of data source. With this it would be soo much easier to determine if a form is dirty and has unsaved changes.

    Are there any perfomance improvements from using
    Code:
    Tuple.Create(True, True, True).Equals(Tuple.Create(True, True, True))
    over
    Code:
    If (True And True And True) Then
    ?
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  7. #7
    Lively Member
    Join Date
    May 10
    Posts
    103

    Re: Avoiding long painful conditionals

    Quote Originally Posted by MikeSpider View Post
    Hi,
    I tried but cannot get the LongStr printed out. I put in a MsgBox to test and nothing comes out. I even deleted the "objectArray(i).ToString" and wrote "Something is true". Plain string but still the msgbox is empty.
    Your booleans are not declared correctly then... So the "If booleanArray(i) = True Then" is always False.

    This works for me:

    Code:
            Dim IsFontColour As Boolean = True
            Dim IsFontSize As Boolean = True
            Dim IsWidth As Boolean = True
            Dim IsHeight As Boolean = True
            Dim IsMarginL As Boolean = False
            Dim IsMarginR As Boolean = True
            Dim IsSpeed As Boolean = True
            Dim IsEasing As Boolean = True
    
            Dim Fcolor As String = "Red"
            Dim Fsize As Integer = "777"
            Dim Width As Integer = "512"
            Dim Height As Integer = "128"
            Dim marginL As Integer = "123"
            Dim marginR As String = "456"
            Dim speed As Single = "1.234"
            Dim easing As Single = "5.678"
    
            Dim booleanArray As Boolean() = {IsFontColour, IsFontSize, IsWidth, IsHeight, IsMarginL, IsMarginR, IsSpeed, IsEasing}
            Dim objectArray As Object() = {Fcolor, Fsize, Width, Height, marginL, marginR, speed, easing}
            Dim LongStr As String = ""
    
            For i As Integer = 0 To booleanArray.Length - 1
                If booleanArray(i) = True Then
                    LongStr += objectArray(i).ToString
                End If
            Next
    
            MsgBox(LongStr)
    Last edited by MotoX646; Sep 2nd, 2012 at 11:08 AM.

  8. #8
    Lively Member
    Join Date
    May 10
    Posts
    103

    Re: Avoiding long painful conditionals

    Quote Originally Posted by dunfiddlin View Post
    Huh? Doesn't that give exactly the same result for (forgive the shorthand) aT bT cF, aF bT cT and aT bF cT?
    No, it doesn't give the same result.

    All he (and my code) is doing is building a string of data based on what is true or false. If a boolean is false, it just doesn't add the data to the string.

    Here is another way to do the same thing...

    Code:
            Dim IsFontColour As Boolean = True
            Dim IsFontSize As Boolean = True
            Dim IsWidth As Boolean = True
            Dim IsHeight As Boolean = True
            Dim IsMarginL As Boolean = False
            Dim IsMarginR As Boolean = True
            Dim IsSpeed As Boolean = True
            Dim IsEasing As Boolean = True
            Dim Fcolor As String = "Red"
            Dim Fsize As Integer = "777"
            Dim Width As Integer = "512"
            Dim Height As Integer = "128"
            Dim marginL As Integer = "123"
            Dim marginR As String = "456"
            Dim speed As Single = "1.234"
            Dim easing As Single = "5.678"
    
            Dim LongStr As New System.Text.StringBuilder
    
            If IsFontColour Then LongStr.Append(Fcolor.ToString)
            If IsFontSize Then LongStr.Append(Fsize.ToString)
            If IsWidth Then LongStr.Append(Width.ToString)
            If IsHeight Then LongStr.Append(Height.ToString)
            If IsMarginL Then LongStr.Append(marginL.ToString)
            If IsMarginR Then LongStr.Append(marginR.ToString)
            If IsSpeed Then LongStr.Append(speed.ToString)
            If IsEasing Then LongStr.Append(easing.ToString)
    
            MsgBox(LongStr.ToString)
    Last edited by MotoX646; Sep 2nd, 2012 at 11:27 AM.

  9. #9
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: Avoiding long painful conditionals

    I'm trying to use the For loop solution but I'm getting an error "Conversion from string "" to type 'Integer' is not valid."

    Code:
     Dim BooleanArray As Boolean() = {IsWidth, IsHeight, IsMarginL, IsMarginR, IsFontColour, IsFontSize, IsSpeed, IsEasing, IsCallBack, IsReset}
        Dim PropertyArray As Object() = {prpt1, prpt2, prpt3, prpt4, prpt5, prpt6, speed, easing, callBackF, resetId}
       
        
        Dim AnimMainStr As String = ""
    
     Private Sub Button1_Click(.....)
    ....
    
     If fontColor <> "" Then
                BooleanArray(IsFontColour) = True
                PropertyArray(prpt1) = "fontColour:'" & fontColor & "'"   ---->> the error is in this line
            End If
    
    ...
     For i As Integer = 0 To BooleanArray.Length - 1
                If BooleanArray(i) = True Then
                    AnimMainStr += "$('#" & triggerId & "').click(function(){" & vbCrLf & _
                                        vbTab & " $('#" & targetId & "').animate({ " & vbCrLf & _
                                         vbTab & vbTab & PropertyArray(i).ToString & "," & vbCrLf & _
                                         vbTab & vbTab & "}) " & vbCrLf & _
                                         vbTab & ";});"
    
    
    ...
    Anyways, I will experiment with the other sugestions,
    Thanks,
    Mike

  10. #10
    Lively Member
    Join Date
    May 10
    Posts
    103

    Re: Avoiding long painful conditionals

    Quote Originally Posted by MikeSpider View Post
    I'm trying to use the For loop solution but I'm getting an error "Conversion from string "" to type 'Integer' is not valid."

    Code:
    BooleanArray(IsFontColour) = True
    PropertyArray(prpt1) = "fontColour:'" & fontColor & "'"   ---->> the error is in this line

    Arrays only accept integers for their index position, not strings. The index starts at 0 and counts up.

    So to change the value of "IsFontColour" in "BooleanArray", you need to provide the correct index number.

    Code:
    BooleanArray(4) = True
    To change "prpt1" in "PropertyArray", do this:

    Code:
    PropertyArray(0) = "fontColour:'" & fontColor & "'"
    Last edited by MotoX646; Sep 2nd, 2012 at 12:48 PM.

  11. #11
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: Avoiding long painful conditionals

    Yes you are right.
    Thanks everybody for the suggestions and all the help
    Mike

  12. #12
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: [RESOLVED] Avoiding long painful conditionals

    I still have a problem.

    Sorry for marking it resolved so early.

    The conditionals are not working as I want to.

    The values appear empty but still appear.

    Code:
     If marginL <> "" Then
                IsMarginL = True
                prpt5 = "marginLeft:" & marginL
            Else
        IsMarginL = False
            End If
    
     If IsMarginL = True Then
                AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt5)
            End If

    although it is false it still appends the word "marginLeft:" although with no value.
    It should not append anything at all bc is set to false.

    I dont know what i'm doing wrong here.

    Thanks,
    Mike

  13. #13
    Lively Member
    Join Date
    May 10
    Posts
    103

    Re: [RESOLVED] Avoiding long painful conditionals

    Quote Originally Posted by MikeSpider View Post

    Code:
    If marginL <> "" Then
        IsMarginL = True
        prpt5 = "marginLeft:" & marginL
    Else
        IsMarginL = False
    End If
    
    If IsMarginL = True Then
        AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt5)
    End If

    although it is false it still appends the word "marginLeft:" although with no value.
    It should not append anything at all bc is set to false.

    I dont know what i'm doing wrong here.
    What is the value of marginL when you get the error?

    Because, obviously, if it appends the word "marginLeft:" then IsMarginL must be true, which means marginL must contain text.

    If marginL is a string, I would try this:

    Code:
    If Not marginL = "" Then
        IsMarginL = True
        prpt5 = "marginLeft:" & marginL
    Else
        IsMarginL = False
    End If

  14. #14
    Fanatic Member
    Join Date
    Jan 06
    Posts
    515

    Re: [RESOLVED] Avoiding long painful conditionals

    One thing that will save you a bit of typing and be less odd is to not explicitly state boolean values in your test:
    Use:
    Code:
    If IsFontColour Then
    rather than:
    Code:
    If IsFontColour = True Then
    The latter sounds awkward - e.g., you don't ask "is it raining equals true?", you ask "is it raining?".
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  15. #15
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 10
    Location
    Denmark
    Posts
    521

    Re: [RESOLVED] Avoiding long painful conditionals

    In situations such as this one, I usually prefer to use good old-fashioned bit-fields, as examplified by:

    VB.NET Code:
    1. <FlagsAttribute()>
    2. Public Enum MyBitfield
    3.  
    4.     IsFontColour = &H1 'Value gets doubled for each field so each one occupy a single bit of a 32 bit number (ie. max 32 entries).
    5.     IsFontSize = &H2
    6.     IsWidth = &H4
    7.     IsHeight = &H8
    8.     IsMarginL = &H10
    9.     IsMarginR = &H20
    10.     IsSpeed = &H40
    11.     IsEasing = &H80
    12.  
    13. End Enum
    14.  
    15. Public Class MyBitClass
    16.  
    17.     Public Function TestBitfields(ByVal sBitValue As MyBitfield) As Boolean
    18.  
    19.         'Set test to hold True for IsFontSize, IsSpeed and IsHeight and False for all others fields.
    20.         Dim test As MyBitfield = MyBitfield.IsFontSize Or MyBitfield.IsSpeed Or MyBitfield.IsHeight
    21.  
    22.         If (sBitValue And test) = test Then 'Simultaneous test for 3 True and 5 False booleans
    23.  
    24.             '...
    25.  
    26.         End If
    27.  
    28.         'Can also be used as:
    29.         If (sBitValue And &HCB) <> 0 Then
    30.  
    31.             'I get to here, if one of IsEasing, IsSpeed, IsHeight, IsFontSize or IsFontColour is True.
    32.  
    33.         End If
    34.  
    35.         Return test = MyBitfield.IsMarginL
    36.  
    37.     End Function
    38.  
    39. End Class
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  16. #16
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: [RESOLVED] Avoiding long painful conditionals

    Quote Originally Posted by MotoX646 View Post
    What is the value of marginL when you get the error?

    Because, obviously, if it appends the word "marginLeft:" then IsMarginL must be true, which means marginL must contain text.

    If marginL is a string, I would try this:

    Code:
    If Not marginL = "" Then
        IsMarginL = True
        prpt5 = "marginLeft:" & marginL
    Else
        IsMarginL = False
    End If
    I'm not getting any error. Just not the expected results.
    And Yes it's a String datatype.
    I tried your example still the same.
    I dont understand Why is not detecting the boolean at all.
    Code:
    
     'setting properties vars
            If txtResetId.Text <> "" Then
                IsReset = True
            Else
                IsReset = False
            End If
            If fontColor <> "" Then
                IsFontColour = True
                prpt1 = "fontColour:'" & fontColor & "'"
            Else
                IsFontColour = False
            End If
    
            Dim FSize As String = CStr(fontSize)
            If FSize <> "" Then
                IsFontSize = True
                prpt2 = "fontSize:'" & fontSize & "px'"
            Else
                IsFontSize = False
            End If
    
            If Not widthAnim = "" Then
                IsWidth = True
                prpt3 = "width:" & widthAnim
            Else
                IsWidth = False
            End If
    
            'If widthAnim <> "" Then
            '    IsWidth = True
            '    prpt3 = "width:" & widthAnim
            'Else
            '    IsWidth = False
            'End If
            If heightAnim <> "" Then
                IsHeight = True
                prpt4 = "height:" & heightAnim
            Else
                IsHeight = False
            End If
            If marginL <> "" Then
                IsMarginL = True
                prpt5 = "marginLeft:" & marginL
            Else
                IsMarginL = False
            End If
            If marginR <> "" Then
                IsMarginR = True
                prpt6 = "marginRight:" & marginR
            Else
                IsMarginR = False
            End If
    
            'setting speed vars
            If rbSlow.Checked = True Then
                speed = "'" & rbSlow.Text & "'"
                IsSpeed = True
            ElseIf rbFast.Checked = True Then
                speed = "'" & rbFast.Text & "'"
                IsSpeed = True
            End If
            If txtSpeed.Text <> "" Then
                speed = txtSpeed.Text
                IsSpeed = True
            End If
    
            'Setting Easing vars
            If rbSwing.Checked = True Then
                easing = "'" & rbSwing.Text & "'"
                IsEasing = True
            ElseIf rbLinear.Checked = True Then
                easing = "'" & rbLinear.Text & "'"
                IsEasing = True
            End If
    
            'Setting callback function vars
            If cbCallBack.CheckState = CheckState.Checked Then
                callBackF = txtCallBack.Text
                IsCallBack = True
            End If
    
            Dim firstParam As String = "$('#" & triggerId & "').click(function(){" & vbCrLf & _
                                    vbTab & " $('#" & targetId & "').animate({ " 
            AnimMainStr.Append(firstParam.ToString)
    
            
    
            If IsWidth = True Then
                AnimMainStr.Append(vbCrLf & vbTab & vbTab & prpt3)
            End If
            If IsHeight = True Then
                AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt4)
            End If
            If IsMarginL = True Then
                AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt5)
            End If
            If IsMarginR = True Then
                AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt6)
            End If
            If IsFontSize = True Then
                AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt2)
            End If
    
            If IsSpeed = True Then
                AnimMainStr.Append(vbCrLf & vbTab & "}," & speed)
            End If
    
            If IsEasing = True Then
                AnimMainStr.Append("," & easing)
            End If
            If IsCallBack = True Then
                AnimMainStr.Append("," & callBackF & ");")
            End If
    
            If IsFontColour = True Then
                AnimMainStr.Append(vbCrLf & vbTab & "$('#" & targetId & "').css({""color"":""" & fontColor & """})" & vbCrLf _
                                         & vbTab & "});")
            End If
    
            MsgBox(AnimMainStr.ToString)
    Thanks,
    Mike

  17. #17
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,984

    Re: [RESOLVED] Avoiding long painful conditionals

    Well it's clever but it surely adds another level of complication in that now you need to remember what to set the values to in the program rather than just choosing from True & False. And where the values are for intrinsic properties, you also need to set an intermediary variable. As the original intention was to simplify in order to prevent errors I'm not sure this would be my favourite solution.

  18. #18
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,984

    Re: [RESOLVED] Avoiding long painful conditionals

    Quote Originally Posted by MikeSpider View Post
    I'm not getting any error. Just not the expected results.
    And Yes it's a String datatype.
    I tried your example still the same.
    I dont understand Why is not detecting the boolean at all.
    Code:
    
     'setting properties vars
            If txtResetId.Text <> "" Then
                IsReset = True
            Else
                IsReset = False
            End If
            If fontColor <> "" Then
                IsFontColour = True
                prpt1 = "fontColour:'" & fontColor & "'"
            Else
                IsFontColour = False
            End If
    
            Dim FSize As String = CStr(fontSize)
            If FSize <> "" Then
                IsFontSize = True
                prpt2 = "fontSize:'" & fontSize & "px'"
            Else
                IsFontSize = False
            End If
    
            If Not widthAnim = "" Then
                IsWidth = True
                prpt3 = "width:" & widthAnim
            Else
                IsWidth = False
            End If
    
            'If widthAnim <> "" Then
            '    IsWidth = True
            '    prpt3 = "width:" & widthAnim
            'Else
            '    IsWidth = False
            'End If
            If heightAnim <> "" Then
                IsHeight = True
                prpt4 = "height:" & heightAnim
            Else
                IsHeight = False
            End If
            If marginL <> "" Then
                IsMarginL = True
                prpt5 = "marginLeft:" & marginL
            Else
                IsMarginL = False
            End If
            If marginR <> "" Then
                IsMarginR = True
                prpt6 = "marginRight:" & marginR
            Else
                IsMarginR = False
            End If
    
            'setting speed vars
            If rbSlow.Checked = True Then
                speed = "'" & rbSlow.Text & "'"
                IsSpeed = True
            ElseIf rbFast.Checked = True Then
                speed = "'" & rbFast.Text & "'"
                IsSpeed = True
            End If
            If txtSpeed.Text <> "" Then
                speed = txtSpeed.Text
                IsSpeed = True
            End If
    
            'Setting Easing vars
            If rbSwing.Checked = True Then
                easing = "'" & rbSwing.Text & "'"
                IsEasing = True
            ElseIf rbLinear.Checked = True Then
                easing = "'" & rbLinear.Text & "'"
                IsEasing = True
            End If
    
            'Setting callback function vars
            If cbCallBack.CheckState = CheckState.Checked Then
                callBackF = txtCallBack.Text
                IsCallBack = True
            End If
    
            Dim firstParam As String = "$('#" & triggerId & "').click(function(){" & vbCrLf & _
                                    vbTab & " $('#" & targetId & "').animate({ " 
            AnimMainStr.Append(firstParam.ToString)
    
            
    
            If IsWidth = True Then
                AnimMainStr.Append(vbCrLf & vbTab & vbTab & prpt3)
            End If
            If IsHeight = True Then
                AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt4)
            End If
            If IsMarginL = True Then
                AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt5)
            End If
            If IsMarginR = True Then
                AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt6)
            End If
            If IsFontSize = True Then
                AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt2)
            End If
    
            If IsSpeed = True Then
                AnimMainStr.Append(vbCrLf & vbTab & "}," & speed)
            End If
    
            If IsEasing = True Then
                AnimMainStr.Append("," & easing)
            End If
            If IsCallBack = True Then
                AnimMainStr.Append("," & callBackF & ");")
            End If
    
            If IsFontColour = True Then
                AnimMainStr.Append(vbCrLf & vbTab & "$('#" & targetId & "').css({""color"":""" & fontColor & """})" & vbCrLf _
                                         & vbTab & "});")
            End If
    
            MsgBox(AnimMainStr.ToString)
    Thanks,
    Mike
    We seem to have gotten a long, long way from
    to write this more efficiently and quicker
    even if it did work!

  19. #19
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: [RESOLVED] Avoiding long painful conditionals

    The only boolean being detected is :

    Code:
    
    'Setting callback function vars
            If cbCallBack.CheckState = CheckState.Checked Then
                callBackF = txtCallBack.Text
                IsCallBack = True
            End If
    All the <>"" are not working. I mean, all checks for empty fields are not working I dont undertand.
    Last edited by MikeSpider; Sep 2nd, 2012 at 03:31 PM.

  20. #20
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: [RESOLVED] Avoiding long painful conditionals

    Well it's clever but it surely adds another level of complication in that now you need to remember what to set the values to in the program rather than just choosing from True & False. And where the values are for intrinsic properties, you also need to set an intermediary variable. As the original intention was to simplify in order to prevent errors I'm not sure this would be my favourite solution.
    Yeah man, I was having a hard time appending everything in a for loop, but will need to practice in a spare time.

  21. #21
    Lively Member
    Join Date
    May 10
    Posts
    103

    Re: [RESOLVED] Avoiding long painful conditionals

    Quote Originally Posted by David Anton View Post
    One thing that will save you a bit of typing and be less odd is to not explicitly state boolean values in your test:
    Use:
    Code:
    If IsFontColour Then
    rather than:
    Code:
    If IsFontColour = True Then
    The latter sounds awkward - e.g., you don't ask "is it raining equals true?", you ask "is it raining?".
    I knew someone would mention that. When giving example code I always explicitly state boolean values because I feel it is easier to read and follow for "newbies". But I agree that it's easier to not explicitly state them.

    I disagree however that it sounds awkward. Your example sounds awkward only because you treated it like a question. An If statement is not a question, it's just a statement. It's not asking "is it raining?", it stating "If 'it is raining' equals true, then...". In long form it would mean, "If the statement that "it is raining" is true, then...".

    Not explicitly stating the boolean value is what I think sounds awkward when you use "If Not"...

    Code:
    If ItsRaining = False Then
    Is a lot more straight forward than:

    Code:
    If Not ItsRaining Then
    If Not ItsRaining?



    Anyway...

  22. #22
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: [RESOLVED] Avoiding long painful conditionals

    Now this is very weird!
    I commented everything and just wrote these lines:

    Code:
     If txtMarginL.Text <> "" Then
                MsgBox("It's not empty")
            Else
                MsgBox("It's empty")
            End If
    I dont write anything in the textbox but it still says "it's not empty!" if I put the cursor in the text field and press
    "delete" even though i didn't write anything, it then says " it's empty!" .
    Weird stuff!

  23. #23
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,984

    Re: [RESOLVED] Avoiding long painful conditionals

    Quote Originally Posted by MotoX646 View Post
    I knew someone would mention that. When giving example code I always explicitly state boolean values because I feel it is easier to read and follow for "newbies". But I agree that it's easier to not explicitly state them.

    I disagree however that it sounds awkward. Your example sounds awkward only because you treated it like a question. An If statement is not a question, it's just a statement. It's not asking "is it raining?", it stating "If 'it is raining' equals true, then...". In long form it would mean, "If the statement that "it is raining" is true, then...".

    Not explicitly stating the boolean value is what I think sounds awkward when you use "If Not"...

    Code:
    If ItsRaining = False Then
    Is a lot more straight forward than:

    Code:
    If Not ItsRaining Then
    If Not ItsRaining?



    Anyway...
    Well, yeah, but it's all totally arbitrary. There's nothing awkward about 'If Raining' and 'If Not Raining', is there? It's not meant to be read as an English essay. It's meant to be read as a computer program. And as a computer program, 'If VarName Then' and 'If Not VarName Then' are both functional and comprehensible.

  24. #24
    Lively Member
    Join Date
    May 10
    Posts
    103

    Re: [RESOLVED] Avoiding long painful conditionals

    Your original question was about avoiding long conditional statements. You wanted a conditional statement for every possible combination of 8 booleans, so you can create a string with every possible combination of properties in it. That is a very large number of possible combinations. Boolean is True or False, 1 or 0, that is base-2. With 8 different booleans you have 2^8 combinations. That would mean 256 different combinations... and conditional statements. That is just NOT the right way to go about doing what you were doing.

    That is why, after looking at your code, I realized you don't really need every possible combination... You were just adding values to a string if the corresponding boolean was true. That is a pretty simple task... But somehow you have made it more complex than it needs to be...


    Quote Originally Posted by MikeSpider View Post
    The only boolean being detected is :
    All the <>"" are not working. I mean, all checks for empty fields are not working I dont undertand.
    That is because your fields are probably not empty.

    Instead of:

    Code:
    If fontColor <> "" Then
      
    End If
    Try:

    Code:
    If Not fontColor = Nothing Then
        IsFontColour = True
        prpt1 = "fontColour:'" & fontColor & "'"
    Else
        IsFontColour = False
    End If

    Use MsgBox's to help debug your code... See what the values are set to. See what value IsFontColour is set to...

    Code:
    msgbox(fontColor)
    
    If Not fontColor = Nothing Then
        IsFontColour = True
        prpt1 = "fontColour:'" & fontColor & "'"
    Else
        IsFontColour = False
    End If
    
    MsgBox(IsFontColour.tostring)

    If you don't tell me/us the values, then we can't help you.

  25. #25
    Lively Member
    Join Date
    May 10
    Posts
    103

    Re: [RESOLVED] Avoiding long painful conditionals

    Quote Originally Posted by dunfiddlin View Post
    It's not meant to be read as an English essay. It's meant to be read as a computer program..
    Well, the original goal for Visual Basic was to make it read like a normal human would speak. Hence why it has this:

    For x as integer = 0 to 10 Step 1

    Next

    ...and not....

    for (x=0;x<10:x++)
    {

    }

  26. #26
    Lively Member
    Join Date
    May 10
    Posts
    103

    Re: [RESOLVED] Avoiding long painful conditionals

    Quote Originally Posted by MikeSpider View Post
    Now this is very weird!
    I commented everything and just wrote these lines:

    Code:
     If txtMarginL.Text <> "" Then
                MsgBox("It's not empty")
            Else
                MsgBox("It's empty")
            End If
    I dont write anything in the textbox but it still says "it's not empty!" if I put the cursor in the text field and press
    "delete" even though i didn't write anything, it then says " it's empty!" .
    Weird stuff!

    Oh ok, I just now read your post...

    Are you loading some non-unicode characters into your textbox? Some characters are invisible to the eye, but there is a character present.

  27. #27
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: [RESOLVED] Avoiding long painful conditionals

    Quote Originally Posted by MotoX646 View Post
    Oh ok, I just now read your post...

    Are you loading some non-unicode characters into your textbox? Some characters are invisible to the eye, but there is a character present.
    Thanks I already sorted it out.

    Your original question was about avoiding long conditional statements. You wanted a conditional statement for every possible combination of 8 booleans, so you can create a string with every possible combination of properties in it. That is a very large number of possible combinations. Boolean is True or False, 1 or 0, that is base-2. With 8 different booleans you have 2^8 combinations. That would mean 256 different combinations... and conditional statements. That is just NOT the right way to go about doing what you were doing.

    That is why, after looking at your code, I realized you don't really need every possible combination... You were just adding values to a string if the corresponding boolean was true. That is a pretty simple task... But somehow you have made it more complex than it needs to be...

    Can you show me the simpler way then, i could use it for the future. Is it the loop way?

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •