Click here to Skip to main content
15,881,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello
I have 5 classes Ah, Bh, Ch, Bv and Cv
Ah is parent of Bh.
Bh is parent of Ch
Bv is parent of Bh
Cv is parent of Ch
The program seems working fine but I am not sure if the above is legal

Ah  ->Bh ->Ch
      ^    ^
      |    |
      Bv   Cv


Note
Bh has two parent (Ah and Bv)
Ch has two parent (Bh and Cv)

Need answer

What I have tried:

C#
public class Cv
        {
            public Ch[] sunof_cv;
            private int icv;

            public Cv() { }
            public Cv(int Xicv)
            {
                ICV = Xicv;
            }
            public int ICV { get { return icv; } set { icv = value; } }
            public Ch this[int i] { get { return sunof_cv[i]; } set { sunof_cv[i] = value; } }
        }//Cv
        //
        public class Bv
        {
            public Bh[] sunof_bv;
            private int ibv;

            public Bv() { }
            public Bv(int Xibv)
            {
                IBV = Xibv;
            }
            public int IBV { get { return ibv; } set { ibv = value; } }
            public Bh this[int i] { get { return sunof_bv[i]; } set { sunof_bv[i] = value; } }
        }//Bv
  
        public class Ch
        {
            private int ich;
            public Ch() { }
            public Ch(int Xich)
            {
                ICH = Xich;
            }
            public int ICH { get { return ich; } set { ich = value; } }
        }//Ch
        public class Bh
        {
            public Ch[] sunof_bh;
            private int ibh;

            public Bh() { }
            public Bh(int Xibh)
            {
                IBH = Xibh;
            }
            public int IBH { get { return ibh; } set { ibh = value; } }
            public Ch this[int i] { get { return sunof_bh[i]; } set { sunof_bh[i] = value; } }
        }//Bh

        public class Ah
        {
                public Bh[] sunof_ah;
                private int iah;

                public Ah(){ }

                public Ah(int Xiah)
                {
                IAH = Xiah;
                }
                public int IAH { get { return iah; } set { iah = value; } }
                public Bh this[int i] { get { return sunof_ah[i]; } set { sunof_ah[i] = value; } }
        }//Ah
 //-----------------
private void FormLoad(object sender, EventArgs e)
        {
            int i;
            Ch ch = new Ch();
            ch.ICH = 1;
            //-----------------------
            Ah ah = new Ah();
            ah.IAH = 1;
            ah.sunof_ah = new Bh[3];
            for (i = 0; i < 3; i++)
            {
                ah.sunof_ah[i] = new Bh();
                ah.sunof_ah[i].IBH = i;
            }
            //-----------------------
            Bh bh = new Bh();
            bh.IBH = i;
            bh.sunof_bh = new Ch[3];
            for (i = 0; i < 3; i++)
            {
                bh.sunof_bh[i] = new Ch();
                bh.sunof_bh[i].ICH = i;
            }
            //-------------------------------
            //---------- below vertical -----
            //-------------------------------
            Bv bv = new Bv();
            bv.IBV = i;
            bv.sunof_bv = new Bh[3];
            for (i = 0; i < 3; i++)
            {
                bv.sunof_bv[i] = new Bh();
                bv.sunof_bv[i].IBH = i;
            }
            //------------------------------
            Cv cv = new Cv();
            cv.ICV = i;
            cv.sunof_cv = new Ch[3];
            for (i = 0; i < 3; i++)
            {
                cv.sunof_cv[i] = new Ch();
                cv.sunof_cv[i].ICH = i;
            }
            //------------------------------
        }//FormLoad
Posted
Updated 7-Jan-24 13:26pm
v12
Comments
PIEBALDconsult 7-Jan-24 16:34pm    
Parent as in inheritance? Bh _IS_A_ Ah?
Or only Ah _HAS_A_ Bh?
Maciej Los 7-Jan-24 16:48pm    
What is the definition of those classes?
PIEBALDconsult 7-Jan-24 17:53pm    
Please add it to the question with the Improve question button.
Engineer khalid 7-Jan-24 18:17pm    
ok
Dave Kreskowiak 7-Jan-24 22:00pm    
Of course, it's legal. If it wasn't, the compiler would throw all kinds of errors at you.

The real question is does this arrangement logically make sense for the problem you're trying to solve?

1 solution

Parent and Child relationships are quite common. There are 4 types of relationships:
1. one-to-one
2. one-to-many
3. many-to-one
4. many-to-many

All are legal. Which one you use is dependant on what you are trying to achieve.

One thing to be careful with is to avoid a circular reference. This can cause many problems. There are some cases where a circular reference is required, however, it is something to avoid as a beginner.

Another note is regarding variable and class names. We're no longer living in the 70s, 80s, or 90s, where every byte matters. Use meaningful names for readability, not Ah, Bh, Ch, Bv, Cv... Also, use property & field names that describe what they are. Instead of:
C#
public Bh[] sunof_bv;

use
C#
public Bh[] Children;

The field describes the relationship and that there could be more than one. If there is only one child, then use:
C#
public Bh Child;

Also, fields should be private and be exposed as a Property. So your code should now be:
C#
private Bh[] children;

public Bh[] Children 
{
    get => children;
    set => children = value;
}

or just:
C#
public Bh[] Children { get; set; }
 
Share this answer
 

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