Skip to content

Refactor>Extract Method: Produces incorrect syntax #2783

@IvenBach

Description

@IvenBach

Starting with

Public Sub TestExtractMethod_Before_NoSpace()
    
    Const someValue As String = "Fed in value: "
    Dim someRange As Range
    Set someRange = Selection
    
    someRange.Value2 = "A value"
    
    Dim someCounter As Long
    
    Dim Cell As Range
    For Each Cell In someRange
        Cell = someValue & someCounter
        someCounter = someCounter + 1
    Next
End Sub

and selecting

    Dim Cell As Range
    For Each Cell In someRange
        Cell = someValue & someCounter
        someCounter = someCounter + 1
    Next

produces what's below.

Public Sub TestExtractMethod_After_NoSpace()
    
    Const someValue As String = "Fed in value: "
    Dim someRange As Range
    Set someRange = Selection
    
    someRange.Value2 = "A value"
    
    
End Sub
NewMethod someValue, someRange
Private Sub NewMethod(ByRef someValue As String, ByRef someRange As Range)
    Dim someCounter As Long
    Dim Cell As Range
    For Each Cell In someRange
        Cell = someValue & someCounter
        someCounter = someCounter + 1
    Next
End Sub

When I run it on the code below (note the space after Next and End Sub) with the same selection Dim Cell as Range through `Next' I end up with proper syntax, excluding indentation issues.

Public Sub TestExtractMethod_Before_WithSpace()
    
    Const someValue As String = "Fed in value: "
    Dim someRange As Range
    Set someRange = Selection
    
    someRange.Value2 = "A value"
    
    Dim someCounter As Long

    Dim Cell As Range
    For Each Cell In someRange
        Cell = someValue & someCounter
        someCounter = someCounter + 1
    Next
    
End Sub

Results in

Public Sub TestExtractMethod_After_WithSpace()
    
    Const someValue As String = "Fed in value: "
    Dim someRange As Range
    Set someRange = Selection
    
    someRange.Value2 = "A value"
    

    
NewMethod someValue, someRange
End Sub
Private Sub NewMethod(ByRef someValue As String, ByRef someRange As Range)
    Dim someCounter As Long
    Dim Cell As Range
    For Each Cell In someRange
        Cell = someValue & someCounter
        someCounter = someCounter + 1
    Next
End Sub

If I had the code without a space between the declarations Extract Method would work sometimes and not at others. Sporadic at best and couldn't determine any pattern.

    Dim someCounter As Long
    Dim Cell As Range

Should someValue be passed ByVal since in this case its value can't change?

Also saw that NewMethod was used even when one already existed. Should there be an increment number appended to the name? Kind of a fringe case...

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions