使用不变方法,该名称在当前上下文中不存在

时间:2019-04-23 11:53:03

标签: c# code-contracts invariants

我正在尝试在停车场类中的程序中使用不变式(代码协定库),在该类中实现了我的所有方法,但出现“名称在当前上下文中不存在”。突出显示的词似乎引起了问题。 会议ID和泊车ID 来自停车场类别之外的两个不同类别。这是我到目前为止尝试过的。

 class Program
{


    public class Conferences
    {
        public int conferenceID { get; set; }
        public Parking[] parkingArray { get; set; }
        public DateTime DateTime { get; set; }

    }

    public class Parking
    {
        public int parkingID { get; set; }
        public bool parkingIsReserved { get; set; }
        public bool parkingIsPurchased { get; set; }
        public bool parkingisReservedBy { get; set; }
        public DateTime DateTime { get; set; }
    }

       [ContractInvariantMethod]
        void ObjectInvariant()
        {
            Contract.Invariant(**conferenceID** >= 0 && conferenceID <= totalConferences);
            Contract.Invariant(**parkingID** >= 0 && parking <= maxParkingSpaces);
            Contract.Invariant(parkingID <= availableParkings);
        }

1 个答案:

答案 0 :(得分:1)

您的合同不变式应与您要测试的字段处于同一类,在您的示例中也称为Parking

public class Parking
{
    public int parkingID { get; set; }
    public bool parkingIsReserved { get; set; }
    public bool parkingIsPurchased { get; set; }
    public bool parkingisReservedBy { get; set; }
    public DateTime DateTime { get; set; }

   [ContractInvariantMethod]
    void ObjectInvariant()
    {
        Contract.Invariant(**conferenceID** >= 0 && conferenceID <= totalConferences);
        Contract.Invariant(**parkingID** >= 0 && parking <= maxParkingSpaces);
        Contract.Invariant(parkingID <= availableParkings);
    }
}