引用不同类的变量

时间:2014-04-14 03:47:12

标签: 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
    {

        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++)
            {

            }
        }
    }
}

正如标题所说,我正在尝试从类实例访问变量,但不知道如何正确地执行它。

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

正如@GrantWinney正确地指出的那样(因为我正在为你制作一份有效的回复),你无法访问matrix_no1.size,因为它无法访问。 (matrix_no1是在默认C_Matrix_entries构造函数中声明的局部变量,这也超出了范围。)

根据您的代码,这是一个端到端的工作示例,说明如何使用添加到C_Matrix_entries的稍微不同的公共属性来解决问题。除了您添加到S_Matrix_size的新C_Matrix_entries公共属性的风格之外(即Grant Winney也可以使用),您需要计算其size_R和{size_C的产品循环设置中的{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
    {
        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
            }
         }
    }
}