Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Nested Class Access Parent Member [duplicate]

Tags:

c#

oop

Is it possible to access a parent member in a child class...

class MainClass {
  class A { Whatever }

  class B {
    List<A> SubSetList;

    public void AddNewItem(A NewItem) {
       Check MasterListHere ????
    }
  }

  List<A> MasterList;
}

So... my main class will have a master list. It will also have a bunch of instances of B. In each instance of B, I want to add new A's to the particular B, but only if they exist in the Master List. I toyed with making the MasterList static and it works ... until I have more than one instance of MainClass... which I will have.

I could pass a reference to MasterList to each instance of B, but I will eventually have multiple of these "MasterLists" and i don't want to have to pass lots of references if i don't have to.

like image 264
Rob Avatar asked Jul 09 '09 19:07

Rob


People also ask

What is C in coding language?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C programming is an excellent language to learn to program for beginners. Our C tutorials will guide you to learn C programming one step at a time.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

" " C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

In C# there is actually no implicit reference to the instance of the enclosing class, so you need to pass such a reference, and a typical way of doing this is through the nested class' constructor.

like image 87
Mircea Grelus Avatar answered Sep 25 '22 02:09

Mircea Grelus


You can use something like this:


class B {
    private MainClass instance;

    public B(MainClass instance)
    {
        this.instance = instance;
    }

    List SubSetList;

    public void AddNewItem(A NewItem) {
       Check MasterListHere ????
    }
  }
like image 45
Oleks Avatar answered Sep 22 '22 02:09

Oleks