Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

attribute in class can't be saved why

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading