G
Guest
Is there a way to make a property that public can only be set but privately can be read? I've been trying all sorts but can't seem to find an easy solution. I must be making a really simple mistake
Thanks
Marti
Thanks
Marti
You should use pair of get/set methods to achieve this now.
privately can be read? I've been trying all sorts but can't seem to find anMartin said:Is there a way to make a property that public can only be set but
Martin said:Is there a way to make a property that public can only be set but
privately can be read? I've been trying all sorts but can't seem to
find an easy solution. I must be making a really simple mistake!
privately can be read? I've been trying all sorts but can't seem to find anMartin said:Is there a way to make a property that public can only be set but
1) Fire up your Internet browser
2) Navigate to http://www.google.com
3) Enter the word Whidbey in the box and hit the button
codymanix said:Really? Where can I read more about whats planned in Whidbey?
Iam especially interested in the changes from .NET framework 1.1 to 1.2.
Martin said:My preference would have been to use get and set methods but this is
part of an assignment for a lecturer who has asked that we use
properties, seemed a little crazy to me. I have currently found a bit
of a way around it, but I keep getting stack overflow exceptions when
executing this method:
private AddressList InternalDataList;
public AddressList DataList
{
set
{
InternalDataList = value;
DataList = value;
}
}
again within itself (DataList=value).
A way to avoid making that kind of mistake is to use a naming
convention which distinguishes properties from fields.
codymanix said:I _hate_ underscores for private fields.
Why doesn't the C# give a warnung when it encounters an unconditional
recursion like:
public Foo ()
{
get{ return Foo; } // instead of return foo
}
OnFoo()
{
this.Foo(); // instead of base.Foo
}
Iam also wondering why things like a=a (selfassignment) get compiled without
even a warning.
This kinds of errors are the most frequent that are happening to me, since
c# doesn't normally need pointers which where in c++ the most feared
mistakes.
I believe it's quite difficult to do that in a generalised way, to be
honest...
That needn't be a no-op, of course, although it's likely to be a bug,
and a warning would be nice.
codymanix said:Provide me one example where calling the same method/property without
condition will not lead to an stackoverflowexception, this is impossible.
a selfassignment is,if it is not a property always a no-op.
but most times
it happens when you have something like that:
Foo(int tmpVaar)
{
this.tmpVar=tmpVar;
}
Note the typo in the method signature. This way, this.tmpVar=tmpVar is a
selfassignment.
No it's not.
a = a;
could change the value of a, if there are multiple threads running:
Thread 1: Thread 2:
Read value of a
Assign different value to a
Write previously written
value back to a
If thread 1 omits its steps, the result is different.
codymanix said:an unsynchronized read and write of variables between different threads
produces always undefined results.
sure the value of a can change with the assignment but what would be the
gain here? Nobody would seriously be so stupid and do such a thing, so that
is no point.
selfassignment of a variable should always produce a warning, I would even
go so far and say that it should be an compilererror.
impossible.
Without condition, that's true. And I dare say the compiler could pick
up on the simplest cases:
get
{
return PropertyName;
}
set
{
PropertyName = value;
}
But there are any number of cases which are "quite similar" and which
are obviously going to overflow but which the compiler would have a
hard time picking up. I can see the argument that if it's not going to
do a pretty thorough job, it shouldn't try to do the job at all.
cody said:I would say that if the call to itself is the first method call in the
method the compile should issue an error,
since in this case executing the method would *always* produce a stack
overflow.
But if the call to itself is in a condional statement (loop/if), or a return
or throw or method/property invocation
precedes the recursive call I would only issue a warning.
I would prefer no warning at all in that situation - it may well be
exactly the correct code, and reasonable code should never produce
warnings unless there's some easy (and readable) way of telling the
compiler that that really *is* what you mean.
codymanix said:void Foo(int a, int b)
{
a/=b--;
Foo(a, b);
}
Would you consider this as reasonable and correct code that should not
produce a warning?
Note how the recursion is stopped by a DevisionByZeroException.
You can always use a #pragma warn to shut the compiler up in such
situatations. to stupid that c# doesn't yet support this nice feature.