-
Oct 5th, 2017, 01:51 PM
#1
Thread Starter
Fanatic Member
[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
-
Oct 5th, 2017, 02:15 PM
#2
Thread Starter
Fanatic Member
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
-
Oct 5th, 2017, 04:24 PM
#3
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
-
Oct 6th, 2017, 04:20 AM
#4
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.
Originally Posted by ssabc
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|