Difference between revisions of "Creating Public and Private Functions in VB6"

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search
 
(2 intermediate revisions by one user not shown)
Line 1: Line 1:
== Function Example ==
+
== Standard Private Function ==
 +
 
 +
=== example ===
  
 
  Private Function fnFormatPhoneNum(strIn As String) As String
 
  Private Function fnFormatPhoneNum(strIn As String) As String
Line 19: Line 21:
  
 
== Public Function in a Module ==
 
== Public Function in a Module ==
 +
 +
=== example ===
  
 
You may want to create a function that is available to all forms in your program.  You can put it in a module and make it public so that it may be used by any form.
 
You may want to create a function that is available to all forms in your program.  You can put it in a module and make it public so that it may be used by any form.
Line 35: Line 39:
  
 
   strRetval = MODULE_NAME.GetSettingEx(HKEY_CURRENT_USER, "Software\album", "DefaultStorybookPath")
 
   strRetval = MODULE_NAME.GetSettingEx(HKEY_CURRENT_USER, "Software\album", "DefaultStorybookPath")
 +
 +
=== troubleshooting ===
 +
 +
* Compile Error: Method or data member not found
 +
 +
I bet you made the function "Private" instead of "Public".  Make sure the function in the module is a "Public Function".
  
 
 
 
 

Latest revision as of 14:19, 10 January 2008

Standard Private Function

example

Private Function fnFormatPhoneNum(strIn As String) As String
  Dim strLen As Integer
  strIn = Trim(strIn):   strLen = Len(strIn)
  Select Case strLen
  Case 10
    fnFormatPhoneNum = "(" & Right(strIn, 3) & ") " & Mid(strIn, 3, 3) & "-" & Left(strIn, 4)
  Case 7
    fnFormatPhoneNum = Right(strIn, 3) & "-" & Left(strIn, 4)
  Case Else
    fnFormatPhoneNum = strIn
  End Select
End Function

Here's how to call the function above from the program code:

 strVariableName = fnFormatPhoneNum("" & arCompanies(cnt, 4))

Public Function in a Module

example

You may want to create a function that is available to all forms in your program. You can put it in a module and make it public so that it may be used by any form.

MODULE CODE:

 Public Function GetSettingEx(HKEY As Long, sPath As String, sValue As String)
   Dim KeyHand&
   Dim datatype&
   Call RegOpenKey(HKEY, sPath, KeyHand&)
   GetSettingEx = RegQueryStringValue(KeyHand&, sValue)
   Call RegCloseKey(KeyHand&)
 End Function

FORM CODE:

 strRetval = MODULE_NAME.GetSettingEx(HKEY_CURRENT_USER, "Software\album", "DefaultStorybookPath")

troubleshooting

  • Compile Error: Method or data member not found

I bet you made the function "Private" instead of "Public". Make sure the function in the module is a "Public Function".