Property does not exist in the current context

后端 未结 4 1327
旧时难觅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:27

    There are two problems with your code.

    1. Marque is an int and you are trying to set a string
    2. You can't access the property outside a method or constructor.

    Use this instead and you'll find out about problem 1

    public Audi()
    {
        this.Marque = "Audi";
    }
    
    0 讨论(0)
  • 2021-01-29 01:28

    In C#, you can not initialize property in class body. You should initialize them in constructor:

    namespace ConsoleApplication5
    {
        public class Audi : Voiture
        {
            Audi() {
                this.Marque = "Audi";
            }
    
            public void Deraper()
            {
                Console.WriteLine("Vroooouum !!");
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-29 01:32

    Problem is your lack of basic understanding of OOP. You're trying to assign the base class property from the class scope which is not the way it works in OOP. You have to create some "callable" scope eg. constructor, and inside of that scope assign your parent's fields/properties:

    public class Audi : Voiture {
    // Draper() ...
    
        public Audi() : base() {
            Marque = "Audi";
        }
    }
    

    more descriptive answer :
    Everything inside a class scope is known as definition and every other scope in it is called an implementation. Since you've defined ( made a definition ) of that field/property in your parent class, you have to assign it in an implementation where access modifiers of that field/property allows you to or in the very same moment you're defining this field/property.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题