开发者

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

VB6 classes have no 开发者_如何学Goparameterized constructors. What solution have you chosen for this? Using factory methods seems like the obvious choice, but surprise me!


I usually stick to factory methods, where I put the "constructors" for related classes in the same module (.BAS extension). Sadly, this is far from optimal since you can't really limit access to the normal object creation in VB6 - you just have to make a point of only creating your objects through the factory.

What makes it worse is having to jump between the actual object and your factory method, since organization in the IDE itself is cumbersome at best.


How about using the available class initializer? This behaves like a parameterless constructor:

Private Sub Class_Initialize()
    ' do initialization here

End Sub


I use a mix of factory functions (in parent classes) that then create an instance of the object and call a Friend Init() method.

Class CObjects:

Public Function Add(ByVal Param1 As String, ByVal Param2 As Long) As CObject
  Dim Obj As CObject
  Set Obj = New CObject
  Obj.Init Param1, Param2
  Set Add = Obj
End Function

Class CObject:

Friend Sub Init(ByVal Param1 As String, ByVal Param2 As Long)
  If Param1 = "" Then Err.Raise 123, , "Param1 not set"
  If Param2 < 0 Or Param2 > 15 Then Err.Raise 124, , "Param2 out of range"

  'Init object state here
End Sub

I know the Friend scope won't have any effect in the project, but it acts as a warning that this is for internal use only. If these objects are exposed via COM then the Init method can't be called, and setting the class to PublicNotCreatable stops it being created.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜