Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I know this is going to be a basic question for you but I'm trying to create a code where a class called "Program2" would pass value to a class called "Holder" and Holder suppose to accumulate the value that was being transferred by Program2. And Once I retrieve the value from Holder, the value should be added by 1. I hope this makes sense. To better understand my goals for this code, I will include the current output and the desired output. I have also included the actual code for Program2 and Holder below. Take note, i'm really a beginner in coding so please bare with my explanation and coding terminologies.

My output right now is like this:
**********First Loop**********
Total loop: 3
This is the output produced from Holder Class: 3
Total loop passed 1st time: 4

**********Second Loop**********
This is the output produced from Holder Class: 3
Total loop passed 1st time: 4

What i want to happen is for the output to be like this:
**********First Loop**********
Total loop: 3
This is the output produced from Holder Class: 3
Total loop passed 1st time: 4

**********Second Loop**********
This is the output produced from Holder Class: 6
Total loop passed 1st time: 7

As you can see from my desired output, i want the Second Loop to add/accumulate the number of loops that the foreach loop produces. And hopefully it will just keep on stacking up the value as it goes on the Third or Fourth Loop.

What I have tried:

Below is the code for Program2:
C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestCode
{
class Program2
{
    private static int x = 0;
     static void Main()
    {
        int[] array = new int[] { 1,2,3};
        foreach (int element in array)
        {
            x++;
        }

        Console.WriteLine("**********First Loop**********");
        Console.WriteLine("Total loop: {0}", x);
        Holder h1 = new Holder();
        h1.setValue(x);
        h1.display();
        Console.WriteLine("Total loop passed 1st time: {0}",h1.getValue());

        SecondLoop();

        Console.ReadKey();          
    }

    public static void SecondLoop()
    {
        Console.WriteLine();
        Console.WriteLine("**********Second Loop**********");
        Holder h1 = new Holder();
        h1.setValue(x);
        h1.display();
        Console.WriteLine("Total loop passed 2nd time: {0}", h1.getValue());
    }

 }
 }


Here is code for Holder:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestCode
{
public class Holder
{
    private int x = 0;
    public Holder()
    {

    }

    public void setValue(int x1)
    {
        x = x1;
    }

    public int getValue()
    {
        return x+1;
    }

    public void display()
    {
        Console.WriteLine("This is the output produced from Holder Class: {0}",x);
    }

  }
 }


Feel free to suggest better alternatives for this to produce the desired result that i have mentioned above. It can be a total different approach or you can basically build up the code that i have wrote.
Posted
Updated 7-May-17 4:53am

1 solution

The problem is that you are using two separate instances of Holder - you create one in main and a second one in SecondLoop. And because they are different instances they have independent values - they each count for themselves. It's the same with cars: if you have a car and I have a car; you put your mobile in the glovebox of your car - you don't expect to find it in the glovebox of my car because we "know" that each vehicle is a separate instance of the Car class.
You can do it - that's what static variables are there for - but I suspect that it won't work even then because you have a setValue method on the Holder and your code calls it in both cases - so the SecondLoop code would "throw away" the value that accumulated in main.

What I would do is pass the instance of Holder from main to SecondLoop as a parameter.
And I'd also change it a bit to put the increment in the Holder class:
public class Holder
{
    private int _X = 0;
    public int X
    {
        get { return _X; }
        set { _X = value; }
    }
    public int Inc(int inc = 1)
    {
        _X += inc;
        return _X;
    }
    public void display()
    {
        Console.WriteLine("This is the output produced from Holder Class: {0}", _X);
    }
}
 
Share this answer
 
v3
Comments
BebeSaiyan 7-May-17 11:25am    
i replaced my code in Holder into your solution but why is it giving me ton of errors? like for example public X, the X does not exist in the current context, actually pretty much everything doesnt exist in the current context. Also it gives me, invalid token |_.
OriginalGriff 7-May-17 11:48am    
Not sure what happened there - probably finger trouble at my end - I've fixed it.
BebeSaiyan 7-May-17 12:13pm    
i fixed it as well. public X should be public int X
OriginalGriff 7-May-17 12:21pm    
Yep! Total brain fart on my part there... :O
BebeSaiyan 8-May-17 3:37am    
All is working now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900