且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

VB6中的对象赋值

更新时间:2023-10-03 17:28:16

和许多现代语言一样,VB6 也有值类型和引用类型.类定义引用类型.另一方面,像 Integer 这样的基本类型是值类型.

Like many modern languages, VB6 has value types and reference types. Classes define reference types. On the other hand, your basic types like Integer are value types.

基本区别在于赋值:

Dim a as Integer
Dim b as Integer
a = 2
b = a
a = 1

结果是 a 为 1 而 b 为 2.这是因为值类型中的赋值会产生副本.这是因为每个变量都在堆栈上为 value 分配了空间(在 VB6 的情况下,Integer 占用堆栈上的 2 个字节).

The result is that a is 1 and b is 2. That's because assignment in value types makes a copy. That's because each variable has space allocated for the value on the stack (in the case of VB6, an Integer takes up 2 bytes on the stack).

对于类,它的工作方式不同:

For classes, it works differently:

Dim a as MyClass
Dim b as MyClass
Set a = New MyClass
a.Value1 = 2
Set b = a
a.Value1 = 1

结果是 a.Value1b.Value1 都是 1.这是因为对象的状态是存储在堆中,而不是堆栈中.只有对象的 reference 存储在堆栈中,因此 Set b = a 会覆盖该引用.有趣的是,VB6 通过强制您使用 Set 关键字来明确说明这一点.大多数其他现代语言不需要这个.

The result is that both a.Value1 and b.Value1 are 1. That's because the state of the object is stored in the heap, not on the stack. Only the reference to the object is stored on the stack, so Set b = a overwrites the reference. Interestingly, VB6 is explicit about this by forcing you to use the Set keyword. Most other modern languages don't require this.

现在,您可以创建自己的值类型(在 VB6 中它们被称为用户定义类型,但在大多数其他语言中它们被称为结构或结构).这是教程.

Now, you can create your own value types (in VB6 they're called User Defined Types, but in most other languages they're called structs or structures). Here's a tutorial.

类和用户定义类型之间的区别(除了类是引用类型而 UDT 是值类型之外)是类可以包含 UDT 不能包含的行为(方法和属性).如果您只是在寻找记录类型的类,那么 UDT 可能是您的解决方案.

The differences between a class and a user defined type (aside from a class being a reference type and a UDT being a value type) is that a class can contain behaviors (methods and properties) where a UDT cannot. If you're just looking for a record-type class, then a UDT may be your solution.

您可以混合使用这些技术.假设您需要一个类,因为您有某些行为和计算要与数据一起包含.您可以使用 memento 模式 来保存 UDT 内对象的状态:

You can use a mix of these techniques. Let's say you need a Class because you have certain behaviors and calculations that you want to include along with the data. You can use the memento pattern to hold the state of an object inside of a UDT:

Type MyMemento
    Value1 As Integer
    Value2 As String
End Type

在您的班级中,确保所有您的内部状态存储在 MyMemento 类型的私有成员中.编写您的属性和方法,以便它们只使用该私有成员变量中的数据.

In your class, make sure that all your internal state is stored inside a private member of type MyMemento. Write your properties and methods so they only use data in that one private member variable.

现在制作对象的副本很简单.只需在你的类上编写一个名为 Copy() 的新方法,它会返回你的类的一个新实例,并用它自己的备忘录副本对其进行初始化:

Now making a copy of your object is simple. Just write a new method on your class called Copy() that returns a new instance of your class and initialize it with a copy of its own memento:

Private Memento As MyMemento

Friend Sub SetMemento(NewMemento As MyMemento)
    Memento = NewMemento
End Sub

Public Function Copy() as MyClass
    Dim Result as MyClass
    Set Result = new MyClass
    Call Result.SetMemento(Memento)
    Set Copy = Result
End Function

Friend 仅对项目外部的内容隐藏它,因此隐藏 SetMemento 子程序并没有多大作用,但这是您使用 VB6 所能做的一切.

The Friend only hides it from stuff outside your project, so it doesn't do much to hide the SetMemento sub, but it's all you can do with VB6.

HTH