简体   繁体   English

尝试 output 时获取 null 值

[英]Getting a null value when try to output

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

namespace Week7_A
{
    class Experiment
    {
        public  string NAME { get; set; }
        private int NUM { get; set; }
    private string COLOR;
        private double WEIGHT;
        private double VOLUME;
        private string[] colors = new string[6] {"Crimson", "Azure", " Taupe", "Mauve", "Vermillion", "Chartreuse"};
        public bool error;




        public Experiment(string Name, int Num, string Col, double Weight)
        {
            NAME = Name;
            NUM = Num;
            COLOR = color;
            WEIGHT = weightt;
        }


        public string color
        {
            get { return COLOR; }
            set
            {
                foreach(string i in colors)
                {
                    if (value == i) 
                    error = false;
                    break;
                }
            }
        }

        public double weightt
        {
            get { return WEIGHT; }

            set { WEIGHT = value; }


        }



        
    }`

}

There is serious conceptual confusion in coding.编码中存在严重的概念混淆。 I edited the code you want to do below and developed a test application.我在下面编辑了您要执行的代码并开发了一个测试应用程序。 I recommend reading this article to get a grasp of the classes.我建议阅读这篇文章来掌握这些课程。 The test application produces the following output:测试应用程序生成以下 output:

Name: John, Number: 15, Color: Chartreuse, Weight: 50.75姓名:约翰,编号:15,颜色:黄绿色,重量:50.75

using System;

namespace Test
{
    public class Experiment
    {
        // "STATIC VARIABLE": You must define "static" data that takes a constant value inside the class.
        private static string[] colors = new string[6] {"Crimson", "Azure", " Taupe", "Mauve", "Vermillion", "Chartreuse"};
        
        // "FIELDS": Fields are generally defined as "private" and are closed to external setting.
        private string _name;
        private int _number;
        private string _color;
        private double _weight;
        private double _volume;
        public bool _errorState;
        
        // "CONSTRUCTOR": The constructor is used to initialize an object. It is not necessary to assign directly to fields without checking the value.
        public Experiment(string name, int number, string color, double weight)
        {
            _name = name;     // Not recommended use; the value is assigned to the field without checking it.
            _number = number; // Not recommended use; the value is assigned to the field without checking it.
            Color = color;    // Recommended Usage; in the set method of the Property, the field is checked before assigning a value. However, there is a logic error in this usage.
            Weight = weight;  // Not recommended use; property's set method does not check before assigning the field.
        }

        // "PROPERTIES": The properties provide the interface for the "private" defined fields to be accessible.
        public string Color
        {
            get { return _color; }
            
            set
            {
                foreach(string i in colors){
                    if (value == i) {
                        _errorState = false;
                    } else {
                        _color = i; 
                    }
                }
            }
        }

        public double Weight
        {
            get { return _weight; }
            
            set { _weight = value; }
        }
        
        public string Name
        {
            get { return _name; }
        }
        
        public int Number
        {
            get { return _number; }
        }
    }
}
                    
public class Program
{
    public static void Main()
    {
        Test.Experiment experiment = new Test.Experiment("John", 15, "blue", 50.75);
        Console.WriteLine("Name: {0}, Number: {1}, Color: {2}, Weight: {3}", experiment.Name, experiment.Number, experiment.Color, experiment.Weight);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当数据库表中没有空值时获取空值 - Getting Null value when there is no null in Database table 当我尝试 Output 时出现错误 从下拉列表中选择了什么 - Getting an Error When I Try To Output What Was Chosen From a Dropdown 与AJAX一起传递时,在IActionResult中获取空值 - Getting a null value in IActionResult when passed with AJAX 当 mocking 时,IConfiguration 不断获得 null 值 - IConfiguration keeps getting null value when mocking 尝试检索 datagridview 列值“15 位”时出错 - getting an error when try to retrieve datagridview column value "15 digits" 从存储过程中的sqldatasource更新事件中的输出值获取null - Getting null from output value in sqldatasource updated event from storedprocedure 当我尝试在我的代码中进行空检查时,为什么会出现错误 - Why am i getting an error when i try to put a null check in my code 可空引用类型:“Try”方法模式,返回 false 时收到 null 警告 - Nullable reference types: “Try” method pattern, getting warning for null when returning false 当我尝试通过存储过程插入空值时获取SqlException - Getting a SqlException when I try to insert null values through a stored procedure 当我尝试使用LINQ镜像数据库时,为nvarchar类型的行获取NULL - Getting NULL for a row a type nvarchar when I try to use LINQ to mirror my database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM