Skip to main content

Replace with Constant

Replaces this expression with a reference to a constant with the same value.

#Purpose

Allows you to reduce code maintanence costs by eliminating duplicate values/expressions.

#Availability

Available from the context menu or via shortcuts:

  • when the edit cursor, or caret is on a string or a number and the current class/type has a constant initialized to the same string or number.

#Example

private const string MyConst = "Test string";
private string TestMethod(string str)
{
    return "Test string" + str;
}
Private Const MyConst As String = "Test string"
Private Function TestMethod(ByVal str As String) As String
    Return "Test string" + str
End Function

Result:

private const string MyConst = "Test string";
private string TestMethod(string str)
{
    return MyConst + str;
}
Private Const MyConst As String = "Test string"
Private Function TestMethod(ByVal str As String) As String
    Return MyConst + str
End Function

#Screenshot

rsReplaceWithConstantCSharp

See Also