The requested page is not available for the requested platform. You are viewing the content for Default platform.

Introduce Parameter Object

  • 3 min to read

Consolidates selected parameters into a single structure and activates Linked Identifiers for the structure references.

Introduce Parameter Object is a cascading refactoring. That is, the refactoring affects all method calls and method declarations in interfaces, base and descendant classes. For instance, if you apply the refactoring to the method declaration within an interface, it also changes the appropriate method declarations in all implementors and all calls to these methods.

Purpose

If you frequently need to pass similar sets of values to methods, it might be useful to encapsulate these values into a structure. As the result, calling statements will become more compact and you will be able to add data processing logic to the newly declared structure.

Availability

From the context menu or via shortcuts:

  • when the caret is on a method declaration or call, provided that it has more than one parameter.
  • when two or more parameters are selected in a method declaration or call.
Note

The refactoring is available for method declarations in interfaces, base and descendant classes as well as for the declaration in the current class.

Notes

  • This refactoring automatically declares a new structure. The created structure has a constructor that initializes all properties to values passed as parameters. All initialized values can be accessed via public read-only properties, which replace simple parameters in the original code. Note that Introduce Parameter Object uses public read-only fields instead of properties. See Options below.
  • This refactoring is the functional opposite of Decompose Parameter.

Options

The Editor | Refactoring | Introduce Parameter Object page of the Options dialog allows you to specify whether to declare a class or a struct, where to declare it, and whether it should have properties or fields in it.

Example

class TestClass
{
    private string TestMethod(string param1, int param2, bool param3)
    {
        return String.Format("{0} - {1}, {2}", param1, param2, param3);
    }
}
Class TestClass
    Private Function TestMethod(ByVal param1 As String, _
        ByVal param2 As Integer,_
        ByVal param3 As Boolean) As String
        Return String.Format("{0} - {1}, {2}", param1, param2, param3)
    End Function
End Class

Result:

class TestClass
 {
    private struct TestMethodArgs
    {
        private string _Param1;
        private int _Param2;
        private bool _Param3;
        public TestMethodArgs(string param1, int param2, bool param3)
        {
            _Param1 = param1;
            _Param2 = param2;
            _Param3 = param3;
        }
        public string Param1
        {
            get
            {
                return _Param1;
            }
        }
        public int Param2
        {
            get
            {
                return _Param2;
            }
        }
        public bool Param3
        {
            get
            {
                return _Param3;
            }
        }
    }
    private string TestMethod(TestMethodArgs testMethodArgs)
    {
        return String.Format("{0} - {1}, {2}", testMethodArgs.Param1, testMethodArgs.Param2, testMethodArgs.Param3);
    }
}
Class TestClass
    Private Structure TestMethodArgs
        Private _param1 As String
        Private _param2 As Integer
        Private _param3 As Boolean
        Public Sub New(ByVal param1 As String, ByVal param2 As Integer, ByVal param3 As Boolean)
            _param1 = param1
            _param2 = param2
            _param3 = param3
        End Sub
        Public ReadOnly Property Param1() As String
            Get
                Return _param1
            End Get
        End Property
        Public ReadOnly Property Param2() As Integer
            Get
                Return _param2
            End Get
        End Property
        Public ReadOnly Property Param3() As Boolean
            Get
                Return _param3
            End Get
        End Property
    End Structure
    Private Function TestMethod(ByVal lTestMethodArgs As TestMethodArgs) As String
        Return String.Format("{0} - {1}, {2}", lTestMethodArgs.Param1, lTestMethodArgs.Param2, lTestMethodArgs.Param3)
    End Function
End Class

Screenshot

rsIntroduceParameterObjectCSharp