How have you dealt with the lack of constructors in VB6?

Viewed 12640

VB6 classes have no parameterized constructors. What solution have you chosen for this? Using factory methods seems like the obvious choice, but surprise me!

4 Answers

This solution is far from perfect, however, this is what I ended up doing for some of my legacy projects. It's somewhat memory-efficient for storage, but just looking up something definitely takes more time than otherwise required, especially for the huge structs.

Suppose you need to implement the following,

type user
 id as long
 name as String
 desc as String
end type

But notice that VB6 natively supports Strings. So Instead of using structs, have "fake-struct" functions such as

Public Function user_raw(ByVal id as Long, ByRef name as String, ByRef desc as String) as String
 Const RAW_DELIM as String = "|"
 user_raw = cstr(id) & RAW_DELIM & name & RAW_DELIM & desc 
End Function

So as a result, you will have passable-by-value (hence stackable into arrays, or multiline Strings), "fake-structs" made of strings such as

1|Foo|The boss of VB

2|Bar|Foo's apprentice

So the construct also counts as a crude way to perform to_string(). On the slightly good side, such storage is almost instantly convertible to the Markdown table syntax.

Later on, you can retrieve data from the struct by doing something like,

Public const RAW_DELIM as String = "|"
Public Const POS_USER_ID = 0
Public Const POS_USER_NAME = 1
Public Const POS_USER_DESC = 2
'...
public function get_raw_variable(byref user as String, byval var as integer) as String
 dim buf() as String

 buf = SPLIT(user, "|")
 Assert ubound(buf) <= var
 get_raw_variable = buf(var)
end function

The retrieval is sure inefficient, but is about the best you can have. Good luck

Related