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

断了今生、忘了曾经 提交于 2020-01-14 06:56:21

问题


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


回答1:


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.




回答2:


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

Private Sub Class_Initialize()
    ' do initialization here

End Sub



回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/3382941/how-have-you-dealt-with-the-lack-of-constructors-in-vb6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!