Roberthamara
Roberthamara

Reputation: 3

How to move 1 variable from a class to another class

The issue with this program is that after running the Data class to move the target variable over to the Program class, so It can be used in the mean calculation.

class Program
{
    static void Main(string[] args)
    {
        int[,] array1 = new int[3, 3]
            { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
        double mean = 0;
    
        Data Datacollection = new Data();            


        //This is where I want the target variable from the data class
        mean = (double) 1800 / target;

        
        Console.WriteLine("The amount of numbers with 2 digits are {0} in a 1800 data graph", target);
        Console.WriteLine("With a mean of {0} in 1800",mean.ToString("0.00"));
        
    }
}



class Data
{

    private int[,] array1 = new int[3, 3]
        { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };

    private int target = 0;
    
    public Data(int Datacollection)
    {            
        Random RandNum = new Random();

        for (int m = 0; m < 200; m++)
        {
            for (int i = 0; i < 3; i++)
            {

                for (int j = 0; j < 3; j++)
                {
                    array1[i, j] = RandNum.Next(0, 5000);
                    Console.Write(" {0} ", array1[i, j]);

                    if (array1[i, j] < 99)
                    {
                        Console.WriteLine("Required data type and required fields found, Data colleted");

                        target = target + 1;
                        
                    }
                }
                Console.WriteLine();
            }            
        }           
    }
}

Upvotes: 0

Views: 79

Answers (2)

Mong Zhu
Mong Zhu

Reputation: 23732

You should rather create a method that calculates what you need and returns the value instead of doing the calculation inside the constructor:

public int CalculateTarget()
{

    int[,] array1 = new int[3, 3]
        { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
    int target = 0;
    Random RandNum = new Random();

    for (int m = 0; m < 200; m++)
    {
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                array1[i, j] = RandNum.Next(0, 5000);
                Console.Write(" {0} ", array1[i, j]);

                if (array1[i, j] < 99)
                {
                    Console.WriteLine("Required data type and required fields found, Data colleted");

                    target = target + 1;
                    
                }
            }
            Console.WriteLine();
        }        
    }
    return target;
}

Then you can call the method in your main:

    //This is where I want the target variable from the data class
    int targetFromData = Datacollection.CalculateTarget();
    mean = (double) 1800 / targetFromData;

A method yields the advantage that it can communicate/document its purpose via its name. If someone else will use your Data object, then it will not be obvious that there is some magic calculation going on already when the object is instantiated

Upvotes: 1

Prasad Telkikar
Prasad Telkikar

Reputation: 16049

Below are the ways to get target variable from Data class to the main method,

  1. Create a property called target in Data class, assign value to it and read value from that property in main method.

    public class Data
    {
    
       public int Target { get; set; } = 0;
    
       public Data(int Datacollection)
       { 
          .... 
    
          this.Target++; //instead of target = target + 1
       }
    

Now use it in Main() method,

public  static void Main(string[] args)
{
    ...
    
    Data Datacollection = new Data();            


    mean = (double) 1800 / Datacollection.Target;

 ...   
}
  1. Write separate function in Data class, return target value to the caller.

    public int CalculateTarget()
    {
     int[,] array1 = new int[3, 3]{ { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
     int target = 0;
     Random RandNum = new Random();
    
     for (int m = 0; m < 200; m++)
     {
         for (int i = 0; i < 3; i++)
         {
             for (int j = 0; j < 3; j++)
             {
                 array1[i, j] = RandNum.Next(0, 5000);
                 Console.Write(" {0} ", array1[i, j]);
                 if (array1[i, j] < 99)
                 {
                     Console.WriteLine("Required data type and required fields found, Data colleted");
    
                     target++;
                 }
             }
             Console.WriteLine();
         }
    
     }
      return target;     
    }
    

Now Call this method in your Main() function,

public  static void Main(string[] args)
{
    ...
    
    Data Datacollection = new Data();            

    //Call here
    var target = Datacollection.CalculateTarget();
    mean = (double) 1800 / target;

 ...   
}

Upvotes: 1

Related Questions