X hits on this document

PDF document

“Beginning Visual C# 2005 Express Edition Video Series” - page 46 / 73

174 views

0 shares

0 downloads

0 comments

46 / 73

Methods

We’ve already discussed methods earlier in this lesson, and stated that you must enclose a method in a class, but at that point it probably didn’t mean a lot. Let’s continue our discussion of Methods by considering the method used in our Car class example:

C#:

public string Drive() { if (make=="Oldsmobile") { return "Chicago"; } else {

return "Toledo";

}

}

Methods can have either Private or Public visibility. (Actually there are a few other types of visibility, but let’s just focus on these two for the time being.) Many times, you'll want Private methods to be used as “helper” functions or utilities that hide how the class does its work internally. Ideally, classes are like mysterious "black boxes" to the code that calls them. You know nothing about how the object works except for the Public properties and methods, and that is good. This is known as "Encapsulation" in Object Oriented programming terminology. So, suppose that we were to change the Drive method from a public method to a private one. Then suppose we were to attempt to call the private Drive method from our Form1_Load event … what do you think would happen? We would get an error that says:

‘Car.Drive()’ is inaccessible due to its protection level In other words, because the method is private, it is protected from other classes attempting to call that particular method.

Supplemental Readings for the Express Edition Videos Copyright © 2005 LearnVisualStudio.NET



Document info
Document views174
Page views174
Page last viewedTue Dec 06 19:57:33 UTC 2016
Pages73
Paragraphs821
Words15988

Comments