VB6中的对象赋值 [英] Assignment of objects in VB6

查看:30
本文介绍了VB6中的对象赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过赋值语句在 VB6 中创建两个相同的对象;像这样...

I am attempting to create two identical objects in VB6 by assignment statements; something like this...

Dim myobj1 As Class1
Dim myobj2 As Class1

Set myobj1 = New Class1
myobj1.myval = 1
Set myobj2 = myobj1

很明显,这并没有创建两个对象,而是对同一个对象的两个引用,这不是我所追求的.有没有办法以这种方式创建第二个对象,或者我必须一次复制一个成员的对象...

It has become apparent that this doesn't create two objects but rather two references to the same object, which isn't what I am after. Is there any way to create a second object in this sort of way, or do I have to copy the object one member at a time...

Set myobj2 = new Class1
myobj2.mem1 = myobj1.mem1
...

?

编辑 2 Scott Whitlock 更新了他的出色答案,我已将他的更改合并到这个现在工作的代码片段中.

Edit 2 Scott Whitlock has updated his excellent answer and I have incorporated his changes into this now-working code snippet.

Private Type MyMemento
     Value1 As Integer
     Value2 As String
End Type

Private Memento As MyMemento

Public Property Let myval(ByVal newval As Integer)
Memento.Value1 = newval
End Property

Public Property Get myval() As Integer
myval = Memento.Value1
End Property

Friend Property Let SetMemento(new_memento As MyMemento)
    Memento = new_memento
End Property

Public Function Copy() As Class1
     Dim Result As Class1
     Set Result = New Class1
     Result.SetMemento = Memento
     Set Copy = Result
End Function

然后在代码中执行赋值......

One then performs the assignment in the code thus...

Set mysecondobj = myfirstobj.Copy

推荐答案

和许多现代语言一样,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

这篇关于VB6中的对象赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆