The interface is defined as a set of methods (Sub and Function methods), properties (property methods), events, and fields (also known as variables) that are declared public in scope.

You can also have private methods and properties in your code. While these methods can be called by code within your object, they are not part of the interface and cannot be called by programs written to use your object. Another option is to use the Friend keyword, which defines the scope to be your current project, meaning that any code within your project can call the method, but no code outside your project (that is, from a different .NET assembly) can call the method. To complicate things a bit, you can also declare methods and properties as Protected, and these are available to classes that inherit from your class. You will look at Protected in Chapter 3, along with inheritance.

For example, you might have the following code in a class:

Public Function CalculateValue() As Integer

End Function

Because this method is declared with the Public keyword, it is part of the interface and can be called by client applications that are using the object. You might also have a method such as this:

Private Sub DoSomething() End Sub

This method is declared as being Private, so it is not part of the interface. This method can only be called by code within the class — not by any code outside the class, such as code in a program that's using one of the objects.

Conversely, you can do something like this:

Public Sub CalculateValue()

DoSomething() End Sub

In this case, you're calling the Private method from within a Public method. While code using your objects can't directly call a Private method, you will frequently use Private methods to help structure the code in a class to make it more maintainable and easier to read.

Finally, you can use the Friend keyword:

Friend Sub DoSomething()

End Sub

In this case, the DoSomething method can be called by code within the class, or from other classes or modules within the current Visual Basic project. Code from outside the project will not have access to the method.

The Friend scope is very similar to the Public scope in that it makes methods available for use by code outside the object itself. Unlike Public, however, the Friend keyword restricts access to code within the current Visual Basic project, preventing code in other .NET assemblies from calling the method. One of the more common uses of Protected is with the Friend modifier as will be discussed in Chapter 3.

Was this article helpful?

0 0

Post a comment