VB 6 Static Function

Advertisements
Advertisements

In the previous article, you learned about functions that returned values. The variables that return the value are of two types – global and local. The local variable gets destroyed every time we call the function in our program.

The only solution to this problem is to make variable or function retain its previous value. This can be achieved using the static keyword in VB 6.

Example: Without static keyword

In this example program a function is called each time a button is clicked and display the count using a message box.

Private Sub Command1_Click()
Dim clkCount As Integer

clkCount = counter()
MsgBox ("Result =" + Str(clkCount))
End Sub

Static Function counter() As Integer
Dim count As Integer
count = count + 1
counter = count
End Function

Output – static function

Output - Static Function
Figure 1 – Output – Static Function

In the example above, each time a user clicks the button, the result is 1. The variable count is reset to 0. To retain the value of count either declare the variable as static or declare the entire function as static. One that’s done the function will retain the value even though we recall the function several times and increment the counter successfully.

Visual Basic 6 For Dummies 1st Edition

Create your own custom Visual Basic programs fast for Windows 95, Windows 98, and Windows NT!

If you want to develop business applications for Windows, then Microsoft’s Visual Basic 6 programming environment is for you. With its powerful and friendly object-oriented interface, Visual Basic 6 is the ideal tool for creating easy-to-use and attractive interfaces of your own. Whether you want to build original programs from the ground up or take advantage of existing wizards, Visual Basic 6 For Dummies will help you design and debug your work – and accomplish all your goals!

  • Covers using Visual Basic 6 with Windows 95, Windows 98, and Windows NT
  • Walks you through the ins and outs of object-oriented programming in Visual Basic and using the Visual Basic toolset
  • Shows you how to make use of menus and submenus, write subprograms that everyone can share, save and retrieve information in databases, integrate ActiveX controls into your applications, and build attractive user interfaces with buttons, boxes, and scroll bars

From the fundamentals of writing BASIC code to the thrill of distributing your very own custom applications, Visual Basic 6 For Dummies brings a fresh perspective to programming in Visual Basic 6 that everyone can understand.

Advertisements
Advertisements
Advertisements