简体   繁体   中英

how to use variables of a class from other classes in c#

i need to call variables that are defined in a class

class testclasss
{
    public testclass(double[,] values)
    {
        double[][] rawData = new double[10][];  
        for (int i = 0; i < 10; i++)
        {
            rawData[i] = new double[] { values[i, 2], stockvalues[i, 0] };  // if data won't fit into memory, stream through external storage
        }
        int num = 3;  
        int max = 30; 
    } 

    public int[] checkvalue(double[][] rawData, int num, int maxCount) 
    {
        ............
    }
}

I have called constructor using

    testclass newk = new testclass(values);

now how can i call the function checkvalues(). i tried using newk.num,newk.maxcount but those variables were not recognized.

Just as your class constructor and checkvalues function are public, so too must the properties you wish to access from outside the class. Put these at the top of your class declaration:

public int num {get; set;}
public int max {get; set;}

Then you can access them via newk.num and newk.max.

EDIT: In response to your second comment, I think you might be a bit confused about how functions and properties interact within the same class. This might help:

class TestClass {
    private int _num;
    private int _max;
    private double[][] _rawData;

    public TestClass(double[,] values, int num, int max) 
    {
        _num = num;
        _max = max;
        _rawData = new double[10][]; 
        for (int i = 0; i < 10; i++) 
        {
            _rawData[i] = new double[] { values[i, 2], stockvalues[i, 0] };
        }
    }

    public int[] CheckValues() 
    {
        //Because _num _max and _rawData exist in TestClass, CheckValues() can access them.
        //There's no need to pass them into the function - it already knows about them.
        //Do some calculations on _num, _max, _rawData.  
        return Something;          
    }
}

Then, do this (for example, I don't know what numbers you're actually using):

double[,] values = new double[10,10];
int num = 3;
int max = 30;
Testclass foo = new Testclass(values, num, max);
int[] results = foo.CheckValues();

I believe this is what you're looking for:

class TestClass {

    public int num { get; set; }  
    public int max { get; set; }   

    public double[][] rawData;

    public TestClass(double[,] values)
    {
           rawData = new double[10][];  
           for (int i = 0; i < 10; i++)
           {
               rawData[i] = new double[] { values[i, 2], stockvalues[i, 0] };  // if data won't fit into memory, stream through external storage
           }
           this.num = 3;  
           this.max = 30; 
     } 

     public int[] CheckValue() 
     {............}      
  }

You'll need a property with a get accessor.

Here is a simplified example based on your code:

class testclass
{
   private int _num = 0;
   private int _max = 0;

   public int Num
   {
      set { _num = value; }
      get { return _num; }
   }

   public int Max
   {
      set { _max = value; }
      get { return _max; }
   }

   public testclass()
   {
            Num = 3;  
            Max = 30; 
   }    
}

//To access Num or Max from a different class:

testclass test = new testclass(null);
Console.WriteLine(test.Num);

Hope that helps.

Using your exact same code, except for public testclass(double[,] values) , which I changed to public testfunction(double[,] values)

class testclass
{
    public testfunction(double[,] values)
    {
        double[][] rawData = new double[10][];  
        for (int i = 0; i < 10; i++)
        {
        rawData[i] = new double[] { values[i, 2], stockvalues[i, 0] };  // if data won't fit into memory, stream through external storage
        }
        int num = 3;  
        int max = 30; 
    } 

    public int[] checkvalue(double[][] rawData, int num, int maxCount) 
    {
        ............
    }
}

Have you tried calling checkvalues() function like this?

testclass.checkvalue();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM