Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I could not do three Dimension property in class. Please help me!

What I have tried:

C#
public class Bill
{
    private int CHAR_ARRAY_LENGTH=32;
    private int recordId;
    private string[][][] billname=new string[3][][];
    //
    public Bill()
    {
        int k1,k2,k3;
        for (k1 = 0; k1 < 3; k1++)
        {
            billname[k1] = new string[4][];
            for (k2 = 0; k2 < 4; k2++)
            {
                billname[k1][k2] = new string[5];
                for (k3 = 0; k3 < 5; k3++)
                {
                    billname[k1][k2][k3] = "";
                }
            }
        }
    }
    public Bill(int XrecordId, string[][][] Xbillname)
    {
        RECORDID = XrecordId;
        BILLNAME = Xbillname;
    }
    public int RECORDID
    {
        get { return recordId; }
        set { recordId = value; }
    }
    public string[][][] BILLNAME// <====i wish if
                                // I could correct this property
    {
        get { return billname; }
        set
        {
            int stringSize = value.Length;
            string billnameString = value;

            if (CHAR_ARRAY_LENGTH >= stringSize)
            {
                billnameString = value + new string
                        (' ', CHAR_ARRAY_LENGTH - stringSize);
            }
            else
            {
                billnameString = value.Substring
                                 (0, CHAR_ARRAY_LENGTH);
            }
            //billname = billnameString.ToCharArray();
            billname = billnameString;
        }
    }
}//class Bill

Bill[] bly = new Bill[2];
public Form1()
{
    InitializeComponent();
}

private void FormLoad(object sender, EventArgs e)
{
    int i,j,k1,k2,k3;
    for (i = 0; i < 2; i++)
    {
        bly[i] = new Bill();
        bly[i].RECORDID = i;
        for (k1 = 0; k1 < 3; k1++)
        {
            for (k2 = 0; k2 < 4; k2++)
            {
                for (k3 = 0; k3 < 5; k3++)
                {
                    bly[i].BILLNAME[k1][k2][k3] = "Hello " +
                    k1.ToString() + "," + k2.ToString() + "," +
                    k3.ToString();
                    MessageBox.Show(bly[i].BILLNAME[k1][k2][k3],
                    "bly[" + i.ToString() + "].BILLNAME[" +
                    k1.ToString() + "]" + "[" + k2.ToString() +
                    "]" + "[" + k3.ToString() + "]");
                }//k3
            }//k2
        }//k1
    }//i
}//FormLoad
Posted
Updated 13-Sep-23 4:01am
v5
Comments
CHill60 31-Aug-23 7:19am    
You're going to have to explain your problem more clearly
Dave Kreskowiak 31-Aug-23 8:28am    
Like 'Griff said, us a structure or class to hold the dimensions of the data. Using an array to hold these pieces of information is problematic for people new to coding because you have to make sure you are using the correct indices for each and every access to the array, everywhere in your code. Make one mistake and you're hunting for a difficult to find problem.
Engineer khalid 31-Aug-23 11:49am    
The question now is improved with more detail
program run ok when
public string[][][] BILLNAME
{
get { return billname; }
set { billname = value; }
}
Engineer khalid 31-Aug-23 11:53am    
i have tried and tried ...please help

The way I'd do it is to drop the whole idea of a three dimensional array, and create a class that has three properties: X, Y, and Z - and by preference make then numeric values rather than strings, as that makes using them considerably easier later.

Then create a collection (which can be a single dimensional array, or a List) of that new class. That way, the three values for a single building are kept together.
 
Share this answer
 
Review my solution it might has hidden error but it works fine....
namespace AllocationInClass
{
    public partial class Form1 : Form
    {
        public class Bill
        {
            private const int MAX_CHAR_ARRAY_LENGTH=32;

            private const int SIZE_OF_CHAR = 1;
            private const int SIZE_OF_INT = 4;
            //private const int SIZE_OF_DOUBLE = 8;
            public  const int SIZE_OF_ALL_MEMBERS = SIZE_OF_INT +
                                                   3 * 4 * 5* SIZE_OF_CHAR * MAX_CHAR_ARRAY_LENGTH + 
                                                   124;//padding


            private int recordId;
            private string[][][] billname=new string[3][][];

            //
            public Bill()
            {
                int k1,k2,k3;
                for (k1 = 0; k1 < 3; k1++)
                {
                    billname[k1] = new string[4][];
                    for (k2 = 0; k2 < 4; k2++)
                    {
                        billname[k1][k2] = new string[5];
                        for (k3 = 0; k3 < 5; k3++)
                        {
                            billname[k1][k2][k3] = "";
                        }
                    }
                }
            }
            public Bill(int XrecordId, string[][][] Xbillname)
            {
                RECORDID = XrecordId;
                BILLNAME = Xbillname;
            }
            public int RECORDID
            {
                get { return recordId; }
                set { recordId = value; }
            }
            public string[][][] BILLNAME
            {   
                get { return billname; }
                set
                {
                    int k1, k2, k3;
                    int[][][] stringSize = new int[3][][];
                    string[][][] billnameString = new string[3][][];
                    for (k1 = 0; k1 < 3; k1++)
                    {
                        stringSize[k1] = new int[4][];
                        billnameString[k1] = new string[4][];
                        for (k2 = 0; k2 < 4; k2++)
                        {
                            stringSize[k1][k2] = new int[5];
                            billnameString[k1][k2] = new string[5];
                            for (k3 = 0; k3 < 5; k3++)
                            {
                                stringSize[k1][k2][k3] = value[k1][k2][k3].Length;
                                billnameString[k1][k2][k3] = value[k1][k2][k3];

                                if (MAX_CHAR_ARRAY_LENGTH >= stringSize[k1][k2][k3])
                                {
                                    billnameString[k1][k2][k3] = value[k1][k2][k3] + new string(' ', MAX_CHAR_ARRAY_LENGTH - stringSize[k1][k2][k3]);
                                }
                                else
                                {
                                    billnameString[k1][k2][k3] = value[k1][k2][k3].Substring(0, MAX_CHAR_ARRAY_LENGTH);
                                }
                                //billname = billnameString.ToCharArray();
                                billname[k1][k2][k3] = billnameString[k1][k2][k3];
                            }//k3
                        }//k2
                    }//k1
                }//set
            }
        }//Bill

        Bill[] bly = new Bill[2];
        public Form1()
        {
            InitializeComponent();
        }

        private void FormLoad(object sender, EventArgs e)
        {
            int i,j,k1,k2,k3;
            MessageBox.Show(Bill.SIZE_OF_ALL_MEMBERS.ToString(), "SIZE_OF_ALL_MEMBERS");
            for (i = 0; i < 2; i++)
            {
                bly[i] = new Bill();
                bly[i].RECORDID = i;
                for (k1 = 0; k1 < 3; k1++)
                {
                    for (k2 = 0; k2 < 4; k2++)
                    {
                        for (k3 = 0; k3 < 5; k3++)
                        {
                            bly[i].BILLNAME[k1][k2][k3] = "Hello " + k1.ToString() + "," + k2.ToString() + "," + k3.ToString();
                            MessageBox.Show(bly[i].BILLNAME[k1][k2][k3],
                            "bly[" + i.ToString() + "].BILLNAME[" + k1.ToString() + "]" + "[" + k2.ToString() + "]" + "[" + k3.ToString() + "]");
                        }//k3
                    }//k2
                }//k1
            }//i
        }//FormLoad
    }
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900