Share variable between multiple classes

前端 未结 5 1528
囚心锁ツ
囚心锁ツ 2021-01-06 13:11

If i have 3 classes, lets say: Mainclass, ChildClass, OtherChild.

MainClass()
{
     ChildClass cc = new ChildClass();
     OtherChild oc = new OtherChild();         


        
5条回答
  •  臣服心动
    2021-01-06 13:56

    If I correctly understand your question, this can work. Note, my syntax may be wrong as I'm not super fluent at c# but I hope you can get the basic idea

    MainClass()
    {
         ChildClass _cc = new ChildClass();
         OtherChild _oc = new OtherChild();
         ChildClass cc = get {return _cc;} set{_cc = value;}
         OtherChild oc = get {return _oc;} set{_oc = value;}
         oc.Parent = this;
         //Set the name property of childclass
         string childName = "some name";
    }
    
    ChildClass()
    {
        public string name {get; set;}
    }
    
    OtherChild()
    {
         //Here i want to get the name property from ChildClass()
         //Doing this will make a new instance of ChildClass,  which will not have the name property set.
         Public MainClass parent {get; set;}
         ChildClass cc = parent.cc; 
    
    }
    

提交回复
热议问题