Jon Slaughter said:
You know, before you write such a long winded post you should try and
understand what I said.
Believe me, I did. You're not making it easy. Excuse me for trying to
help.
Its not all your fault because I do expect anyone that replies to put a
little effort into figuring out what I asked.
The best question does not require "anyone that replies to put a little
effort into figuring out what you asked". Garbage in, garbage out. If you
don't ask a clear question, you won't get a clear answer.
Since you seem to be confused I'll break it down.
Yes, I am confused. However, you appear to be as well. Welcome to the
club.
Whats the difference between
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
This one compiles.
and
public string Name
{
get { return Name; }
set { Name = value; }
}
This one does not.
with a preprocesser that converts the second into the first?
There is no preprocessor that converts the second into the first.
There is a direct correspondece. I could easily write(and probably will) a
preprocesser that will search for "properties" and add a field.
Great. What will you call that field? At the very least, you cannot use
the same name that you've given your property (as you have done above). Why
do you think it is better to implicitly declare a field, rather than doing
it explicitly? How closely will the preprocessed code have to be to the
example you've provided in order to result in an implicitly defined field?
Can I write "get { return number + 1; }" and "set { number = value - 1; }"
and have your processor correctly generate the field? How many passes do
you expect your preprocessor to execute before it is sure that the field
being used in a property definition hasn't been already declared? Will your
preprocessor handle partial class declarations correctly?
And what benefit do you see in going to the trouble to write a preprocessor
just so you can save writing a line of code for the simplest case of
defining a property?
It will work fine and there should be no issues(if there are then you need
to state some syntatic or semanitic reason why it will not work).
I don't see any reason at all that you can't accomplish what you want to, as
long as you do it correctly (there are lots of ways to do it incorrectly).
I just don't see the value in it. There are so many other uses for
properties, writing a preprocessor just so you can get out writing a single
line of code once in awhile seems like a big waste of time.
This is very similar to when I wrote a preprocessor to add properties to
C++. it is actually very similer.
I don't doubt that.
The fact of the matter is that by adding having to add a field declaration
for a property to work is redundant(and if not then explain why).
You don't have to add a field declaration for a property to work. You only
need to do so if you want the property to work by accessing a field.
Furthermore, if you are so concerned with minimizing your typing, why
declare a property in the way you're showing us in the first place? Why not
just make a public field?
The only case that I can think of is when you want the field used by the
property to be protected... but then one could change the grammar slightly
to allow for that. Its not impossible to do and is quite easy.
Do you mean that you want to specify the modifier for the implicit field, as
"protected" instead of "private" (which would presumably be the default for
your preprocessor)? I agree, it's not impossible to do. I also don't see
how that's an issue at all. There are so many other pitfalls involved, this
seems like the least of your concerns.
The point is to loose the redundancy that exists....
There is no redundancy. A field is not the same thing as a property. One
declares a property for the purpose of declaring a property. One may or may
not declare a field to support that property. It all depends on the
implementation of the property.
why the hell did C# remove the necessary requirement in C++ for
prototypes? Because its redundant can be redudancy is error prone or
usless.
I'm not sure what "requirement in C++ for prototypes" you're talking about.
It's true that the Visual Studio C# environment does away with having to
explicitly include header files. But that's because it has a whole
different way of referencing code objects. Prototypes still exist...they
are just encapsulated differently.
About your arguments that the property doesn't have to work with a field
with the same name is quite useless because in almost all cases they are
the same except for case or very similar.
I never made such an argument. I have no idea why you think I did. The
name of a supporting field for a property is entirely irrelevant. You can
name the supporting field whatever you like.
I never said they were the same thing. The point is that a property
represents a field.
No. No. And did I mention? No.
IMHO, this is the crux of your misunderstanding. A property most
emphatically does NOT represent a field. It may well wrap a field, but as I
said before, this is the simplest, most trivial example of a property. It
is the least interesting, and a property is MUCH much more than that.
There need not be ANY field associated directly with a property, and even
when there is a field associated with a property, the property may (and
often will) do much more than just returning or assigning the value of that
field.
[...]
again, the point is that when I declare a property the compiler can
unambigulous determine that a field is being used(because thats the whole
point of properties)...
That is not "the whole point of properties".
since it can then it can handle the redundancy implicitly. There is no
reason that you have given why this cannot be done.
I have not tried to say it cannot be done. I don't disagree that what you
want can be done. I just don't think doing it is useful, and I believe that
your desire to do it (and your belief that the authors of C# should have
done it) demonstrates a incorrect understanding of what a property is.
If, say, its not grammatically possible because, say, C# uses a context
free grammar and it would require a context sensitive grammar then that is
a reason(ofcourse thats not the case)... but saying things like a field is
not a property is completely off the point and has nothing to do with the
argument.
It has everything to do with the "argument", such as it is. Personally, I
didn't realize we were having an argument. I was trying to help you
understanding the true difference between a field and a property, and I
believe the other respondents were too. Perhaps if you stopped viewing this
as an "argument", you could relax enough to take in the information being
offered.
A property might not be a field and a field might not be a property but
where theres a property theres a field
For the third time, this is NOT true. It is false to claim that "where
there's a property there's a field".
and hence theres no reason to declare a field when theres a property for
it.
The reason for declaring a field when there's a property for it is to tell
the compiler where and what the field being used in the property definition
is, if indeed a field IS being used in the property definition.
The point is that the conversion I have given is programmatically
identical and reduces redundancy. There might be other reasons why its not
good to do it that way but you have not given any.
I wasn't trying to. I was trying to explain to you the difference between a
field and a property, since that appeared to be the question you asked.
If you're looking for an answer to why you shouldn't write the preprocessor
you suggest, the only answer I have to that is that I think it'd be a waste
of time. But please, go right ahead and do it if you think it will help you
write your code.
That still doesn't mean that you understand the difference between a
property and a field.
Pete