this is the summary of program and i only include the addition of matrices my problem would be the variables in Addition class cannot read variables from the other class i need to know how to fix that the error would be :

The name 'a' does not exist in the current context
The name 'b' does not exist in the current context

that is why i cant sum the array to give the result help! thanks in advance

*****Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace matrix
{
    class Program
    {
        static void Main(string[] args)
        {
            matrix.TypeOfMatrix.TypeSel();
        }


    }
}


*******TypeOfMatrix


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace matrix
{
    class TypeOfMatrix
    {
        public static void TypeSel()
        {

            Console.WriteLine("Two Matrices are to be entered with the selection of different types");


            Console.WriteLine("1-Regular Matrix:");
            Console.WriteLine("2-Lower Triangular Matrix:");
            Console.WriteLine("3-Upper Triangular Matrix:");
            Console.WriteLine("4-Sparse Matrix:");

            for (int i= 0; i<2; i++)
            {

            Console.WriteLine("Type of Matrice:");
            string s = Console.ReadLine();
            int e2 = int.Parse(s);
            switch (e2)
            {

                case 1:
                    Console.WriteLine("\nRegular Expression");


                    matrix.RegularMatrix.Reg();
                    matrix.Operations.OpSelect();


                    break;


                default:

                    Console.WriteLine("Invalid Entry (Please Try Again)");
                    matrix.TypeOfMatrix.TypeSel();
                    break;




            }

            }
        }
    }
}
******RegularMatrix

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace matrix
{
    public class RegularMatrix
    {
         public static int k = 0, l = 0, m = 0, n = 0;

        int[,] a = new int[10, 10];
        int[,] b = new int[10, 10];
        int[,] c = new int[10, 10];
        int[,] d = new int[10, 10];



        public static void Reg()
        {

        RegularMatrix cs = new RegularMatrix();


        cs.ReadMatrix();
        cs.arrange();


        //Console.WriteLine();

        //matrix.Operations.OpSelect();

        }

        public void ReadMatrix()
        {

            Console.Write("\n Enter the size:");
            int m = int.Parse(Console.ReadLine());
            int n = m;
            a = new int[m, n];
            Console.WriteLine("\n Enter the elements:");
            for (int i = 0; i < a.GetLength(0); i++)
            {
                for (int j = 0; j < a.GetLength(1); j++)
                {
                    a[i, j] = int.Parse(Console.ReadLine());
                }
            }



            //Console.WriteLine("\n Size of Matrix 2 :");
            //Console.Write("\n Enter the number of rows in Matrix 2 :");
            //m = int.Parse(Console.ReadLine());
            //Console.Write("\n Enter the number of columns in Matrix 2 :");
            //n = int.Parse(Console.ReadLine());
            //b = new int[m, n];
            //Console.WriteLine("\n Enter the elements of Matrix 2:");
            //for (int i = 0; i < b.GetLength(0); i++)
            //{
            //    for (int j = 0; j < b.GetLength(1); j++)
            //    {
            //        b[i, j] = int.Parse(Console.ReadLine());
            //    }
            //}

        }

        public void arrange()
        {
            Console.WriteLine("\n Matrix 1:");
            for (int i = 0; i < a.GetLength(0); i++)
            {
                for (int j = 0; j < a.GetLength(1); j++)
                {
                    Console.Write("\t" + a[i, j]);
                }
                Console.WriteLine();
            }



            //Console.WriteLine("\n Matrix 2:");
            //for (int i = 0; i < b.GetLength(0); i++)
            //{
            //    for (int j = 0; j < b.GetLength(1); j++)
            //    {
            //        Console.Write("\t" + b[i, j]);
            //    }
            //    Console.WriteLine();
            //}


        }

    }
}

**** Operations
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace matrix
{
    class Operations
    {
        public static void OpSelect()
        {
            Console.WriteLine("\n");
            Console.WriteLine("Enter the type of operation:");
            Console.WriteLine("1-Addition of the matrices:");
            Console.WriteLine("2-Subtraction of the matrices:");
            Console.WriteLine("3-Multiplication of the matrices:");


            string s = Console.ReadLine();
            int e = int.Parse(s);
            switch (e)
            {
                case 1:
                    Console.WriteLine("Addition");
                    matrix.Addition.RegAdd();
                    ;
                    break;



                default:
                    Console.WriteLine("Invalid entry");
                    break;

            }
        }
    }
}


**** Addition

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace matrix
{
   class Addition
    {


         public static void RegAdd(){





            Console.WriteLine("Matrix 1 + Matrix 2: ");
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {   

                   Console.Write(a[i, j] + b[i, j] + " ");
                } Console.WriteLine();
            } Console.WriteLine();
    }
}

}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

a,b,c,d are instance variables and you are treating them like they are class variables. You either need to instantiate an object of type RegularMatrix or declare them as static.

static int[,] a = new int[10, 10];
static int[,] b = new int[10, 10];
static int[,] c = new int[10, 10];
static int[,] d = new int[10, 10];

i tried tihs one still the same problem :(

you may not always want to put public on your class variables. if you give access to them through a property you can also do data validation before the user crashes your program. the first one is an automatic property, the system will automatically make its variables. the second one is manual and it allows you more flexibility.

public int i {get;set;}

int ix = 0;
public int i
{
    get
    {
        return iX;
    }
    set
    {
        if(value > 10)
            iX = value;
    }
}

This is how you can access the varialbes (or accessors) on the other classes and pass data into them:

namespace Nov22Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass1 class1 = new MyClass1();
            class1.SendingMessage();
        }   
    }

    class MyClass1
    {
        public void SendingMessage()
        {
            string str1 = "This message is going from class 1 to class 2.";
            MyClass2 myClass2 = new MyClass2();
            myClass2.str2 = str1;
            myClass2.ShowingMessage();
        }
    }

    class MyClass2
    {
        public string str2 { private get; set; }
        
        public void ShowingMessage()
        {
            Console.WriteLine("{0}", str2);
            Console.ReadLine();
        }
    }
}

Edited 6 Years Ago by Mitja Bonca: n/a

This article has been dead for over six months. Start a new discussion instead.