Questions tagged [properties]

A property, in some object-oriented programming languages, is a special sort of class member, intermediate between a field (or data member) and a method. Properties are read and written like fields, but property reads and writes are (usually) translated to get and set method calls.

Property, in some object-oriented programming languages, is a special sort of class member, intermediate between a field (or data member) and a method.

Properties are read and written like fields, but property reads and writes are (usually) translated to get and set method calls. The field-like syntax is said to be easier to read and write than lots of method calls, yet the interposition of method calls allows for data validation, active updating (as of GUI visuals), or read-only 'fields'. That is, properties are intermediate between member code (methods) and member data (instance variables) of the class, and properties provide a higher level of encapsulation than public fields.

See:

18121 questions
184
votes
4 answers

What is the attribute property="og:title" inside meta tag?

I have this extract of website source code: What does this property attribute stand for, and what is its purpose?
luca
  • 36,606
  • 27
  • 86
  • 125
184
votes
5 answers

How to check whether an object has certain method/property?

Using dynamic pattern perhaps? You can call any method/property using the dynamic keyword, right? How to check whether the method exist before calling myDynamicObject.DoStuff(), for example?
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
183
votes
12 answers

Get PHP class property by string

How do I get a property in a PHP based on a string? I'll call it magic. So what is magic? $obj->Name = 'something'; $get = $obj->Name; would be like... magic($obj, 'Name', 'something'); $get = magic($obj, 'Name');
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
180
votes
15 answers

Thread-safe List property

I want an implementation of List as a property which can be used thread-safely without any doubt. Something like this: private List _list; private List MyT { get { // return a copy of _list; } set { _list = value; } } It seems…
Xaqron
  • 29,931
  • 42
  • 140
  • 205
177
votes
16 answers

Properties vs Methods

Quick question: When do you decide to use properties (in C#) and when do you decide to use methods? We are busy having this debate and have found some areas where it is debatable whether we should use a property or a method. One example is…
Sir Rippov the Maple
  • 7,491
  • 5
  • 42
  • 52
176
votes
8 answers

How to loop through all the properties of a class?

I have a class. Public Class Foo Private _Name As String Public Property Name() As String Get Return _Name End Get Set(ByVal value As String) _Name = value End Set End Property …
Sachin Chavan
  • 5,578
  • 5
  • 49
  • 75
174
votes
10 answers

Overriding fields or properties in subclasses

I have an abstract base class and I want to declare a field or a property that will have a different value in each class that inherits from this parent class. I want to define it in the baseclass so I can reference it in a base class method - for…
flytzen
  • 7,348
  • 5
  • 38
  • 54
171
votes
9 answers

Why does 2 == [2] in JavaScript?

I recently discovered that 2 == [2] in JavaScript. As it turns out, this quirk has a couple of interesting consequences: var a = [0, 1, 2, 3]; a[[2]] === a[2]; // this is true Similarly, the following works: var a = { "abc" : 1 }; a[["abc"]] ===…
Xavi
  • 20,111
  • 14
  • 72
  • 63
168
votes
5 answers

C# Iterate through Class properties

I'm currently setting all of the values of my class object Record. This is the code that I'm using to populate the record at the moment, property by property. // Loop through each field in the result set for (int i = 0; i <= resultItems.Length;…
Luke
  • 22,826
  • 31
  • 110
  • 193
160
votes
16 answers

Why is it impossible to override a getter-only property and add a setter?

Why is the following C# code not allowed: public abstract class BaseClass { public abstract int Bar { get;} } public class ConcreteClass : BaseClass { public override int Bar { get { return 0; } set {} } } CS0546…
ripper234
  • 222,824
  • 274
  • 634
  • 905
159
votes
5 answers

How to access property of anonymous type in C#?

I have this: List nodes = new List(); nodes.Add( new { Checked = false, depth = 1, id = "div_" + d.Id }); ... and I'm wondering if I can then grab the "Checked" property of the anonymous object. I'm…
wgpubs
  • 8,131
  • 15
  • 62
  • 109
155
votes
27 answers

Properties file in python (similar to Java Properties)

Given the following format (.properties or .ini): propertyName1=propertyValue1 propertyName2=propertyValue2 ... propertyNameN=propertyValueN For Java there is the Properties class that offers functionality to parse / interact with the above…
Andrei Ciobanu
  • 12,500
  • 24
  • 85
  • 118
152
votes
9 answers

Read properties file outside JAR file

I have a JAR file where all my code is archived for running. I have to access a properties file which need to be changed/edited before each run. I want to keep the properties file in the same directory where the JAR file is. Is there anyway to tell…
Neil
  • 5,919
  • 15
  • 58
  • 85
152
votes
11 answers

How do I reference a JavaScript object property with a hyphen in it?

I am using this script to make a style object of all the inherited, etc. styles. var style = css($(this)); alert (style.width); alert (style.text-align); With the following, the first alert will work fine, but the second one doesn't... it's…
Damon
  • 10,493
  • 16
  • 86
  • 144
152
votes
10 answers

Real world example about how to use property feature in python?

I am interested in how to use @property in Python. I've read the python docs and the example there, in my opinion, is just a toy code: class C(object): def __init__(self): self._x = None @property def x(self): """I'm the…
xiao 啸
  • 6,350
  • 9
  • 40
  • 51