简体   繁体   中英

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

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

}

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

    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 = "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; // Defaults to "White".
    }

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM