Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying values from a private int in c#

Tags:

c#

private

public

Here is the code

I can´t get the limit counter to increment for each time I can get it to count to 1 between each outputline, but that´s it Any idea why? I want it to be able to count every "overshoot"

class Actuator
{
    private int limit_count = 0;

    public int Inc_Limit_counter(int temp,int co2_conc,int rel_humid)
    {
        if(temp > 70 || co2_conc > 450 || rel_humid > 77)
            limit_count++;

        //Console.WriteLine("test {0}",limit_count);
         return limit_count;
    }

    public int Get_limit_count()
    {
        return limit_count;
    }
}


class Program
{
    static int read_random_values()
    {
        Random r = new Random();
        int temp, co2_conc, rel_humid, i;

        Console.WriteLine("Temperature in celcius:");
        for (i = 0; i <= 100; i++)
        {
            temp = r.Next(-50,50);
            co2_conc = r.Next(300,600);
            rel_humid = r.Next(0,100);

            Console.WriteLine("The temperature is: {0}, Co2 concentration is: {1} and Relative Humidity is: {2}",temp,co2_conc,rel_humid);

            Actuator Counter1 = new Actuator();
            Counter1.Inc_Limit_counter(temp, co2_conc, rel_humid);     
        }

        return 0;
    }


    static void Main()
    {
        read_random_values();
        Actuator object1 = new Actuator();
        object1.Get_limit_count();
    }
}
like image 966
mrcool Avatar asked Nov 08 '22 08:11

mrcool


1 Answers

The Problem is that you are creating a new instance of the Actuator class each time you call the Inc Method. So your counter will be increased one time and then the object is not used any more. I think you are missing the static Keywords for the counter and the two Methods of the Actuator class.

like image 101
Thomas Voß Avatar answered Nov 14 '22 22:11

Thomas Voß