D. Edward Mitchell 16:00, 14 April 2020 (UTC) Hello World!    groupKOS Developer Share —usually UNDER CONSTRUCTION

Grandpa's VB6 code unique strings module

From groupKOS Developer Share
Jump to navigation Jump to search


What this is

This is Microsoft Visual Basic 6.0 (VB6) source code —a BAS file (MyApp.BAS).

A public function, uStr(), serves to store strings in an array, and returns the array index to use as an index to access the stored string.

If a string exists already, the uStr() function returns the index of the first string, and discards the duplicate value.

In this way, every stored string is unique.

But why?

Because Grandpa (3rd person of me) was strange, a.k.a., Codasaurus Hex.

There are many data widgets in every modern programming language, like collections (VB6), dictionaries, keyed-arrays, and on.

The unique strings function can be upgraded to a binary-tree storage structure, and implemented as a static-array with dynamic-pointers.

This puts the humble check-everything linear search used in this function, into a big-O access machine.

A big-O device can operate on a data file of any size with little performance decrease as the file grows larger.

The improved binary tree unique strings module becomes the workhorse for categorical analysis, wherein every data value is a categorized, with a real system having perhaps millions of possible categories of numbers.

With a QAT paradigm, the big-O access over unique strings makes one of two category engines that comprise quasi axiomatic analysis by looking for categorical coincidence.

By comparing live operations with two categories found existing in a data stream over time, dynamical dependencies may be revealed as they happen between those two categories.

One category is the data measured. Other categories come from separate data that is coincidental in time with the data measurement. Should there be dynamical relationships of one category to another, then a regularity pertinent to the dynamic relationship of the category-pair is observable.

An example, learned from a Ph.D of mathematics — if categorical analysis is performed on stock-market data, the phase of the moon can be observed as a dynamical operator in the stock market (slight, but evident).

Back in the day

Grandpa used modules as quick classes, because they can have private variables, and public and private functions. No explicit instancing needed.

VB6 can do normal class definitions and instancing in a basic way, too, of course.

Read more trivia below.


As an instancing method doesn't happen with a module, the storage method below, uStr(qStr), is auto-allocated on first use, and allocates again as needed by chunk.

A VB 6.0 BAS code module —UniqueStrings

 

By Codasaurus Hex

' VB6 code module: unique_strings.bas v.0.0.0
'   by DonEMitchell a.k.a. Codasaurus Hex
'   Coding style: Grandpa's VB6 
'   Started Dec 23, 2019
'   Please note: No ads were blocked in the creation of this document.
'   This was created on a valid VB6 license, gifted from Microsoft for
'   participating in a user-experience study for release of some new
'   version of MS Office Excel VBA new edition spiffy dingies.
'   This document was created on Windows 10, running VB6.exe natively,
'   uninstalled, and portably from there to there with a folder copy.

Option Explicit ' A VB6 compiler setting.  Standby.
Option Base 1   ' Instance all arrays beginning with element 1 (zero plus one)

'' Dimension variables before any methods to make them
'' module-scope variables and constants (seen within module methods)
Dim mlngLastIndex As Long  ' mlng <---> module long abbreviation
Const chunk As Long = 64   ' Fun to say
Dim uStrs()    As String   ' If this were 1997, this should be mlngArUStrs()
                           ' or such kind of disambiguation,
                           ' which does help clarify strange code.
                           

Public Function uStr(ndx As Long) As String
    On Error Resume Next
    uStr = uStrs(ndx)
End Function

Public Function uNdx(qStr As String) As Long
    ' Add unique strings to uStrs()
    ' Return ndx (array index) of existing or unique qStr
    ' Return zero on any error
    ' Note: For large data-sets, a linear search takes large time,
    '       and slows performance.  Then use a hash or binary tree.
    Dim i
    
    On Error GoTo errTrap
    
    ' Virgin uStrs() array?
    If mlngLastIndex = 0 Then
        ReDim uStrs(chunk)
    End If
    
    ' Linear search from first through last array elements, or until found.
    For i = 1 To mlngLastIndex
        If qStr = uStrs(i) Then
            ' qStr found in array
            ' Return existing index
            uNdx = i

            Exit Function
        End If
    Next
    
    ' So now, qStr is unique in the uStrs() array
    ' Check & allocate array storage if needed
    If mlngLastIndex + 1 > UBound(uStrs) Then ReDim Preserve uStrs(mlngLastIndex + chunk)
    
    uStrs(mlngLastIndex + 1) = qStr    ' The storage is completed here (we're not in the error trap)
    
    ' Augment the module index to the new successful index and return it 
    ' Bail now before the error trap
    mlngLastIndex = mlngLastIndex + 1
    uNdx = mlngLastIndex
    
    Exit Function
errTrap:
    ' Return failure (zero)
    uNdx = 0
End Function


VB 6 trivia

A Visual Basic 6.0 code module is saved as a BAS file. 'Code module' means a BAS file in the VB IDE (an Integrated Development Environment... it edits and debugs and compiles and phones home).

A Visual Basic 6.0 code module is scanned at start-up of the IDE, then treated like an object with scoped methods and properties. The code module is 'objectified', so to speak, by the built-in 'intelli-sense' now prevalent an any modern programmer's editor. Most any. An intelli-sense objectifies a coding project's properties and objects and such, and makes them available in the editor at the typing cursor. This is much like the smart-completion with drop-down selections in a browser's editable URL text line. Intelli-sense brought everything every already typed into a project available as a pop-up list —when the syntax needed it. Intelligent enough. It was hot stuff.

The property name given to the BAS code module can also be used in code statements to address the code module like a class.

The code module is 'instanced' per se at start-up.

A module is similar to a global singleton class instance, not creatable twice, that is auto-instanced at start-up.

The code module will support 'Property Get()' and 'Property Let()' statements. Variables can be declared 'Public' or 'Private'. Public variables are 'seen' by the entire project, usable in all code modules.

Passing class-instances and user-defined data types to a function in a module is possible. But, the IDE will not compile if a user-type or class is passed to a function in the same form module.

VB6 is responsible for reclaiming array storage.

Visual Basic 6.0 had its own magazine for, at the time, us two million suddenly marketable biz-developers. We were marketable because we were IT people mostly, that could suddenly weave apps. Our marketability was that we came with IT speak, and could understand IT people's Microsoft world of crazy.

While VB6 lacks most modern code editor necessities, a graphical form editor and seamless integration of mouse and keyboard (etc.) code events made front-end development possible, and cost effective. VB 6.0 created a season in the dot-com bubble called rapid-app-development.

If zero references to external dependencies are added to a VB 6.0 project, that project will compile and run on any Windows platform since back in the day (Before Windows XP?). This was very convenient while working with a computer scientist that needed a portable application.

Writing a program with zero external dependencies in VB 6.0 is stark. The popular Common Control shipped with VB 6.0, but even that was an external reference, and required that VB6 apps were bundled into an installer, such that VB6 could be registered to access external executables already on the local box.