Vote for this idea if you like it here:
http://lab.msdn.microsoft.com/produc...5-254fbbe96ee9
-----------------------------------------------------------------------------
This is a consortium of ideas from another thread on topic
-----------------------------------------------------------------------------
One of the big issues of organizing items within a class, is there are many
times subsets of functionality where only certain methods should have access
to certain variables, and there is currently no declarative way to state or
maintain this in the language.
Here is just one example, but imagine other variations of member
variable/method associations:
So for example if you had a complex class that internally maintained an
array for its own private use, and you added methods Add, Remove and Get and
wanted to make sure those were the only methods that had access to that
variable because it needed to be locked for multithreading purposes, there is
no way to enforce or ensure this in the language and results in numerous
accidental, or even purposeful uses of the underlying variables
inappropriately counter to the original design.
Note this suggestion along these lines has received positive feedback in the
newsgroups. This is a variation on another suggestion that I think makes it
more broadly applicable and appropriate. I can assure you this type of thing
has come up constantly in development, not having a declaritive way to
associate variables with the methods that should use them.
Proposed Solution:
What if I we could write:
class MyClass
{
scope {
bool sentData_=false;
public void SendData()
{
if (!sentData_)
{
//DoSend()
sentData_=true;
}
}
}
Where scope can organize a set of methods with access to particular variables.
Obviously there are isssues and concerns about overlap, an what if a method
needed access to only one of the variables in that scope but not all and
something from another scope. So not sure how viable this is but something
to think about if there is some way to improve... ideas?
}
Compiler errors WXS123, 2006-05-25
Any uses of the variables outside of the scoped variables/methods should
result in compiler errors.
--------------------------------------------------------------------------------
Very common scenario WXS123, 2006-05-25
In any reasonably sized class it is probably very rare that every method
needs access to, or even should have access to every variable in the class.
Given that, it makes sense to scope the variables to a set of methods that
are allowed access. This organizes both mentally for the developer and
deterministically for compilation. It is common to have internal subsets of
functionality within a class that are used as private helpers to other
methods. Currently all variables and all methods are fare game. It may even
make sense to find some way to scope methods in such a way that they should
only be used by a small subset of other methods. Otherwise with current
functionality once you are inside a class, it is the wild west, anybody can
party on anything. One could try to argue to break out subsets into other
classes. But many times the set of functionality is so limited, it would be
like a micro class... making it available as a class would require possibly
more functionality and interface thought to make it right, thus most
developers rarely would do that and instead just create the few methods that
work with some member fields in the class and be done with it. And with
several of those subsets it is not only hard to discern what variables work
with which methods, but can be functionally disasterous if a variable that is
supposed to be protected by locking before accessing is used directly in
another method. When you have a team of developers adding functionality to
classes this becomes a major issue in organization and use.
--------------------------------------------------------------------------------
Example of compiler error WXS123, 2006-05-26
class MyClass { scope { bool sentData_=false; public void SendData() { if
(!sentData_) { //DoSend() sentData_=true; } } public void
TryToUseVariableInappropriately() { sendData_=true; //Compiler error, this
method not scoped for this variable } }