locked
in c#, how do I create a multidensional array of different datatypes RRS feed

  • Question

  • User-858993849 posted

    I am wanting to create a multidimensional array that passes different datatypes.  I assume I'll just have to pass them as object. How do I do that?

    Thursday, November 26, 2015 1:12 PM

Answers

  • User603616845 posted

    Hi,

    jayhawker

    a multidimensional array that passes different datatypes

    It is very Simple..Create an array of object class and assign anything to the array.

    As done for primitive types, a multidimensional array of objects can be made a field of a class. You can declare the array without specifying its size. Here is an example:

    public class Triangle
    {
        public Point[,] Vertices;
    }

    If you know the dimensions that the array will have, you can specify them using the new operator at the same time you are creating the field. Here is an example:

    public class Triangle
    {
        public Point[,] Vertices = new Point[3,2];
    }

    This creation signals a multidimensional array of Point objects. It will consist of three lists and each list will contain two Point objects

    To initialize the array, access each member by its index to allocate memory for it. Once you get the member, you access each one of its fields or properties and initialize it with the desired value. Here is an example:

    public class Triangle
    {
        public Point[,] Vertices = new Point[3, 2];
    
        public Triangle()
        {
            Vertices[0, 0] = new Point(); // Point A(x, y)
            Vertices[0, 0].x = -2;        // A(x,  )
            Vertices[0, 0].y = -4;        // A( , y)
            Vertices[1, 0] = new Point(); // Point B(x, y)
            Vertices[1, 0].x =  3;        // B(x,  )
            Vertices[1, 0].y =  5;        // B( , y)
            Vertices[2, 0] = new Point(); // Point C(x, y)
            Vertices[2, 0].x =  6;        // C(x,  )
            Vertices[2, 0].y = -2;        // C( , y)
        }
    }

    If the class is equipped with the right constructor, you can use it to initialize each member of the array.

    Once the array is ready, you can access each members using its index and manipulate it. For example you can display its value(s) to the user. Here is an example:

    using System;
    
    public class Point
    {
        private int XCoord;
        private int YCoord;
    
        public Point()
        {
        }
    
        public Point(int X, int Y)
        {
            XCoord = X;
            YCoord = Y;
        }
    
        public int x
        {
            get { return XCoord; }
            set { XCoord = value; }
        }
    
        public int y
        {
            get { return YCoord; }
            set { YCoord = value; }
        }
    }
    
    public class Triangle
    {
        public Point[,] Vertices = new Point[3, 2];
    
        public Triangle()
        {
            Vertices[0, 0] = new Point(); // Point A(x, y)
            Vertices[0, 0].x = -2;        // A(x,  )
            Vertices[0, 0].y = -4;        // A( , y)
            Vertices[1, 0] = new Point(); // Point B(x, y)
            Vertices[1, 0].x =  3;        // B(x,  )
            Vertices[1, 0].y =  5;        // B( , y)
            Vertices[2, 0] = new Point(); // Point C(x, y)
            Vertices[2, 0].x =  6;        // C(x,  )
            Vertices[2, 0].y = -2;        // C( , y)
        }
    
        public void Identify()
        {
            Console.Write("Triangle Vertices: ");
            Console.WriteLine("A({0}, {1}),  B({2}, {3}), and C({4}, {5})",
                              Vertices[0, 0].x, Vertices[0, 0].y,
                              Vertices[1, 0].x, Vertices[1, 0].y,
                              Vertices[2, 0].x, Vertices[2, 0].y);
        }
    }
    
    public static class Exercise
    {
        static int Main(string[] args)
        {
            Triangle Tri = new Triangle();
            Tri.Identify();
    
            return 0;
        }
    }

    This would produce:

    Triangle Vertices: A(-2, -4),  B(3, 5), and C(6, -2)
    Press any key to continue . . 

    Here is the Multidimensional array Example, how to create, how to add value, how to get value.

    Hope this will help you.

    thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 26, 2015 1:33 PM