如何在c#中使用其他类的变量

时间:2013-07-22 14:37:50

标签: c#

我需要调用类

中定义的变量
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 个答案:

答案 0 :(得分:3)

正如您的类构造函数和checkvalues函数是公共的一样,您希望从类外部访问的属性也是如此。将它们放在类声明的顶部:

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

答案 1 :(得分: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() 
     {............}      
  }

答案 2 :(得分:1)

您需要一个带有访问权限的属性。

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

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

希望有所帮助。

答案 3 :(得分:0)

使用完全相同的代码,但公共测试类(双[,]值)除外,我将其更改为公共测试功能(双[,]值)

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