[英]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.