access-modifiers

What is the purpose of access modifiers?

自闭症网瘾萝莉.ら 提交于 2019-11-28 03:53:59
问题 I know this applies to many languages, and not just Java, but that is the language I'm most familiar with. I understand what the modifiers do, and how to use them. I just want to know, why do we need them? Why can't every object be accessible, whether or not it needs to be? 回答1: The reason becomes more apparent when you have to maintain a larger project. When a method or variable is public, you have to be careful when you make changes to it, because you never know which parts of the codebase

Java private field access possible when having a reference?

让人想犯罪 __ 提交于 2019-11-28 03:18:45
问题 I came across the following "strange" feature today - if you have a reference to an object from the class A in the body of the class A you can access the private fields of this object - i.e: public class Foo{ private int bar; private Foo foo; public void f() { if(foo.bar == bar) // foo.bar is visible here?! { // } } } Anyone has a good explanation about this? 回答1: Access modifiers work at the class level, not at the instance level: all code in the same class can access private members of all

internal abstract methods. Why would anyone have them?

喜夏-厌秋 提交于 2019-11-28 02:44:25
问题 I was doing some code review today and came across an old code written by some developer. It goes something like this public abstract class BaseControl { internal abstract void DoSomething(); } If you have a derived class within the same assembly, it would work public class DerivedControl : BaseControl { internal override void DoSomething() { } } But deriving the base class in a different assembly would give compile time error DerivedControl does not implement inherited abstract member

Class is inaccessible due to its protection level

人走茶凉 提交于 2019-11-28 01:49:30
I have three classes. all are part of the same namespace. here are the basics of the three classes. //FBlock.cs namespace StubGenerator.PropGenerator { class FBlock : IDesignRegionInserts, IFormRegionInserts, IAPIRegionInserts, IConfigurationInserts, ISoapProxyClientInserts, ISoapProxyServiceInserts { private List<Property> pProperties; private List<Method> pMethods; public FBlock(string aFBlockName) { pProperties = new List<Property>(); pMethods = new List<Method>(); } public Property AddProperty(string aName) { Property loProp = new Property(this, aName, pProperties.Count); pProperties.Add

Default access modifier in C#

可紊 提交于 2019-11-27 23:38:45
If I will create a new object like the following, which access modifier will it have by default? Object objectA = new Object(); Any member will always have the most restrictive one available - so in this case the accessibility of objectA is private . (Assuming it's an instance variable. It makes no sense as a local variable, as they don't have any access rules as such.) So this: class Foo { Object objectA = new Object(); } is equivalent to this: internal class Foo { private Object objectA = new Object(); } The "default to most private" means that for types, the accessibility depends on the

Deletion of copy-ctor & copy-assignment - public, private or protected?

青春壹個敷衍的年華 提交于 2019-11-27 21:37:45
问题 In order to make an object non-copiable we can explicitly delete both its copy-constructor and copy-assignment operator. My question is: What is the right place to do it - in the public , private or protected section of the class? And - does this choice make any difference? 回答1: what is the right place to do it - in the public, private or protected section of the class? I would put them in the public section . This is because deleting a constructor or an assignment operator is orthogonal to

Internal abstract class: how to hide usage outside assembly?

南笙酒味 提交于 2019-11-27 19:15:15
I have a common assembly/project that has an abstract base class, then several derived classes that I want to make public to other assemblies. I don't want the abstract base class to show up in these other assemblies in Intellisense, so I thought I'd make it internal , but I get this error: Inconsistent accessibility: base class 'Settings' is less accessible than class 'IrcSettings' .... I don't really get this. I am forced to make the abstract Settings class public , and thus visible outside this assembly. How can I make this class internal instead? As I understand, you want your abstract

Any reason to write the “private” keyword in C#?

亡梦爱人 提交于 2019-11-27 18:26:10
As far as I know, private is the default everywhere in C# (meaning that if I don't write public , protected , internal , etc. it will be private by default). (Please correct me if I am wrong.) So, what's the reason to write that keyword, or why does it even exist for members? For example, when an event handler is auto-generated it looks like this: private void RatTrap_MouseEnter(object sender, CheeseEventArgs e) { } But why does it even write private if that's implied and default? Just so that novice developers (who don't know it's the C# default) know that it's private? Or is there a

Why does Typescript use the keyword “export” to make classes and interfaces public?

◇◆丶佛笑我妖孽 提交于 2019-11-27 17:45:49
While dabbling with Typescript I realised my classes within modules (used as namespaces) were not available to other classes unless I wrote the export keyword before them, such as: module some.namespace.here { export class SomeClass{..} } So now I can use the above code like this: var someVar = new some.namespace.here.SomeClass(); However I was just wondering why this keyword is used opposed to just using the public keyword which is used at method level to signify that a method or property should be externally accessible. So why not just use this same mechanism to make classes and interfaces

Visual Studio's Access Modifier drop down option is disabled for resource file [closed]

倖福魔咒の 提交于 2019-11-27 17:32:47
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . Not sure why Access Modifier drop down is disabled for a Resource file. alt text http://img683.imageshack.us/img683/9157/accessmodifier.png Here are file properties: alt text http://img199.imageshack.us/img199/3930/resxprop.png 回答1: If possible, change your Custom Tool back to the default "ResXFileCodeGenerator"