2014-04-14 113 views
1
using System; 

namespace Matrix_Algebra 
{ 
    public struct S_Matrix_size 
    { 
     public int size_R, size_C; 
     public S_Matrix_size(int r, int c) 
     { 
      this.size_R = r; 
      this.size_C = c; 
     } 
    } 

    public class C_Matrix_entries 
    { 

     public C_Matrix_entries() 
     { 
      int r, c; 
      Console.WriteLine("Enter number of rows and columns "); 

      r = Convert.ToInt32(Console.ReadLine()); 
      c = Convert.ToInt32(Console.ReadLine()); 

      S_Matrix_size size = new S_Matrix_size(r,c); 

      double[,] entry = new double [size.size_R,size.size_C]; 

      Console.WriteLine("Enter the entries from first row [left to right] to the last row "); 
      for (int i = 0; i<size.size_R; i++) 
      { 
       Console.WriteLine("Enter the {0} row", i + 1); 
       for (int j = 0; j<size.size_C;j++) 
       { 
        entry[i, j] = Convert.ToDouble(Console.ReadLine()); 
       } 
      }  
     }  


    } 
} 

namespace Row_Reduce_Algebra 
{ 
    using Matrix_Algebra; 
    public class TEST 
    { 
     static void Main() 
     { 
      C_Matrix_entries matrix_no1 = new C_Matrix_entries(); 
      for (int i = 0; i < **matrix_no1.size**; i++) 
      { 

      } 
     } 
    } 
} 

正如標題所說,我試圖從一個類實例中訪問一個變量,但不知道如何正確執行。參考不同類別的變量

回答

1

由於@GrantWinney理所當然指出的(因爲我塑造爲你工作的回覆),您將無法訪問matrix_no1.size,因爲它是不可訪問。 (它也超出範圍,matrix_no1是在默認的C_Matrix_entries構造函數中聲明的局部變量。)

根據您的代碼,這裏是一個端到端的工作示例,說明如何使用某種方法修復問題不同的公共財產加入C_Matrix_entries。除了您添加到C_Matrix_entries(即,Grant Winney's也會工作)的新S_Matrix_size公共財產的味道之外,您需要計算您的循環設置中的size_Rsize_C屬性的乘積。

using System; 

namespace Matrix_Algebra 
{ 
    public struct S_Matrix_size 
    { 
     public int size_R, size_C; 
     public S_Matrix_size(int r, int c) 
     { 
      this.size_R = r; 
      this.size_C = c; 
     } 
    } 

    public class C_Matrix_entries 
    { 
     private S_Matrix_size _size; 

     public C_Matrix_entries() 
     { 
      int r, c; 
      Console.WriteLine("Enter number of rows and columns "); 

      r = Convert.ToInt32(Console.ReadLine()); 
      c = Convert.ToInt32(Console.ReadLine()); 
      _size = new S_Matrix_size(r,c); 

      double[,] entry = new double [_size.size_R,_size.size_C]; 

      Console.WriteLine("Enter the entries from first row [left to right] to the last row "); 
      for (int i = 0; i<_size.size_R; i++) 
      { 
       Console.WriteLine("Enter the {0} row", i + 1); 
       for (int j = 0; j<_size.size_C;j++) 
       { 
        entry[i, j] = Convert.ToDouble(Console.ReadLine()); 
       } 
      }  
     }  

     public S_Matrix_size Size { get { return _size; } } 
    } 
} 

namespace Row_Reduce_Algebra 
{ 
    using Matrix_Algebra; 
    public class TEST 
    { 
     static void Main() 
     { 
      C_Matrix_entries matrix_no1 = new C_Matrix_entries(); 
      for (int i = 0; i < matrix_no1.Size.size_R * matrix_no1.Size.size_C; i++) 
      { 
       // TODO: something useful 
       Console.WriteLine(i); // FORNOW 
      } 
     } 
    } 
} 
2

您無法訪問matrix_no1.size,因爲size無法訪問。

將公共財產添加到您的C_Matrix_entries類中,並在Main()中引用它。

public class C_Matrix_entries 
{ 
    public S_Matrix_size size { get; private set; } 

    public C_Matrix_entries() 
    { 
     ... 

     size = new S_Matrix_size(r,c);