Visual Basic

Resource Files

Resource files are wonderful places for storing strings and graphics that a program might need. Unfortunately, Visual Basic limits us to only one resource file per project, which makes it difficult to use a generic resource file, as we usually require more application-specific data within the resource file.

One way around this limitation is to use a Resource Only Object Server (ROOS). A ROOS is simply an ActiveX component that has the sole purpose of returning data from its own resource file. The ROOS should contain methods to extract data from its internal resource file. You can add all sorts of functionality, such as giving the ROOS the ability to parse tokens and return fully qualified strings. This is similar to how many Microsoft applications work. For example, a ROOS's resource file might contain an entry like this:

IDS_ERROR_BADFILEOPEN  "Could not open the file %1, %2"

Calling the ROOS method, you could then code:

Dim sFileName      As String
  Dim vRoosParams    As Variant
  Dim oROOS          As MyROOS
  On Error Resume Next
  sFileName = "C:\MYFILE.TXT"
  Open sFileName For Input As #1
  If Err.Number <> 0 Then
      Set oROOS = New MyROOS
      ReDim vRoosParams(0 to 1)
      VRoosParams(0) = sFileName
      VRoosParams(1) = Err.Description
      MsgBox oROOS.GetString(IDS_ERROR_BADFILEOPEN, vRoosParams), _
          vbExclamation
  End If

In this example, an error 53 (File not found) would result in the following message: "Could not open the file C:\MYFILE.TXT, File not found." The ROOS code will in fact be base code that never needs to change. It simply provides an access method to the underlying resource file and gives you parsing functionality. You simply need to create the resource file for the project, attach it to the ROOS, and compile a DLL. You can compile each ROOS with a different class name, like ProjectXROOS or ProjectYROOS for whatever project you are using. This gives you the ability to use multiple resource files within any particular project. Chapter 14 gives a detailed example of how you might build a ROOS.