[RESOLVED] Select Case if text contains logic question-VBForums
Results 1 to 4 of 4

Thread: [RESOLVED] Select Case if text contains logic question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2013
    Posts
    156

    Resolved [RESOLVED] Select Case if text contains logic question

    Hello:

    Assuming I have some of these conditions met, should this logic work?

    Code:
        Private Sub ScaleViews(ByVal swView As View)
            Debug.WriteLine("swView.Name: " & swView.Name)
            Select Case swView.Name
            ' View names starting with Drawing will use DrawingScale1
                Case LCase(swView.Name).Contains("drawing")
                    MsgBox("1")
                    swView.ScaleDecimal = swView.ScaleDecimal * CDec(txtDrawingScale.Text)
    
            ' View names starting with Detail or Section will use DetailScale2
                Case LCase(swView.Name).Contains("detail") Or LCase(swView.Name).Contains("section")
                    MsgBox("2")
                    swView.ScaleDecimal = swView.ScaleDecimal * CDec(txtDetailOrSectionScale.Text)
    
            End Select
    
        End Sub

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Mar 2013
    Posts
    156

    Re: Select Case if text contains logic question

    Guess Not:

    Code:
        Private Sub ScaleViews(ByVal swView As View)
            Debug.WriteLine("swView.Name: " & swView.Name)
            Dim ViewType As String = ""
            If Microsoft.VisualBasic.Left(LCase(swView.Name), 7) = "drawing" Then
                ViewType = "drawing"
    
            ElseIf Microsoft.VisualBasic.Left(LCase(swView.Name), 6) = "detail" Or Microsoft.VisualBasic.Left(LCase(swView.Name), 7) = "section" Then
                ViewType = "detail"
    
            End If
    
            Select Case ViewType
            ' View names starting with Drawing will use Drawing Scale
                Case "drawing"
                    swView.ScaleDecimal = CDec(txtDrawingScale.Text)
    
            ' View names starting with Detail or Section will use Detail Scale
                Case "detail"
                    swView.ScaleDecimal = CDec(txtDetailOrSectionScale.Text)
    
            End Select
    
        End Sub

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    3,923

    Re: Select Case if text contains logic question

    You original code would work in a way if you did your Select against True (at least I think it should, I didn't try it).
    Code:
       Private Sub ScaleViews(ByVal swView As View)
            Debug.WriteLine("swView.Name: " & swView.Name)
            Select Case True
            ' View names starting with Drawing will use DrawingScale1
                Case LCase(swView.Name).Contains("drawing")
                    MsgBox("1")
                    swView.ScaleDecimal = swView.ScaleDecimal * CDec(txtDrawingScale.Text)
    
            ' View names starting with Detail or Section will use DetailScale2
                Case LCase(swView.Name).Contains("detail") Or LCase(swView.Name).Contains("section")
                    MsgBox("2")
                    swView.ScaleDecimal = swView.ScaleDecimal * CDec(txtDetailOrSectionScale.Text)
    
            End Select
    
        End Sub

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,277

    Re: Select Case if text contains logic question

    I've never really liked "Select Case True" because it is basically a more long-winded way to write an If/ElseIf structure.

    Quote Originally Posted by ssabc View Post
    Guess Not:

    Code:
        Private Sub ScaleViews(ByVal swView As View)
            Debug.WriteLine("swView.Name: " & swView.Name)
            Dim ViewType As String = ""
            If Microsoft.VisualBasic.Left(LCase(swView.Name), 7) = "drawing" Then
                ViewType = "drawing"
    
            ElseIf Microsoft.VisualBasic.Left(LCase(swView.Name), 6) = "detail" Or Microsoft.VisualBasic.Left(LCase(swView.Name), 7) = "section" Then
                ViewType = "detail"
    
            End If
    
            Select Case ViewType
            ' View names starting with Drawing will use Drawing Scale
                Case "drawing"
                    swView.ScaleDecimal = CDec(txtDrawingScale.Text)
    
            ' View names starting with Detail or Section will use Detail Scale
                Case "detail"
                    swView.ScaleDecimal = CDec(txtDetailOrSectionScale.Text)
    
            End Select
    
        End Sub
    I don't understand the reason for using the Select Case (and the ViewType variable) there, as it would give the same effect if you put the ScaleDecimal code directly into the If/ElseIf section... but it is your choice.

    Note however that your code can be made shorter (and easier to read) by using the .Net methods rather than the methods from older versions of VB, for example this:
    Code:
            ElseIf Microsoft.VisualBasic.Left(LCase(swView.Name), 6) = "detail" Or Microsoft.VisualBasic.Left(LCase(swView.Name), 7) = "section" Then
    ...can be reduced to this:
    Code:
            ElseIf swView.Name.ToLower.StartsWith("detail") Or swView.Name.ToLower.StartsWith("section") Then

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.