Property does not exist in the current context

后端 未结 4 1320
旧时难觅i
旧时难觅i 2021-01-29 00:54

I\'m getting the following error :

\"Property does not exist in the current context\".

I checked on StackOverflow the usual causes

4条回答
  •  时光说笑
    2021-01-29 01:40

    1) You are not allowed to access and initialize inherited properties outside a method body. You are only allowed to declare new properties or fields there.

    2) Marque is of type int and you cannot assign a string to it

    3) your setter of Marque is empty so this will have no effect

    Solutions:

    1) Move the access of this.Marque into the constructor or a method body!

    2) change either the type of Marque to string or the value that you assign to it to an int

    3) add an extra private field and rewrite the setter (and the getter) in the following way:

    private int marque;
    public int Marque
    {
        get
        {
            return marque;
        }
    
        set
        {
            marque = value;
        }
    }
    

    For more information on how to use properties you can check these links out:

    https://www.dotnetperls.com/property

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties

    EDIT:

    If instead of this.Marque I use Voiture.Marque, I get the same problem.

    This is because the first problem is still valid. If you would do this inside a method body you would get an additional problem! Because Marque is not static, so you cannot use it by calling it with the class name. You would need an instance of an object of type Voiture

提交回复
热议问题