Access to a private member variable of a class using its object (instance)

Here is the VB.NET code snippet

Public Class OOPDemo Private _strtString as String Public Function Func(obj as OOPDemo) as boolean obj._strString = "I can set value to private member using a object" End Function End Class 

I thought we could not access private members using this object, but maybe the CLR allows us to do this. This means that access modifiers are based on a type, not an instance of that type. I also heard that C ++ also allows this.

Any guesses what could be causing this?

Edit:

I think this line from the msdn link provided by RoBorg explains this behavior: "Code in a type that declares a private element, including code inside contained types, can access the element"

+1
oop access-specifier
source share
2 answers

Your question is rather confusing, but I think I understood it as: "Why can I access private variables of another instance (my class)?"

And you're right - in all the OOP languages ​​that I used, you can access private variables from other instances, precisely because access rights are based on where the code is located, and not on which instance of the object it belongs to.

Otherwise, it can be difficult to implement copy constructors or equality operators.

+3
source share

Here is a section on access levels in MSDN .

+3
source share

All Articles