Home > Code Analysis > Code issues specific to declaring members

Code issues specific to declaring members

December 27th, 2011

One of the goals of the Code Issues technology is to help you find mistakes when coding before compiling. This increases the coding speed and allows you to save time in the future when dialing with those mistakes. Let’s take a look at code issues CodeRush provides specific to declaring members:

Member must declare a body because it is not marked abstract or extern

Cause:

The code issue has the error type, because it is not allowed to declare non-abstract and non-extern members without a body. Non-abstract and non-extern members should have an implementation. This code issue is shown for properties and methods.

Sample #1:

CodeRush - Member must declare a body because it is not marked as abstract or extern

Sample #2:

CodeRush - Member must declare a body because it is not marked as abstract or extern (event)

How to fix:

  • Add the body of the method:

CodeRush Member Must Declare A Body Because It Is Not Marked Abstract Or Extern Fix

  • Add the body of the event accessor:

CodeRush Member Must Declare A Body Because It Is Not Marked Abstract Or Extern Fix 2

Extern member cannot declare a body

Cause:

This code issue is the opposite of the previous one. Highlights the member that is marked as an extern and that declares a body at the same time as an error.

Sample:

CodeRush - Extern method can not declare a body

How to fix:

  • Remove the body of the extern member:

CodeRush Extern Member Cannot Declare A Body Fix

Destructor must declare a body

Cause:

Destructors are a special type of members, so they have a dedicated code issue of the same error type. Destructors, like non-abstract and non-extern members, are also must declare a body.

Sample:

CodeRush - Destructor must declare a body

How to fix:

  • Implement the desctructor:

CodeRush Destructor Must Declare A Body Fix

Only class types can contain destructors

Cause:

It is not allowed to declare destructors in a type different from a class, such as a structure or an interface. This code issue highlights destructors declared in the wrong type with an error.

Sample:

CodeRush - Only class types can contain destructors

How to fix:

  • Remove the descructor:

CodeRush Only Class Types Can Contain Destructors Fix

Member names cannot be the same as their enclosing type

Cause:

The code issue of the error type highlights members with the same name as their enclosing type, because member names must not match the name of their enclosing type. Only type constructors can be named the same as their enclosing type.

Sample:

CodeRush - Member names can not be the same as their enclosing type

How to fix:

  • Remove the member type to make it a constructor:

CodeRush Member Names Cannot Be The Same As Their Enclosing Type Fix 1

  • Rename the member:

CodeRush Member Names Cannot Be The Same As Their Enclosing Type Fix 2

Method must have a return type

Cause:

Methods can not be declared without a return type. For example, procedures that do not return a value should have the ‘void’ return type. If the method does not have a return type and it is not a constructor, the method is highlighted as erroneous. Only constructors should be declared without a return type specified.

Sample:

CodeRush - Method must have a return type

How to fix:

  • Specify the member type:

CodeRush Method Must Have A Return Type Fix

Protected member cannot be declared in struct

Cause:

Protected members are not allowed inside structs because you can not inherit from a structure. This code issue has an error type:

Sample:

CodeRush - Protected member can not be declared in struct

How to fix:

  • Change the member access modifier, e.g.,:

CodeRush Protected Member Cannot Be Declared In Struct Fix

Protected member in sealed type will be private

Cause:

Sealed classes can not be inherited, so any protected member is treated as a private.

Sample:

CodeRush - Protected member in sealed class will be private

How to fix:

  • Change the member access modifier, e.g.,:

CodeRush Protected Member Cannot Be Declared In Struct Fix

Indexer cannot be static

Cause:

Indexed properties are not allowed to be static. This code issue has an error type.

Sample:

CodeRush - Indexer can not be static

How to fix:

  • Remove the static keyword:

CodeRush Indexer Cannot Be Static Fix

—–
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: 2008 and up
Updated: Nov/12/2012
ID: C154

Similar Posts: