rookie_rockie

hi. i porting from java to c# and i encountered some error. i hope someone can explain it to me
i'm creating an interface like this:

interface MessageHeader
{
byte NACK = (byte) 0x03;

}
and i will get a compiler error : Interfaces cannot contain fields.

however if i change the codes to this:
byte CONNECT { get;set;}

it will just compiled fine.can someone pls explain it to me .thanks in advance





Re: Visual C# General Interfaces cannot contain fields.--need help

Thomas Danecker

interfaces can't have a state (that means they can't have fields). Interfaces only defines methods (properties are also a kind of methods) which can be implemented by classes.

See http://msdn2.microsoft.com/en-us/library/87d83y5b.aspx






Re: Visual C# General Interfaces cannot contain fields.--need help

Peter Ritchie

If NACK is a constant, I suggest creating a new static class in the same namespace and put it there. For example:

Code Snippet
public static class Constants
{
const int NACK = 0x11;
}

Then, wherever NACK is currently used, replace it with Constants.NACK.