tags:

views:

1650

answers:

4

I'm using VS 2008, and in my property pages for the project I see that I'm targeting .Net 3.5.

Here is the error I'm getting when trying to compile:

AMSDataModels.Vehicle.VIN.get' must declare a body because it is not marked abstract, extern, or partial

And here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AMSDataModels
{
    public class Vehicle
    {
        //NodeID for datastore persistance
        public Guid NodeID { get; set; }

        public string VIN { get; 
            set { 
                if (value.Length != 17) throw new ArgumentOutOfRangeException("VIN", "VIN must be 17 characters"); 
            } }

        public string Make { get; set; }
        public string Model { get; set; }
    }
}

If I strip the body from set so that its just:

public string VIN { get; set; }

All works, but I lose my ability to check the VIN as it is set.

Does anyone have a suggestion of how to fix this or a better way to approach the problem at hand?

I really like the shorthand notation - but verifying the legitimacy of input is important too!

+2  A: 

Yes, you will have to declare get implementation as well. Oh, and your set code does not do anything other than validation. You will need to provide additional implementation for that as well, assuming that you want to set the value if it passes validation.

If you need anything more than just basic get/set implementation, you will have to implement the whole property, not just the difference.

Adrian Godong
+8  A: 

If you're going to add logic in the set, you need to add it into the get as well. Notice in your set you're not actually setting a value to anything?

Add a backing field,

private string _vin;

and return that in the get.

public string VIN
{
    get { return _vin; }
    set
    {
      if (value.Length != 17) 
        throw new ArgumentOutOfRangeException("VIN", "VIN must be 17 characters"); 
      else
        _vin = value;
    }
}
Brandon
Thanks, I guess my first priority was making sure I wasn't doing unnecessary work creating the private field or wasn't somehow attempting to use the automatic properties wrong.
Matt
Actually you do not need to create a field just return VIN :)
Yassir
"return VIN"? That would be a recursive function unless I am missing something obvious...
Ed Swangren
If you're missing it, than I am too.
Brandon
+4  A: 

When automatic properties are used, the compiler automatically generates a backer field. When you declare your own, there's no way for it to know what field to use for the get method. So you have to declare both or none.

Incidentally, your current set method only checks for the value - it never actually assigns it to anything.

Paul Alexander
Back to the IDE I guess - thanks.
Matt
+2  A: 

You'll have to use the good ol' backing field. The short-hand notation can't be mixed. The only extra fun is to change the access modifier on get and set, e.g. get; private set;

flq