'_____________________________________________________________________________________________ ' USING CLASSES PART 4: ' SHARED MEMBERS 'This program allows us to add two numbers. It uses a class called DoMath which contains one 'function/method called Add. 'This function is declared as a Shared Member of the class so that we can access it from anywhere 'inside our application. The main purpose in showing you this creepy program is that in the btnGO_click event, 'you will notice that I do not have to first create a new instance of DoMath using the New keyword. I can 'simply use the Add method. 'This is like placing a function inside the form itself or by putting it in a general code module. 'But this is a way of showing you yet another way to interact with a class you create. 'By putting functions inside their own module/class, it organizes your stuff and makes it easy to add this 'class to a new project. '_____________________________________________________________________________________________ Option Explicit On Public Class frmMain Private Sub btnGO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGO.Click '---We can use this method - without instantiating a DoMath object because we created the method (function) ' as being shared in the DoMath Class Dim First As Integer Dim Second As Integer Dim Result As Integer First = txtFirst.Text Second = txtSecond.Text Result = DoMath.Add(First, Second) 'just use the add method of DoMath txtAnswer.Text = Result End Sub '-------------------------------------------------------------------------------------------- ' DO MATH CLASS 'This little class contains 1 method called Add which adds 2 numbers that are passed to it. 'This method is declared as Shared so other parts of my app can use it freely without first 'creating a new instance of DoMath first. This is the equivalent of placing a Public or Friend function 'inside a general C# module. Since Functions in a class are public by default, you do not have to 'include Public Shared....you can just use Shared if you like. I put them both here for completeness. '-------------------------------------------------------------------------------------------- Public Class DoMath Public Shared Function Add(ByVal a As Integer, ByVal b As Integer) As Integer Return a + b End Function End Class