Keyword this is not valid in a static property, static method, or static field initilizer. The modifier 'Public' is not Valid for this item

huangapple go评论61阅读模式
英文:

Keyword this is not valid in a static property, static method, or static field initilizer. The modifier 'Public' is not Valid for this item

问题

我是C#的新手,尝试过谷歌搜索,但没有找到答案。在我使用this关键字时,它会标记一个错误。(我应该将它删除,改用Car吗?)当我使用Public Car()时,它也会标记一个错误。我尝试去掉Public,它可以工作,但我需要在不同的文件中使用它。这可能是因为我在追踪一个过时的课程。

using System.Text;
using System;

namespace SimpleClasses
{
    class Program
    {
        static void Main(string[] args) 
        {
            Car myNewCar = new Car();

            myNewCar.Make = "Toyota";
            myNewCar.Model = "Cutlass Supreme";
            myNewCar.Year = 1986;
            myNewCar.Color = "Silver";

            Car myOtherCar = myNewCar;

            Console.WriteLine(myOtherCar.Make);

            Console.WriteLine("Before: " + myNewCar.Make);
            doByValue(myNewCar);
            Console.WriteLine("After By Value: " + myNewCar.Make);
            doByReference(ref myNewCar);
            Console.WriteLine("After By Reference: " + myNewCar.Make);

            static void doByValue(Car car)
            {
                car = new Car();
                car.Make = "BMW";
            }

            static void doByReference(ref Car car)
            {
                car = new Car();
                car.Make = "BMW";
            }

            Console.ReadLine();
        }
    }

    class Car
    {
        public string Make { get; set; }
        public string Model { get; set; }
        public int Year { get; set; }
        public string Color { get; set; }

        public Car()
        {
            this.Make = "Nissan";
        }

        public double DetermineMarketValue()
        {
            return 0.0;
        }
    }
}

希望这可以帮助你解决问题。

英文:

Im new to c#, I tried googling but no answers. Its flaging me an error when I use this keyword. (Should i just take it out and use Car instead?) Its also flaging an error when I use Public Car(). I tried talking out Public and it worked but I need to use it in different Files. It could be because im following an outdated course.

using System.Text;
using System;

namespace SimpleClasses
{
    class Program
    {
        static void Main(string[] args) 
        {
            Car myNewCar = new Car();

            myNewCar.Make = "toyota";
            myNewCar.Model = "Cutlas Supreme";
            myNewCar.Year = 1986;
            myNewCar.Color = "Silver";

            Car myOtherCar = myNewCar;


            Console.WriteLine(myOtherCar.Make);


            Console.WriteLine("Before: " + myNewCar.Make);
            doByValue(myNewCar);
            Console.WriteLine("After By Value: " + myNewCar.Make);
            doByReference(ref myNewCar);
            Console.WriteLine("After By Reference: " + myNewCar.Make);

            static void doByValue(Car car)
            {
                car = new Car();
                car.Make = "BMW";
            }

            static void doByReference(ref Car car)
            {
                car = new Car();
                car.Make = "BMW";
            }



            Console.ReadLine();

            public Car()
            {
                this.Make = "Nissan";
            }

        }
    }





    class Car
    {
        public string Make { get; set; }
        public string Model { get; set; }
        public int Year { get; set; }
        public string Color { get; set; }


        public double DetermineMarketValue()
        {


            return 0.0;
        }
    }

}

答案1

得分: 1

这段代码是Car类的构造函数,它不应该放在静态main方法中。将它移到Car类内部:

class Car
{
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }
    public string Color { get; set; }

    public Car()
    {
        this.Make = "Nissan";
    }

    public double DetermineMarketValue()
    {
        return 0.0;
    }
}

虽然这段代码会将每个汽车实例初始化为Nissan制造,但通常你会使用构造函数来确保在新实例中提供所有必需的值,以帮助确保Car实例始终处于有效状态。例如:

public Car(string make, string model, int year, string color = "White")
{
    Make = !string.IsNullOrEmpty(make) ? make : throw new ArgumentNullException(nameof(make));
    Model = !string.IsNullOrEmpty(model) ? model : throw new ArgumentNullException(nameof(model));
    Year = year >= 1940 && year <= DateTime.Today.Year ? year : throw new ArgumentOutOfRangeException(nameof(year));
    Color = color; // 默认为"White"。
}

这需要任何想要创建Car实例的代码提供有关汽车的所有必需详细信息,以帮助避免将无效/不完整的对象传递给其他部分。验证传递的值是一个早期捕获潜在问题的好方法。

在C#中,请注意类、方法和命名空间的作用域以及{}的使用。

像这样的代码:

static void doByValue(Car car)
{
    car = new Car();
    car.Make = "BMW";
}

static void doByReference(ref Car car)
{
    car = new Car();
    car.Make = "BMW";
}

在Main方法的作用域内也没有意义。

静态main方法是应用程序启动时执行的函数。"Car"是一个类,表示属性和函数适用于汽车的封装。将类视为一个事物的模板或蓝图,以及如何使用该事物的说明。Main函数可能会创建一个或多个Car "对象" 实例。类是模板,对象是那些类的实例。

希望这有助于理解类和实例之间的关系。

英文:

This code is a constructor for the Car class, it does not belong in the static main method. Move it inside the Car class:

class Car
{
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }
    public string Color { get; set; }

    public Car()
    {
        this.Make = &quot;Nissan&quot;;
    }

    public double DetermineMarketValue()
    {
        return 0.0;
    }
}

Though this code would intitialize every car instance as a Nissan make. Normally you would use the constructor to ensure all required values in a new instance are provided to help ensure that a Car instance is always in a valid state. For example:

    public Car(string make, string model, int year, string color = &quot;White&quot;)
    {
        Make = !string.IsNullOrEmpty(make) ? make : throw new ArgumentNullException(nameof(make));
        Model = !string.IsNullOrEmpty(model) ? model : throw new ArgumentNullException(nameof(model));
        Year = year &gt;= 1940 &amp;&amp; year &lt;= DateTime.Today.Year ? year : throw new ArgumentOutOfRangeException(nameof(year));
        Color = color; // Defaults to &quot;White&quot;.
    }

This requires any code wanting to create an instance of a Car would need to provide all of the required details about the car to help avoid a situation where an invalid/incomplete object ends up getting passed around. It's a good idea to validate values being passed in to catch potential problems early.

In C#, be mindful of scopes for classes, methods, and namespaces, the use of { and }.

Code like this:

        static void doByValue(Car car)
        {
            car = new Car();
            car.Make = &quot;BMW&quot;;
        }

        static void doByReference(ref Car car)
        {
            car = new Car();
            car.Make = &quot;BMW&quot;;
        }

inside the scope of the Main method also does not make any sense.

The static main method is a function that will execute when your application starts. "Car" is a class, meaning an encapsulation for properties and functions applicable to a Car. Think of a Class as a template or blueprint for a thing and/or the instructions for how that thing should be used. The Main function may create one or more instances of Car "objects". Classes are the templates, Objects are the instances of those classes.

Hopefully that helps make some sense around classes and instances.

huangapple
  • 本文由 发表于 2023年1月9日 07:27:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定