|
Megidolaon wrote: and if I ever got into such a situation, I'd re-think my design.
Absolutely, if you can't read it, it's not a property.
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP blog: TDD - the Aha! | Linkify!| FoldWithUs! | sighist
|
|
|
|
|
The only good example of a write-only property I've come across is with authentication. A class representing a user might have a username and a password. The password is required for authentication, but should not be generally accessible to the rest of an application. If you have a code-base which supports third-party plug-in authoring then you may not be able to guarantee all plug-in code is safe / responsible... In this case a write-only property for the class is a good plan if the user object is then shared.
Otherwise I would tend to agree with the use of a setter method... Might be a background thing, though - I know a lot of the VB folks like properties for class field setters/getters.
AJ
|
|
|
|
|
I have several classes I use for instrumentation control and port access. I use the write only property as a means of passing instructions to the remote devices (reads easier in my opinion) and this suggests that the property value is not stored just passed along.
Ex:
remoteDevice.IO.Send = "SETVOLT 25";
If I wanted to set some kind of parameter value that is stored in the class but should not be read I would use the following method:
serializer.SetInitialValue(103);
|
|
|
|
|
Well I consider those to be backward, but it's really a matter of style.
|
|
|
|
|
Don't you know about the black hole design pattern?? Looks like this..
public class BlackHole<t>
{
private T _hole;
public T Hole {
set {
try {
this._hole = value;
}
catch {
throw new BlackHoleException();
}
}
}
}
</t>
|
|
|
|
|
Marcello Faga wrote:
Don't you really want to see what you have put?
Not really, it could be a writeonly type of scenario.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
I often use read only Properties, but write only properties, while useful in a few rare instances, are usually not what you want to do.
|
|
|
|
|
<br />
private Pair<RefPoint3d> m_BoundingBox;<br />
<br />
public Pair<RefPoint3d> BoundingBox<br />
<br />
{<br />
<br />
get { return BoundingBox; }<br />
<br />
set { m_BoundingBox = value; }<br />
<br />
}
Did you never define a property in this way?
The run-time consequence is amazing: no exceptions at all, and sudden termination of the debug session when you are hovering the call at the property in your client module.
PS: the error is inside the get property, because of the not voluntary recursive cycle it generates.
PPS: I have no idea how this issue can be coded, the automatic refactoring of VS2005 should make impossible to code malware property.
****************************
Strong congruence
for strong people;
with a compatible behaviour.
For a semantical way of life.
modified on Tuesday, June 10, 2008 10:22 AM
|
|
|
|
|
When I was first learning, I did stuff like this:
public int Value
{
get{return Value;}
set{Value=value;}
}
I don't think that I understood that properties were actually methods back then.
|
|
|
|
|
Marcello Faga wrote: Someone knows how fast this code can kill your application?
It depends on the stack size of your application...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
I think everyone here knows that.
|
|
|
|
|
Of course PIEBALD, it was a rethorical question .
The real question is how someone can have coded that one?
****************************
Strong congruence
for strong people;
with a compatible behaviour.
For a semantical way of life.
modified on Wednesday, June 11, 2008 4:54 AM
|
|
|
|
|
It's actually very easy, because recursive properties are perfectly legal. And Refactoring doesn't do anything about it, because you have to invoke it manually for a specific Refactoring case. While this is definitely a subtile bug, it is a pretty common one if you are following the basic .NET Framework Naming convention to use PascalCasing for the public property and camelCasing for the private field. Intellisense can even help you to make it wrong, depending on your exact settings and what you typed before. If you put an underscore in front of the field name, it can't happen (or is at least easy to spot).
|
|
|
|
|
That's one reason I hate camel casing. Pascal casing or Hungarian notation is my thing.
|
|
|
|
|
That problem has been presented here or in Subtle Bugs multiple times.
|
|
|
|
|
Using automatic properties you won't run in such problem.
Giorgi Dalakishvili
#region signature
my articles
#endregion
|
|
|
|
|
Oops...
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
The following code I found some developer used very often. I just wonder might he loves to run getStatus twice to make sure his code executed perfect.
if(GetStatus(id) != "")
string status = GetStatus(id);
private string GetStatus(int recordId)
{
string s = "";
//First, use ADO.NET to get the Status of this recordId
//Then, assign the status to a string.
if(//Check to make sure there is data and not DB null)
s = //The data;
//Finally, return the string
return s;
}
|
|
|
|
|
Possibly he went from the VB realm.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
It is an ambitious way to save a memory variable that people succumb to a bad performance of an application by calling a lengthier function twice or more.
Vasudevan Deepak Kumar
Personal Homepage Tech Gossips
All the world's a stage,
And all the men and women merely players.
They have their exits and their entrances;
And one man in his time plays many parts... --William Shakespeare
|
|
|
|
|
If it's worth getting, it's worth getting twice.
|
|
|
|
|
That is a race just waiting to happen!!! In C# I try not to use an 'is' followed by a cast for the same reason. The following code reads so much nicer:
Bar bar = o as Bar;
if(bar != null)
{
}
than this:
if(o is Bar && !object.ReferenceEquals(o, null))
{
}
|
|
|
|
|
I wrote this at 4AM the other morning :S I'm a little embarrassed. But it's still doing it's bit in my program.
for(int y = 0; y < bmp.biHeight; y=y+3){
for(int x = 0; x < bmp.biWidth; x=x+3){
for(int i = 0; i < 9; i++){
int yp=0, xp=0;
get_pixel(x+xp,y+yp,blank[i]);
if(xp<3)
xp++;
else{
xp = 0;
yp++;
}}
avg(blank, Merge, 9);
set_pixel(x,y,Merge);
for(int i = 0; i < 9; i++){
blank[i].rgbtBlue=0;
blank[i].rgbtGreen=0;
blank[i].rgbtRed=0;
}
clear_pixel(Merge);
yp=0, xp=0;
}}
"Sir, I protest. I am NOT a merry man!"
|
|
|
|
|
|
Did I miss something? isn't:
yp=0, xp=0;
out of scope?
|
|
|
|