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:

Sample #2:

How to fix:

Add the body of the method:

Add the body of the event accessor:

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:

How to fix:

Remove the body of the extern member:

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:

How to fix:

Implement the desctructor:

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:

How to fix:

Remove the descructor:

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:

How to fix:

Remove the member type to make it a constructor:

Rename the member:

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:

How to fix:

Specify the member type:

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:

How to fix:

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

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:

How to fix:

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

Indexer cannot be static

Cause:

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