[SOLVED] attribute in class can't be saved why

Issue

I’m trying to coding in c#, I have two main classes first one is router and this is it’s implementation

public class Router
{
    public int RouterValue = 0;
}

this I use this class in another class called Route which contains list of routers and this is implementation :

public class Route
    {
        public List <Router> routers = new();
        bool exitLoop = false;
        public void ReadInput()
        {
            Router router = new();
            Console.WriteLine(" \n Please enter data Input : \n ");
            for (int i = 0; !exitLoop ; i++)
            {
                router.RouterValue = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("router value" , router.RouterValue);
                routers.Add(router);
                
                if (routers[i].RouterValue == 0)
                {
                    exitLoop = true;
                }
            }
        }
        public void OutPut()
        {
            Console.WriteLine("out put : \n");
            for (int i = 0; i < routers.Count; i++)
            {
                Console.WriteLine(routers[i].RouterValue);
            }
        }
        public int count()
        {
            return routers.Count;
        }
        public int sum(List<Router> _routers)
        {
            int sum = 0;
            for (int i = 0; i < routers.Count; i++)
                sum += _routers[i].RouterValue;
            return sum;
        }
    }

but when i create a Roue object and trying to enter values it’s print them as ZERO’S and the sum also equals zero, So where is the problem ? i debug it and trying to put setters ang getters but all of it can’t help, any one have an idea about it ?

this is the part in my main class where i added values :

Routes.Add(op.CreatRoute());
                Routes[^1].ReadInput();
                Routes[^1].OutPut();

Solution

You’re only ever creating one Router object:

Router router = new();

Everything you do just updates that one object. And the last value you enter (to terminate the loop) is 0, so the last value that one Router object ever has is 0.

(This is a good opportunity to step through the code in a debugger and observe the runtime values of your variables. You’ll find that the routers field on your Route object always has exactly one element in it, no matter how many times you iterate that loop.)

Create the new Router object inside the loop instead:

for (int i = 0; !exitLoop ; i++)
{
    Router router = new(); // <--- here
    router.RouterValue = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("router value" , router.RouterValue);
    routers.Add(router);
            
    if (routers[i].RouterValue == 0)
    {
        exitLoop = true;
    }
}

That way each iteration of the loop creates (and appends to the list) an entirely new instance of a Router, rather than always operating on the same instance.

As an aside, this is also a good practice of keeping variables defined only in the scope where you need them. This makes the code overall simpler and easier to follow, and helps you encapsulate functionality as the complexity of an application grows.

Answered By – David

Answer Checked By – Mary Flores (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *