简体   繁体   English

当前上下文中不存在如何解决名称“名称”和“值”的问题

[英]How to fix the name 'Name' & 'Value' does not exist in the current context

I am new to C#, and I am halfway throught a task in which we have to create Counter class and use it to create and work with Counter objects. 我是C#的新手,在完成任务的过程中,我们必须创建Counter类并使用它来创建和使用Counter对象。 I have run into an error that I can't resolve The name 'Name' & 'Value' do not exist in the current context. 我遇到了无法解决的错误。名称“名称”和“值”在当前上下文中不存在。

I have to tell Console, to WriteLine with the format "{0} is {1}", and the result of asking c for its Name, and the result of asking c for its Value. 我必须告诉Console,将格式为“ {0}为{1}”的WriteLine以及要问c的名称和要问c的值的结果。

I have a feeling it has something to do with the scope of Name and Value but after multiple attempts I still can't figure out where I am going wrong 我觉得它与名称和值的范围有关,但是经过多次尝试,我仍然无法弄清楚哪里出了问题

File name Program.cs 文件名Program.cs

namespace CounterTest
{
   public class MainClass
    {
        private static void PrintCounters(Counter[] counters)
        {
            foreach ( Counter c in counters) 
            {
                Console.WriteLine("Name is: {0} Value is: {1}", Name, Value);
            } **** Where I am receiving the error                ^      ^

        }
       public static void Main(string[] args)
        {

        }
    }
}

File name Class1.cs 文件名Class1.cs

namespace CounterTest
{
    public class Counter
    {
        private int _count;
        public int Value
        {
            get
            {return _count;}
        }
        private string _name;
        public string Name
        {
           get
            {return _name;}
            set
            { _name = value; }
        }

        public Counter(string Name)
        { _name = Name;
          _count = 0;}

        public void Increment()
        {
            _count = _count + 1; 
        }
        public void Reset()
        {
            _count = 0;
        }
    }
}

Thank you for any help you are able to provide. 感谢您提供的任何帮助。

Change 更改

Console.WriteLine("Name is: {0} Value is: {1}", Name, Value);

To

Console.WriteLine("Name is: {0} Value is: {1}", c.Name, c.Value);

You can also use string interpolation which is available in C# 6 or higher as: 您也可以使用在C#6或更高版本中可用的字符串插值:

Console.WriteLine($"Name is: {c.Name} Value is: {c.Value}");

The problem is that you don't specify in code where "Name" and "Value" come from and that's why you get that error. 问题是您没有在代码中指定“名称”和“值”的来源,这就是为什么会出现该错误的原因。

foreach ( Counter c in counters) 
{
    Console.WriteLine("Name is: {0} Value is: {1}", Name, Value);
}

In foreach cycle you take a single "c" element from "counters" array. 在foreach循环中,您从“计数器”数组中获取一个“ c”元素。 Afterwards you should specify that "Name" and "Value" are properties of "c" by writing "c.Name" and "c.Value". 之后,您应该通过写入“ c.Name”和“ c.Value”来指定“名称”和“值”是“ c”的属性。

Since using a foreach loop, you can easily use the object properties easily by using the dot operator 由于使用了foreach循环,因此可以通过使用点运算符轻松地使用对象属性

for example 例如

  foreach ( Counter c in counters) 
        {
            Console.WriteLine("Name is: {0} Value is: {1}", c.Name, c.Value);
        } 

or You can use string interpolation for better readability 或者您可以使用字符串插值来提高可读性

foreach ( Counter c in counters) 
        {
            Console.WriteLine($"Name is: {c.Name} Value is: {c.Value}");
        }

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

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