Back to Devexpress

Declare Class

coderushforroslyn-115689-coding-assistance-code-providers-declaration-providers-declare-class.md

latest3.5 KB
Original Source

Declare Class

  • Aug 25, 2021
  • 3 minutes to read

Purpose

Generates a class for reference to a non-existent class and adds the class members referenced from the initial code to it. This code provider can drop a marker onto the initial class reference, if the class is created in a new file (the default behavior) and the Markers feature is enabled.

See the following topic section for more details: Markers: How to Enable

The Declare Class code provider is useful for Test-Driven Development. This provider allows you to create declarations for classes referenced in a code fragment.

Note

The Declare Class code provider also declares class members called from the initial code.

Availability

Available when the caret is in a class name if the class does not exist.

Usage

  1. Place the caret in a non-existent class name in its construction.

  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.

  3. Select Declare Class from the menu.

After execution, the code provider adds a new file to the project and declares the class in it.

csharp
//Filename: Customer.cs
using System;

namespace ConsoleApp
{
    public class Customer
    {
        public Customer(string str1, string str2)
        {
        }
        public void SaveToDB()
        {
            throw new NotImplementedException();
        }
        public int Age {
            get {
                throw new NotImplementedException();
            }
            set {
                throw new NotImplementedException();
            }
        }
    }
}
vb
'Filename: Customer.vb
Namespace ConsoleApp
    Public Class Customer
        Public Sub New(ByVal str1 As String, ByVal str2 As String)
        End Sub
        Public Sub SaveToDB()
            Throw New NotImplementedException
        End Sub
        Property Age As Integer
            Get
                Throw New NotImplementedException
            End Get
            Set(ByVal Value As Integer)
                Throw New NotImplementedException
            End Set
        End Property
    End Class
End Namespace

Customization

Change Code Actions Settings

You can configure the “Declare Class” code provider settings on the Editor | C# (Visual Basic) | Code Actions | Code Actions Settings options page.

For example, specify the “Position of newly-generated type declarations” setting to configure where CodeRush should place the newly-generated type declarations. The possible options are:

  • Above the current type
  • Below the current type
  • Create a new file (the default value)

For more information, refer to the following topic: Code Actions Settings.

Change Scope

You can also change the default visibility modifier of the generated type and its members on the Editor | C# (Visual Basic) | Scope Options options page.

See the following topic for details: Scope.