Property does not exist in the current context

心不动则不痛 提交于 2020-01-22 03:37:07

问题


I'm getting the following error :

"Property does not exist in the current context".

I checked on StackOverflow the usual causes of this, but I have made no one of the mistakes presented. (at least none of those I understood ^^). I am working with Microsoft Visual Studio 2015

Here is my code :

namespace ConsoleApplication5
{
    public class Voiture
    {
        public int Vitesse { get; set; }
        public Voiture()
        {
            Vitesse = 5;
        }
        public string Marque
        {
            get
            {
                throw new System.NotImplementedException();
            }

            set
            {
            }
        }
    }
}

and in another file

namespace ConsoleApplication5
{
    public class Audi : Voiture
    {
        public void Deraper()
        {
            Console.WriteLine("Vroooouum !!");
        }

        this.Marque = "Audi";
    }
}

If instead of this.Marque I use Voiture.Marque, I get the same problem. As you can see, the namespaces are OK. Any idea of what is going on ?


回答1:


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




回答2:


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";
}



回答3:


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 !!");
        }
    }
}



回答4:


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.



来源:https://stackoverflow.com/questions/45231755/property-does-not-exist-in-the-current-context

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!