It is important that functions/methods share data between a class because in programming you will be having a lot of methods that will need to share data to function correctly. For example, if you had to calculate different types of data with equations, you would have the calculations being done in one method then transfer only the calculated data to a display method. If data isn't shared between these two classes then only the calculation method will have the data, and no other methods can access it, making it difficult to use that data to perform more tasks.
The scope refers to the portion of the program a variable is visible, as well as where it can only be used. It is referred to where a variable can be read from and/or written to, in other words where the variable lives. Visibility compared to scope is where the variables can be used and shared. We can set where a variable can be accessed with three different options, public, private and protected. This is important because if set a variables access to public instead of private in a class then all of the methods inside that class will have the liberty to use that variable. This can create issues with bigger programs as the data can be changed and overwritten since the access is easy to use for multiple methods. If the access is private then only a specific method can use that variable(s), which is better when you want to protect that data from being used in other methods.
sources:
https://msdn.microsoft.com/en-us/library/ms973875.aspx
https://msdn.microsoft.com/en-us/library/awt60xs1.aspx
Comments
Post a Comment