When the ASP script in default.asp calls CHelloData, it declares a public variable, ConnectionString, to hold the data source name (DSN) contained in the global.asa file:
Option Explicit
'=== PUBLIC PROPERTIES
' These properties can be modified directly by the caller
Public ConnectionString As String
Next, the component declares the object variable objHelloData as an instance of the CHelloData class. The variable is declared as private, which means that it can only be used by the object that called it, ObjHello. This is an example of early binding; it sets up the method call the application needs to do its work by creating the object of which that method is a member. The code initializes the class, sets the default value for the member variable ConnectionString, and then, in the last line in the code fragment, creates the HelloData object inside the HelloWorld object. Since HelloData is a private class, it can only be created inside the class where it is declared; it cannot be called, for example, from the ASP page itself.
'=== PRIVATE DATA MEMBERS
Private objHelloData As CHelloData
Private Sub Class_Initialize()
' Set the class defaults here
ConnectionString = "DSN=Helloworld"
Set objHelloData = New CHelloData
End Sub
Because the CHelloData class is a private class defined within the boundaries of the Visual Basic project, we can safely instantiate it in the MTS context with the New keyword. The object is not created immediately; rather, Visual Basic will instantiate it when it is first used. The following subroutine is called later in the HelloWorld component's execution after CHelloData has done its work and returned. The subroutine deactivates HelloData.
Private Sub Class_Terminate()
Set objHelloData = Nothing
End Sub
The following code begins the subroutine that ultimately prints the string "VISUAL BASIC COMPONENT SAYS: Hello World" in the client's browser. First, an instance of the ASP Response object is created to hold values for the client's default language in the String variable strLang; strPhrase is declared to hold the phrase "Hello World" (or its French or German equivalent) for return to the calling ASP page. (Ultimately, this is the variable returned to the client for presentation to the user.)
Public Sub SayHello(Optional ByVal LanguageCode As String)
Dim objResponse As ASPTypeLibrary.Response
Dim strLang As String, strPhrase As String
On Error GoTo ErrHandler
The next lines of code contain the HelloWorld component's business logic. Within this fragment, the user's default language as an ISO 639-1 string is truncated to a two-character string used to indicate the major language on the client's browser. If no language is indicated, the component submits "en" to retrieve "Hello World" in English. The following graphic displays the SQL Server table as it is viewed from Visual InterDev.
' Business Logic:
' We're only interested in the major language (ISO 639-1),
' so we discard the locality. Also, if no language is
' specified, or if the phrase is not found, we resort to
' English (en).
If LanguageCode <> "" Then
strLang = Left(LanguageCode, 2)
Else
strLang = "en"
End If
The next two lines of code make up the heart of the application's functionality. They call into the HelloData object, retrieve the phrase "Hello World" in the appropriate language, and return the phrase to HelloWorld, the calling object. The first line sets the ConnectionString property to the correct DSN for the table, and the second line calls the GetPhrase method, passing it the two-letter code for the browser's default language.
objHelloData.ConnectionString = ConnectionString
strPhrase = objHelloData.GetPhrase(strLang)
Between these two lines of code, the execution leaves the HelloWorld object and begins executing the code in HelloData.
After program execution returns from HelloData, the following lines of code check the return value. If it is empty, it defaults to English and again calls into the HelloData object for the English phrase.
If strPhrase = "" Then
' We can assume there will always be a phrase for English.
strPhrase = objHelloData.GetPhrase("en")
End If
If HelloData.GetPhrase() has returned "Hello World" successfully, the next lines of code send it to the ASP page, along with the appropriate HTML code to make the string "VISUAL BASIC COMPONENT SAYS: Hello World" appear in the client's browser. To do this, the application uses the Write method of the ASP Response object.
' Finally, report the results to the user
Set objResponse = GetObjectContext.Item("Response")
objResponse.Write "<b>Visual Basic COMPONENT SAYS:</b> """ & strPhrase & """<br>" & vbCrLf
Set objResponse = Nothing
Exit Sub
ErrHandler:
' NOTE: This call will cause the component to halt execution
' and return an error to the caller.
Err.Raise Err.Number, Err.Source, Err.Description
End Sub