Bart Friederichs
Bart Friederichs

Reputation: 33531

Defining getter in C#.NET, declared body not accepted

Consider the following code:

[DataContract]
class QObject {
    public QObject() { }
    [DataMember(Name = "objectindex")]
    public int ObjectIndex { get; set; }
    [DataMember(Name = "datapoint")]
    public int DataPoint { get; set; }
    [DataMember(Name = "type")]
    public string Type { get; set; }
    [DataMember(Name = "data")]
    public int[] Data { get; set; }
    [DataMember(Name = "parameters")]
    public string __Parameters {
        get {
            return this.__Parameters;
        }
        set {
            Parameters = new Dictionary<string,string>();
            Parameters.Add("key", "value");
        } 
    }
    public Dictionary<string, string> Parameters;
}

Which seems fine by me, but when I want to compile, it gets the following error:

'QuartzService.NET.QObject.__Parameters.get' must declare a body because it is not marked abstract, extern, or partial

The strange thing is, that the get has declared a body. So, how to fix this?

Upvotes: 0

Views: 435

Answers (3)

Osama Javed
Osama Javed

Reputation: 1442

a getter returns another variable not itself . Maybe you want to return something from parameters. Furthermore your setter always creates a new dictionary and adds the same thing into it. What are you trying to do?

try something like this

        public string __Parameters
    {
        get { return this.recreate(); } 
        set
        {
            Parameters = parse(value));
        }
    }     

    public Dictionary<string, string> Parameters;

where the parse funciton takes in a string, breaks it and stores it into the dictionary. and the recreate function uses the dictionary to recreate the string.

also Do this:

 public Dictionary<string, string> Parameters = new Dictionary<String,String>();

so that you dont accidentally get null pointers and since you only need one dictionary in my opinion. (Dont forget to clear it though when you parse a new String ).

Upvotes: 1

anouar.bagari
anouar.bagari

Reputation: 2104

the getter you defined well introduce a recursive call.

public string __Parameters {
    get {
        return this.__Parameters;
    }
}

try to change this.__Parameters; by

private string _parameters 
public string __Parameters {
    get {
        return this._parameters;
    }
}

Upvotes: 0

Tilak
Tilak

Reputation: 30718

Have you noticed the infinite loop in getter

 public string __Parameters {
    get {
        return this.__Parameters;
    }
 .........

Upvotes: 4

Related Questions