且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以访问内部类中的外部类对象

更新时间:2023-02-15 16:24:09

如果我能够提出一个可以由A类处理的事件,那么我的问题可以解决。 p>如果我正确地读你的话,你想访问innerC中的objB属性,而不是传递它。



这不是C# ,如本文所述: C#嵌套类是C ++嵌套类,而不是Java内部类



如果你想从innerC访问A.objB,那么你必须以某种方式将A类传递给innerC AND 确保以某种方式公开objB属性。 (如果你有objB设置为私有,这将不工作。)


I have three classes like this.

class A
{
    public class innerB
       {
       //Do something
       }

    public class innerC
       {
        //trying to access objB here directly or indirectly over here. 
        //I dont have to create an object of innerB, but to access the object created by A
        //i.e.
             innerB objInnerB = objB;
        //not like this
             innerB objInnerB= new innerB();
       }

private innerB objB{get;set;}  **//Private**

public A()
   {
    objB= new innerB();
   }
}

I want to access the object of class B in Class C that is created by class A. Is it possible somehow to make changes on object of Class A in Class C. Can i get Class A's object by creating event or anyhow.

Edit: My mistake in asking the question above. Object of B created in A is private not public

IS IT POSSIBLE TO DO THIS BY CREATING EVENT

If anyhow I become able to raise an event that can be handled by Class A, then my problem can be solved.

If I'm reading you correctly you want to access the objB property of class A within innerC WITHOUT passing it along.

This isn't how C# inner classes work, as described in this article: C# nested classes are like C++ nested classes, not Java inner classes

If you want to access A.objB from innerC then you are going to have to somehow pass class A to innerC AND make sure that you expose the objB property somehow. (i.e. this won't work if you have objB set to private.)