Best Practices - Single Line If Statements?

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1705 Views - Last Post: 08 April 2013 - 10:46 PM

#16 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Best Practices - Single Line If Statements?

Posted 01 April 2013 - 10:02 AM

View Postdepricated, on 01 April 2013 - 12:40 PM, said:

Wouldn't that exit the If statement as soon as the first condition returned true? Which leaves the other elements unvalidated and unassigned.


Uh, yeah. In the exact same why the OP's Exit Function on all those If .. Else blocks would? :P

Realistically, with all those validates, I'd consider put them in a separate functions.
Was This Post Helpful? 0
  • +
  • -

#17 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3237
  • View blog
  • Posts: 10,867
  • Joined: 12-December 12

Re: Best Practices - Single Line If Statements?

Posted 01 April 2013 - 10:13 AM

If the items to check are not all strings - perhaps Validate accepts Object (which is not good?!) or, as AdamSpeight2008 suggests, the method is overloaded - then an anonymous type could be used:

            Dim checks = New With {.first = "Hello", .second = "There", .third = 42}
            Dim checkList = {checks}.ToList
            Dim passed As Boolean = True
            If checkList.Any(Function(x) Not Validate(x)) Then
                passed = False
            End If
            If passed = False Then
                Exit Function
            End If

This post has been edited by andrewsw: 01 April 2013 - 10:20 AM

Was This Post Helpful? 0
  • +
  • -

#18 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3237
  • View blog
  • Posts: 10,867
  • Joined: 12-December 12

Re: Best Practices - Single Line If Statements?

Posted 01 April 2013 - 10:25 AM

BTW There is already a function called Validate (with WinForms).
Was This Post Helpful? 0
  • +
  • -

#19 depricated  Icon User is online

  • DLN-000

Reputation: 591
  • View blog
  • Posts: 2,110
  • Joined: 13-September 08

Re: Best Practices - Single Line If Statements?

Posted 01 April 2013 - 10:28 AM

So here's the full "base" function

Private Function PrepareExternalInitial() As Boolean
    
    If Not ValidateExternalInitialElements Then
        'if validation fails, exit function and return false
        PrepareExternalInitial = False
        Exit Function 
    End If
       
    Dim mailData As TemplateData
    
    Set mailData = New TemplateData
    
    With mailData
        If addrSilver <> "" Then
            'not a required element
            .setBCC addrSilver
        End If
        
        If stfyyy <> "" Then
            'not a required element
            .setyyy stfyyy
        End If
        
        'sets the html template from the database
        .setDBValues "Initial", "Silver"
        
        .setShortDescription strDescription
        .setStartTime dateIncidentStart
        .setxxx lngxxx
        .setSituation strSituation
        .setImpact strImpact
        .setFix strActions
        .setNextUpdate dateNextUpdate
    End With
    
    PrepareExternalInitial = SendExternalInitial(mailData)
End Function


So I'm going through Validation as so:

Private Function ValidateExternalInitialElements() As Boolean
        Dim elements(6) As Control
        
        Set elements(0) = dateNextUpdate
        Set elements(1) = strDescription
        Set elements(2) = dateIncidentStart
        Set elements(3) = lngRemedy
        Set elements(4) = strSituation
        Set elements(5) = strImpact
        Set elements(6) = strActions
    
        ValidateExternalInitialElements = ValidateControlArray(elements)
End Function

Private Function ValidateControlArray(ByRef controls() As Control) As Boolean
       Dim i As Integer
       For i = LBound(controls) To UBound(controls)
            'the first time any element fails validation, return false and exit function
            ValidateControlArray = Validate(controls(i))
            If ValidateControlArray = False Then
                Exit Function
            End If
        Next
End Function

This post has been edited by depricated: 01 April 2013 - 10:37 AM

Was This Post Helpful? 0
  • +
  • -

#20 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Best Practices - Single Line If Statements?

Posted 01 April 2013 - 10:33 AM

What programming language you using?
Was This Post Helpful? 0
  • +
  • -

#21 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3237
  • View blog
  • Posts: 10,867
  • Joined: 12-December 12

Re: Best Practices - Single Line If Statements?

Posted 01 April 2013 - 10:36 AM

View PostAdamSpeight2008, on 01 April 2013 - 05:33 PM, said:

What programming language you using?

..looks like VB6 (Set, control arrays, setting arguments without parens).

@OP Why did you say this was VB.NET? Surely you know that this is VB6/VBA.

This post has been edited by andrewsw: 01 April 2013 - 10:41 AM

Was This Post Helpful? 0
  • +
  • -

#22 depricated  Icon User is online

  • DLN-000

Reputation: 591
  • View blog
  • Posts: 2,110
  • Joined: 13-September 08

Re: Best Practices - Single Line If Statements?

Posted 01 April 2013 - 10:39 AM

Sorry, ya, VBA
Was This Post Helpful? 0
  • +
  • -

#23 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3237
  • View blog
  • Posts: 10,867
  • Joined: 12-December 12

Re: Best Practices - Single Line If Statements?

Posted 01 April 2013 - 10:43 AM

View Postdepricated, on 01 April 2013 - 05:39 PM, said:

Sorry, ya, VBA

This is more important than you suggest, particularly as this is your first project: VB6, VBA and VB.NET are quite distinct languages (although VB6 and VBA are very closely aligned).

This post has been edited by andrewsw: 01 April 2013 - 10:44 AM

Was This Post Helpful? 0
  • +
  • -

#24 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Best Practices - Single Line If Statements?

Posted 01 April 2013 - 11:00 AM

You could utilize the functionality provided via the use of ParamArray.
' Not sure if control is a valid type in VBA 
Function ValidateControls( ByVal ParamArray stuffToCheck() As Control) As Boolean
  Dim lb = LBound(stuffToCheck)
  Dim ub = UBound(stuffToCheck)
  If ((ub+1)-lb) > 0 Then
    Return False
    For i = lb To ub
      If Not Validate( StuffToCheck( i ).Text ) Then
        Validate = False
        Exit Function
      End If 
    Next
    Validate = True
  Else
    Validate = False
  End If
End Function


Usage:-
IF ValidateControls ( TextBox1, TextBox2 , TextBox3, etc ) Then

End If



Why you using VBA? Is it a Legacy Code Project?
If it is a new project why you not using a more modern programming language?

This post has been edited by AdamSpeight2008: 01 April 2013 - 11:01 AM

Was This Post Helpful? 0
  • +
  • -

#25 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3237
  • View blog
  • Posts: 10,867
  • Joined: 12-December 12

Re: Best Practices - Single Line If Statements?

Posted 01 April 2013 - 11:10 AM

This would be more valid VBA syntax:

Function ValidateControls(ParamArray stuffToCheck() As Variant) As Boolean
  Dim lb As Integer
  Dim ub As Integer
  Dim i As Integer
  
  lb = LBound(stuffToCheck)
  ub = UBound(stuffToCheck)
  
  If ((ub + 1) - lb) > 0 Then
    ValidateControls = False
    For i = lb To ub
      If Not Validate(stuffToCheck(i).Text) Then
        ValidateControls = False
        Exit Function
      End If
    Next
    ValidateControls = True
  Else
    ValidateControls = False
  End If
End Function

Control is a valid type (within a UserForm's module) but ParamArray requires the use of Variant.

Added: Within a standard module it would require:

Dim ctl As MSForms.Control

I'm not sure if stuffToCheck(i).Text will work - I think a Control object/reference would need to be created within the function.

And we cannot declare and initialize a variable.

This post has been edited by andrewsw: 01 April 2013 - 11:17 AM

Was This Post Helpful? 0
  • +
  • -

#26 depricated  Icon User is online

  • DLN-000

Reputation: 591
  • View blog
  • Posts: 2,110
  • Joined: 13-September 08

Re: Best Practices - Single Line If Statements?

Posted 01 April 2013 - 11:12 AM

View Postandrewsw, on 01 April 2013 - 11:36 AM, said:

View PostAdamSpeight2008, on 01 April 2013 - 05:33 PM, said:

What programming language you using?

..looks like VB6 (Set, control arrays, setting arguments without parens).

@OP Why did you say this was VB.NET? Surely you know that this is VB6/VBA.

View PostAdamSpeight2008, on 01 April 2013 - 12:00 PM, said:

Why you using VBA? Is it a Legacy Code Project?
If it is a new project why you not using a more modern programming language?

I'm updating a legacy project and simultaneously building it in VB.NET. Stupid mistake on my part to say it was .NET, sorry about that.

Basically, it's an Access DB/Front End that's already being used in production. I'm adding additional features, but doubling my work because we're going to transition it to VB.NET/SQL this Fall and I want to have a lot of it ready by the time we get there.
Was This Post Helpful? 0
  • +
  • -

#27 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Best Practices - Single Line If Statements?

Posted 08 April 2013 - 10:46 PM

Quote

I'm not sure if stuffToCheck(i).Text will work - I think a Control object/reference would need to be created within the function.
You can reference members of a control that's part of a control array in this manner. If stuffToCheck is a text box array, this will work.

Edit: a little experimenting shows that you can't create control arrays in VBA, only in VB6. In VB6, you can give, say, a group of text boxes the same name and set an index property. In VBA, the controls don't have an index property, so you can't do that.

This post has been edited by BobRodes: 08 April 2013 - 11:04 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2