accessors

public variables vs private variables with accessors

Has anyone else seen people do this: private string _name; public string Name{ get{ return _name; } set{ _name = value;}} I understand using accessors if you are going to exercise some sort of control over how it gets set or perform some sort of function on it when there is a get. But if you are just going to do this, why not just mak...

Launch an event that has accessors

Hi, How can I launch an event that has accessors like this : public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } If it were a normal event I would launch it by: CanExecuteChanged(sender, EventArgs..). ...

When should I start a method name that gets a property with the "get-" prefix?

What's a good rule of thumb for naming methods that return properties/attributes/members of an object? If an object has some immutable quality "blarg", should a method that returns that quality be called "blarg()" or "getBlarg()"? The Java API, for example, is inconsistent: most properties are accessed through "get" methods (even those...

WCF service, how to hide internal methods?

Hi, In my WCF service, I have methods that are currently public, but I want to hide them from the outside world but be able to use them in my WCF service. Is internal what I'm looking at? ...

Fallback accessors in C#?

Does C# have anything like Python's __getattr__? I have a class with many properties, and they all share the same accessor code. I would like to be able to drop the individual accessors entirely, just like in Python. Here's what my code looks like now: class Foo { protected bool Get(string name, bool def) { try { return client....

Is it possible to add an accessor to a property in .NET by overriding it?

Is it possible to do something like this? class A { public virtual string prop { get { return "A"; } } } class B: A { private string X; public override string prop { get { return X; } set { X = value; } ...

What is the definition of "accessor method"?

I've been having an argument about the usage of the word "accessor" (the context is Java programming). I tend to think of accessors as implicitly being "property accessors" -- that is, the term implies that it's more or less there to provide direct access to the object's internal state. The other party insists that any method that touche...

using accessors in same class

I have heard that in C++, using an accessor ( get...() ) in a member function of the same class where the accessor was defined is good programming practice? Is it true and should it be done? For example, is this preferred: void display() { cout << getData(); } over something like this: void display() { cout << data; } data...

Why can I not add a set accessor to an overriden property?

In a base class I have this property: public virtual string Text { get { return text; } } I want to override that and return a different text, but I would also like to be able to set the text, so I did this: public override string Text { get { return differentText; } set { differentText = value; } } This however does n...

What is the benefit of explicitly naming getters and setters as "get..." and "set..."?

Does this rankle anyone else out there? I would much rather see: block.key(newKey); // set the key for this block and testKey = block.key(); // look up the key for this block than block.setKey(newKey); // set the key for this block testKey = block.getKey(); // look up the key for this block First, the "set" and "get" are r...

What's the difference between using obj-c accessors and using dot syntax?

Since I've started on iPhone development I've been kinda confused as to which is the best way to access data as a member in a Class. Let's say I have a class called MyClass, and in it I have: @interface MyClass : NSObject { int myInt; } @property (nonatomic, assign) int myInt; In the implementation, is it better to do this: myO...

Valid use of accessors in init and dealloc methods?

I've heard now from several sources (stackoverflow.com, cocoa-dev, the documentation, blogs, etc) that it is "wrong" to use accessors and settings (foo, setFoo:) in your init and dealloc methods. I understand that there is there is a remote possibility of confusing other objects that are observing the property if you do so. (a simple e...

Objective C - Using an accessor if It does nothing different

In objective c, if the using the getter and directly accessing the ivar do exactly the same thing, no lazy loading code in the getter, all it does is returns the ivar, would you still use the accessor or access the ivar directly since there is no difference? Why? EDIT: I'm talking about inside the class. ...

Proper way to modify an NSMutableDictionary instance variable?

Hello! I have an Objective-C NSMutableDictionary declared inside a class's @interface section, with getter/setter methods, like so: @interface myClass : NSObject { NSMutableDictionary *dict; } - (void) setDict: (NSMutableDictionary *) newDict; - (NSMutableDictionary *) dict; Inside some of my @implementation methods I want to mo...

Make an object accessible to only one other object in the same assembly?

Each business object has a matching object that contains sql calls. I'd like to restrict these sql objects in a way where they can only be used by the matching business object. How can this be achieved? Update Greg brought up the point about testability. Since the SqlObjects will contain very business-process specific sql I don't want ...

Why does attr_accessor clobber the existing variables in this model in Ruby on Rails?

I was bitten by this recently, and it'd be useful to know precisely what's happening to make this happen, so others avoid this mistake. I have a model User, with a schema like so: create_table "users", :force => true do |t| t.string "user_name" t.string "first_name" t.string "last_name" t.string "email" t.st...

Unit test with accessors

This is a two part question. Background: We moved our C# application from VS2005 to VS2008 and in the process moved the application from .net 2.0 to .net 3.5. The transition went smoothly except for Unit Tests. First: Is the unit test framework based off Visual Studios or .NET? Second: This question is derived from the issues we have ...

get and set issues

Here is the code that demonstrates my problem (All in the same namespace): public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Fubar.BGroup.A = true; } public Foo Fubar = new Foo(); } public class Foo { public Foo() { } private BoolGroup bGroup = new BoolGro...

ruby equivalent of c#'s internal keyword?

in C# an "internal" class or method can only be called from within the assembly that it is located... i know Ruby doesn't have "assemblies" or any kind of real package like them (other than gems, but they aren't really the same thing at all), but I'm wondering if there is a way for me to limit the location that a method can be called fro...

What are the advantages of use accessors to instances of IB elements?

Usually, when we have to link an interface element to a field of a class, we use the keyword 'IBOutlet' to inform the pre-copiler: @interface MyController : NSObject { IBOutlet NSWindow *theWindow; } and in the implementation we use directly the pointer theWindow to call methods of the class NSWindow! But what are the advantages ...