0

This is my code :

the purpose is to add two big numbers ,First input two numbers in 2 array then swaping both and adding them , Console - Based ,

I'm a newbie in C# so please explain keeping in mind my few knowledge of codes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sum_Two_BIG_Numbes
{
    public class matrix
    {

        public int[] c;

        public void input(int[] a)
        {
            for (int i = 0; i < a.Length; i++)
            {

                a[i] = Convert.ToInt32(Console.ReadLine());

            }
        }
        public void Jamk(int[] a, int[] b, int[] c)
        {

            for (int i = 0; i < a.Length; i++)
            {
                int temp = a[i] + b[i];
                if ((temp < 10) && (c[i] != 1))
                {
                    c[i] = c[i] + temp;
                }
                else
                {
                    c[i] = c[i] + temp % 10;
                    c[i + 1] = c[i + 1] + temp / 10;

                }
            }


        }
        public void swap(int[] a)
        {

            for (int i = 0; i < a.Length; i++)
            {
                a[i] = a[a.Length - i];
            }

        }
    }
    class Program
    {
        public void Main(string[] args)
        {
            int[] a = new matrix();
            //int[] a = new int[30];
            int[] b = new int[30];
            int[] c = new int[30];
            Console.WriteLine("Enter First Number : ");
            matrix.input(a);


            Console.ReadLine();
        }
    }
}

I get this error "An Object Refrence is requiered for the non-static field . . . . ,"

  • Which line in the code shows that error? – Steven Doggart Jul 12 '12 at 20:17
  • simplest way to change this and gain access would be to change the Public Void methods to public static using the keyword "static" – MethodMan Jul 12 '12 at 20:20
  • possible duplicate of [An object reference is required for the nonstatic field, method, or property 'WindowsApplication1.Form1.setTextboxText(int)](http://stackoverflow.com/questions/498400/an-object-reference-is-required-for-the-nonstatic-field-method-or-property-wi) – Spontifixus Mar 19 '14 at 17:48

3 Answers3

2
matrix.input(a);

Where is matrix declared (it isn't).

int[] a = new matrix();

Additionally, you cannot assign an instance of matrix to an int[]. There is no implicit conversion from one to the other. Though I can't say this is a great design, what you wanted was something like this:

matrix a = new matrix();
a.c = new int[SomeSize];
// more code
a.input(b);  // or something similar...
Ed S.
  • 122,712
  • 22
  • 185
  • 265
0

You need to create a new instance of class matrix inside the Main method like this: matrix instance = new matrix(); Then you can call the methods on the matrix class. Read up on differences between static and instance methods/properties/fields.

laconicdev
  • 6,360
  • 11
  • 63
  • 89
0

your code doesn't really make sense, but here's what i think you mean

public void Main(string[] args)
        {
            matrix m = new matrix();
            int[] a = new int[30];
            int[] b = new int[30];
            int[] c = new int[30];
            Console.WriteLine("Enter First Number : ");
            m.input(a);


            Console.ReadLine();
        }

now, that will get you past the first round of compile errors, but it still doesn't make sense. regarding your input/output.

i suggest you read up on the usage of Console.ReadLine()