简体   繁体   中英

Using methods on arrays

I am very new to C#, and trying to make a program that counts an array, and I'm having trouble using methods/properties on the array (Reset, PrintCounters, Increment). the problems occur from the for loops and below. Thanks in advance if anyone is able to help.

using System;

namespace CounterTest
{
    public class MainClass
    {

        private static void PrintCounters(Counter[] counters)
        {
            foreach (Counter c in counters)
            {
                string name = "";
                int value = 0;

                Console.WriteLine("{0} is {1}", name, value);
            }
        }
        public static void Main(string[] args)
        {
            Counter[] myCounters = new Counter[3];

            myCounters[0] = new Counter("Counter 1");
            myCounters[1] = new Counter("Counter 2");
            myCounters[2] = myCounters[0];

            for (int i = 0; i < 4; i++)
            {
               Counter.Increment(myCounters[0]);
            }
            for (int i = 0; i < 9; i++)
            {
               Counter.Increment(myCounters[1]);
            }
            Counter.PrintCounters(myCounters);
            Counter.Reset(myCounters[2]);
            Counter.PrintCounters(myCounters);
        }
    }
}

Counter class:

using System;
using System.Collections.Generic;
using System.Text;

namespace CounterTest
{
    public class Counter
    {
        private int _count;
        private string _name;

        public Counter(string name)
        {
            _name = name;
            _count = 0;
        }
        public void Increment()
        {
            _count++;
        }
        public void Reset()
        {
            _count = 0;
        }

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        public int Value
        {
            get
            {
                return _count;
            }
            set
            {
                _count = value;
            }
        }
    }
}

The methods you call are not static methods, so they are called like:

for (int i = 0; i < 4; i++)
{
   myCounters[0].Increment();
}
for (int i = 0; i < 9; i++)
{
   myCounters[1].Increment();
}
MainClass.PrintCounters(myCounters); //this is static
myCounters[2].Reset();
MainClass.PrintCounters(myCounters);

You use Counter.Increment(myCounters[0]) like Increment was an extention method on Counter.

public static class ExtentionCounter
{
    public static void Increment(this Counter cnt)
    {
        cnt.Value++;
    }
} 

When with your current definition you should use :

myCounters[0].Increment();

Counter is a type , not an instance , that's why Counter.Increment is an incorrect call ( Increment is not a static method).

 // Given intance of Counter - myCounters[1] call Increment() method on it
 myCounters[1].Increment(); 

instead of

 // Call static method of Counter class - Counter.Increment on instance of Counter
 Counter.Increment(myCounters[1]);

etc. It can be something like this:

    public static void Main(string[] args)
    {
        Counter[] myCounters = new Counter[3]

        myCounters[0] = new Counter("Counter 1");
        myCounters[1] = new Counter("Counter 2");
        myCounters[2] = myCounters[0];

        for (int i = 0; i < 4; i++)
        {
           // call Increment on myCounters[0] instance
           myCounters[0].Increment();
        }
        for (int i = 0; i < 9; i++)
        {
           // call Increment on myCounters[1] instance
           myCounters[1].Increment(); 
        }            

        // PrintCounters method call
        PrintCounters(myCounters);

        // call Reset on myCounters[2] instance
        myCounters[2].Reset();

        // PrintCounters method call 
        PrintCounters(myCounters);
    }

You are calling Counter.Increment and then providing a Counter as a parameter. This piece of code assumes Counter is a static class with static methods, which is not the case.

In your for loops, you should be using the code like this:

for (int i = 0; i < 4; i++)
{
    myCounters[0].Increment();
}

here is the solution for your code. Just compare with yours but see the changes are with ** . On the PrintCounters since your looping trough the Counters as c you need to call the counter name and the valu with **c.Name and c.Value

using System;
using Test;

namespace teste
{
   static class MainClass{

        public static void PrintCounters(Counter[] counters)
        {
            foreach (Counter c in counters)
            {

                **string name = c.Name;**
                **int value = c.Value;**

                Console.WriteLine("{0} is {1}", name, value);
            }
        }
   }
    class Program
    {
        static void Main(string[] args)
        {

            Counter[] myCounters = new Counter[3];

            myCounters[0] = new Counter("Counter 1");
            myCounters[1] = new Counter("Counter 2");
            myCounters[2] = myCounters[0];

            for (int i = 0; i < 4; i++) {

              **myCounters[0].Increment();**
            }
            for (int i = 0; i < 9; i++) {

              **myCounters[1].Increment();**
            }

            **MainClass.PrintCounters(myCounters);**
            **myCounters[2].Reset();**
            **MainClass.PrintCounters(myCounters);**
        }
    }
}

Also since the myCounters is an instance of Counters you need to call the method of the instance like this: myCounters[0].Increment() Same for the other methods as Reset. To call a static method you dont need to instantiate but in your case you need to do reference to the class to use the method PrintCounters like this: MainClass.PrintCounters(myCounters);

Also use the keyword this.something to change instance variables.

using System;
using System.Collections.Generic;
using System.Text;

namespace Test
{
    public class Counter
    {
        private int _count;
        private string _name;

        public Counter(string name)
        {
           **this._name = name;**
            **this._count = 0;**
        }
        public void Increment()
        {
           **this._count++;**
        }
        public void Reset()
        {
            **this._count = 0;**
        }

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        public int Value
        {
            get
            {
                return _count;
            }
            set
            {
                _count = value;
            }
        }
    }
}

i hope it helps

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