0

我需要调用类中定义的变量

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) 
    {
        ............
    }
}

我已经调用了构造函数

    testclass newk = new testclass(values);

现在我如何调用函数 checkvalues()。我尝试使用 newk.num,newk.maxcount 但无法识别这些变量。

4

4 回答 4

3

正如您的类构造函数和检查值函数是公共的一样,您希望从类外部访问的属性也必须是公共的。将这些放在类声明的顶部:

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

然后您可以通过 newk.num 和 newk.max 访问它们。

编辑:针对您的第二条评论,我认为您可能对函数和属性如何在同一个类中交互感到有些困惑。这可能会有所帮助:

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;          
    }
}

然后,执行此操作(例如,我不知道您实际使用的是什么数字):

double[,] values = new double[10,10];
int num = 3;
int max = 30;
Testclass foo = new Testclass(values, num, max);
int[] results = foo.CheckValues();
于 2013-07-22T14:40:27.590 回答
2

我相信这就是您正在寻找的:

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() 
     {............}      
  }
于 2013-07-22T14:45:08.573 回答
1

您需要一个带有 get 访问器的属性。

这是基于您的代码的简化示例:

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);

希望有帮助。

于 2013-07-22T14:48:14.567 回答
0

使用完全相同的代码,除了public testclass(double[,] values),我将其更改为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) 
    {
        ............
    }
}

您是否尝试过像这样调用 checkvalues() 函数?

testclass.checkvalue();
于 2013-07-22T18:16:47.047 回答