I use 'property' to ensure that changes to an objects instance variables are wrapped by methods where I need to.
What about when an instance has an variable that logically should not be changed? Eg, if I'm making a class for a Process, each Process instance should have a pid attribute that will frequently be accessed but should not be ...
What's the advantage of using getters and setters - that only get and set - instead of simply using public fields for those variables?
If getters and setters are ever doing more than just the simple get/set, I can figure this one out very quickly, but I'm not 100% clear on how:
public String foo;
is any worse than:
private String fo...
I have a Main Document class that instantiates 2 classes that control loading of two navigation grid swfs. These are populated by xml from navigation buttons on the main stage. A 3x3 navigation grid repopulates it's images and links when the buttons on the main stage are clicked Then loads the chosen video when a node on the grid is clic...
Okay, I have something like this in C++:
class MyClass{
private:
int someVariable;
int someOtherVariable;
struct structName{
int someStructVariable;
int someOtherStructVariable;
};//end of struct
public:
//getters & setters for the defined variables.
int getSomeStructVariable()
{
// this does not work I get ...
Is there something I need to do differently in setting a string in a setter method? This is my class:
class SavingsAccount
{
public:
void setData();
void printAccountData();
double accountClosure() {return (accountClosurePenaltyPercent * accountBalance);}
private:
int accountType;
string ownerName;
long ssn;
...
It really annoys me already now... I can not find the setting to have by default insertion point at last member.
Why? Generating getters and setters would then be:
ALT+SHIFT+S -> space, space, space... -> ENTER :)
And not like now:
ALT+SHIFT+S -> space, space, space... -> tab, tab, tab, tab, tab, -> up, up, up... -> ENTER
...
I'm working on a Chart class and it has a margin parameter, that holds :top, :bottom, :right and :left values. My first option was to make margin a setter and set values like this:
# Sets :left and :right margins and doesn't alter :top and :bottom
chart.margins = {:left => 10, :right => 15}
It's nice, because it is clearly a setter, ...
I have the following chunk of python code:
import hashlib
class User:
def _set_password(self, value):
self._password = hashlib.sha1(value).hexdigest()
def _get_password(self):
return self._password
password = property(
fset = _set_password,
fget = _get_password)
def __init__(self, user...
I've got a simple application with two entities:
Person:
Attributes:
name
Relationships:
nativeLanguage: (<<-> Language.natives)
nonNativeLanguage: (<<-> Language.nonNatives)
Language:
Attributes:
name
Relationships:
natives: (<->> Person.nativeLanguage)
nonNatives: (<->> Person.nonNativeLanguage)
On the ed...
Is there a benifit to using:
private var _someProp:String;
public function set someProp(value:String):void
{
_someProp = value;
}
public function get someProp():String
{
return _someProp;
}
As opposed to just using:
public var someProp:String;
I realise using getter/setter can be useful when you need to further processing ...
How Can I know if the DataGridCell is currently in edit mode (not IsSelected), I mean, for example a DataGridTextColumn cell is clicked it becomes a TextBox and not a TextBlock, that's what I call IsEditMode.
I wanna set a trigger-setter for this mode.
EDIT:
I tried to set a general style for DataGridCell.IsEditing but it doesn't seem t...
I have a class for products import from CSV file operation which requires about 7 parameters. This is an info which is definitely needed for importer.
All of this parameters have the same life time. In the end we must have an Immutable Object.
I was too scared to list all of them in constructor because of its affect to readability and ...
Whilst playing around with an nhibernate mapping, I noticed that a property setter I had was being overloaded (or ignored). This is expected default behaviour with an nhibernate mapping.
So I changed it to use the field.camelCase - so NHibernate would set the private field of the entity class and not the propety getter/setter so I could...
im from the php world. are there good tutorials explaining what getters and setters are and could give you some examples?
...
Apart from unambiguous clarity, why should we stick to:
car.getSpeed() and car.setSpeed(55)
when this could be used as well :
car.speed() and car.speed(55)
I know that get() and set() are useful to keep any changes to the data member manageable by keeping everything in one place.
Also, obviously, I understand that car.speed() and car.s...
In the following code:
_imageView.hasHorizontalScroller = YES;
_imageView.hasVerticalScroller = YES;
_imageView.autohidesScrollers = YES;
NSLog(@"scrollbar? H %p V %p hide %p",
&(_imageView.hasHorizontalScroller),
&(_imageView.hasVerticalScroller),
&(_imageView.autohidesScrollers));
I'm getting the error:
Contro...
Consider the following class definitions
class of2010(object):
def __init__(self):
self._a = 1
self._b = 2
self._c = 3
def set_a(self,value):
print('setting a...')
self._a = value
def set_b(self,value):
print('setting b...')
self._b = value
def set_c(self,value):
...
I am having a really odd problem trying to set a simple float value to 1.
My property:
{
float direction;
}
@property(nonatomic)float direction;
Which is synthesized:
@synthesize direction;
I then used the following code:
- (void)setDirection:(float)_direction {
NSLog(@"Setter called with value of %f",_direction);
self->d...
I have following two domain classes in Grails 1.1.2:
class A implements Serializable {
MyEnumType myField
Date fieldChanged
void setMyField(MyEnumType val) {
if (myField != null && myField != val) {
myField = val
fieldChanged = new Date()
}
}
}
class B extends A {
List children
void setMyField(MyEnumType val) {
if (m...
I have a list of Parts and some of them need a pointer to an Engine, lets call them EngineParts. What I want is to find these EngineParts using RTTI and then give them the Engine.
The problem is how to design the EnginePart. I have two options here, described below, and I don't know which one to choose.
Option 1 is faster because it doe...