Skip to main content

Widen Scope

  • 2 minutes to read

Moves a variable declaration up (out) in scope.

#Availability

Available from the context menu or via shortcuts:

  • when the caret is on a variable that is declared within a loop, or a conditional statement. As the result, the declaration statement is moved outside of the parent code block to widen variable visibility.

#Example

public bool SaveText(string text)
{
  try
  {
    string fileName = "C:\\data.txt";
    StreamWriter sWriter = File.CreateText(fileName);
    sWriter.Write(text);
    sWriter.Close();
  }
  catch(Exception ex)
  {
    MessageBox.Show(ex.Message);
    return false;
  }
  return true;
}
Public Function SaveText(ByVal text As String) As Boolean
  Try
    Dim fileName As String = "C:\data.txt"
    Dim sWriter As StreamWriter = File.CreateText(fileName)
    sWriter.Write(text)
    sWriter.Close()
  Catch ex As Exception
    MessageBox.Show(ex.Message)
    return false
  End Try
    return true
End Function

Result:

public bool SaveText(string text)
{
  string fileName = "C:\\data.txt";
  try
  {
    StreamWriter sWriter = File.CreateText(fileName);
    sWriter.Write(text);
    sWriter.Close();
  }
  catch (Exception ex)
  {
    MessageBox.Show(string.Format("Cannot save text to \"{0}\": {1}", fileName, ex.Message));
    return false;
  }
  return true;
}
Public Function SaveText(ByVal text As String) As Boolean
  Dim fileName As String = "C:\data.txt"
  Try
    Dim sWriter As StreamWriter = File.CreateText(fileName)
    sWriter.Write(text)
    sWriter.Close()
  Catch ex As Exception
    MessageBox.Show(string.Format("Cannot save text to {0}: {1}", fileName, ex.Message))
    return false
  End Try
    return true
End Function

#Screenshot

rsWidenScopeCSharp

See Also