access-modifiers

C#'s “protected internal” means “protected” *OR* “internal”. Does any keyword mean “protected” *AND* “internal”? [duplicate]

℡╲_俬逩灬. 提交于 2019-12-24 01:24:09
问题 This question already has answers here : How to make a property protected AND internal in C#? (7 answers) Closed 6 years ago . I need to declare a member that is both protected AND internal. However, to my complete bafflement, I just discovered the hard way that "protected internal" actually means protected OR internal. Is there any access modifier that means protected AND internal? 回答1: Though the CLR supports it, in C# there is no way to force a member to be protected AND internal. Both C#

Reflection: Why are there methods like setAccessible()?

喜你入骨 提交于 2019-12-23 12:39:01
问题 Just wondering, why did the people who invented Java write methods like setAccessible(boolean flag) , which makes the access-modifiers (specially private) useless and cannot protect fields, methods, and constructors from being reached? Look at the following simple example: public class BankAccount { private double balance = 100.0; public boolean withdrawCash(double cash) { if(cash <= balance) { balance -= cash; System.out.println("You have withdrawn " + cash + " dollars! The new balance is: "

C# “is inaccessible due to its protection level” error in constructor

Deadly 提交于 2019-12-23 09:17:38
问题 The constructor of the child class "caesar" gives an error. It says that name, type is inaccessible due to its protection level. How come? As this is a child class derived from "Cipher" class it shouldn't give an error like this. How can I overcome this situation. But I want those variables to be private. I don't want to change them as public. ***The second code example works. Can anybody see a difference? namespace Encrypter { class Cipher { public Cipher(string name, string type) { setName

Can I define “method-private” fields in Scala?

大憨熊 提交于 2019-12-22 11:10:15
问题 Given this situation: object ResourceManager { private var inited = false def init(config: Config) { if (inited) throw new IllegalStateException // do initialization inited = true } } Is there any way that I could make inited somehow “private to init()”, such that I can be sure that no other method in this class will ever be able to set inited = false ? 回答1: Taken from In Scala, how would you declare static data inside a function?. Don’t use a method but a function object: val init = { // or

Where is the Friend access modifier intended to be used?

回眸只為那壹抹淺笑 提交于 2019-12-22 10:19:25
问题 The only place I've seen the Friend modifier used is in the WinForms designer, as alluded to in Why is the modifier set to Friend in Winforms? and VB.NET: what does the 'friend' modifier do?. The Friend modifier appears to be an almost arbitrarily wide access level that was created to solve some historic architectural problem in VB, I just wonder if anyone has a meaningful continued use for it? I have had some desires to expose methods only to a given namespace so as to roll the

“protected” methods in C#?

自闭症网瘾萝莉.ら 提交于 2019-12-22 01:42:01
问题 What are the benefits to defining methods as protected in C#? like : protected void KeyDemo_KeyPress( object sender, KeyPressEventArgs e ) { // some code } As compared to something like this: private void FormName_Click( object sender, EventArgs e ) { //some code } I've seen such examples in many books and I don't understand why and when do they use private vs protected ? 回答1: Protected methods can be called from derived classes. Private methods can't. That's the one and only difference

Signature Files and Access Modifers in F#

╄→гoц情女王★ 提交于 2019-12-21 09:23:14
问题 I've recently been trying to learn the Object-Oriented aspects of F#, and have become curious about how to restrict access to types/modules in the language. More specifically, I want to know the difference between writing this: Example.fsi module Stack = val foo : string Example.fs module Stack = let foo = "foo" let bar = "bar" and alternatively this: module Stack = let foo = "foo" let private bar = "bar" Do they not accomplish exactly the same thing in the end? Coming from a C# background, I

Why use public variables?

混江龙づ霸主 提交于 2019-12-21 05:16:13
问题 Variables, methods and classes can receive various security levels. From my C# experience, there is: public internal protected protected internal private Now, I understand the use of making methods and classes private, or internal or protected, but what about variables? Even if I make a variable private, I can use a Property to call it from a different class. I've always been thought that Properties are best-practice. So if I can use that, I don't need to call variables directly via an

Call a protected method from outside a class in PHP

旧时模样 提交于 2019-12-21 04:50:40
问题 I have a very special case in which I need to call a protected method from outside a class. I am very conscious about what I do programmingwise, but I would not be entirely opposed to doing so in this one special case I have. In all other cases, I need to continue disallowing access to the internal method, and so I would like to keep the method protected. What are some elegant ways to access a protected method outside of a class? So far, I've found this. I suppose it may be possible create

Scope of protected members

允我心安 提交于 2019-12-20 03:35:11
问题 Iam preparing for SCJP , also i came to know that protected members scope is within the package as well as in other package with some conditions like possible only with inheritance. For example : i have three classes as Parentclass Childclass Friendclass package x.parent; class Parentclass{ protected int x=10; ............... } package x.child; class Childlass extends Parentclass{ super.x=20; ............... } package x.child; import x.parent.Parentclass; class Friendclass{ Parentclass pc =