数组是否可以具有不同数据类型的值 [英] Could Array have values of different Data Type

查看:50
本文介绍了数组是否可以具有不同数据类型的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常数组不会有不同数据类型的值,但是当使用 Items 方法从字典对象中提取数据时.它处理不同数据类型的数据.

In general arrays won't have values of different data type but when Items method is used to extract data from dictionary object. It does data of different data type.

谁能解释一下,数组是否可能具有不同数据类型的值?

Could anyone clarify me on this, is it possible Arrays to have values of different data type?

Option Explicit

Dim D, i, Arr

Set D = CreateObject("Scripting.Dictionary")

D.CompareMode = VbTextCompare

D.Add "1", 56

D.Add "2", 78

 D.Add "3", "John"

D.Add "4", 100

Arr = D.Items

For i=0 to ubound(Arr) Step+1

Msgbox Arr(i)

Next

推荐答案

VBScript 的类型非常弱.所有变量都是变体(不同的子类型:整数、字符串、对象等).集合可以包含所有子类型的项目;这些项目甚至可以是不同的子类型.

VBScript is very weakly typed. All variables are Variants (of different subtypes: Integers, String, Objects, ...). Collections can hold items of all subtypes; the items can even be of different subtypes.

即使是字典的键也不一定是字符串;它们也可以是对象.WRT 这个关于字典和数组的问题,我必须补充:数组可以是项目,但不是字典的键:

Even the Keys of a Dictionary don't have to be Strings; they can be Objects just as well. WRT this question concerning Dictionaries and Arrays, I have to add: Arrays can be Items, but not Keys of a Dictionary:

>> Set d = CreateObject("Scripting.Dictionary")
>> d.Add "Company", Array("microsoft", "apple")
>> WScript.Echo Join(d("Company"))
>> a = d("Company") <== array assignment in VBScript COPIES!
>> a(1) = "samsung"
>> WScript.Echo Join(a)
>> WScript.Echo Join(d("Company"))
>>
microsoft apple
microsoft samsung
microsoft apple
>> d.Add a, "won't work"
>>
Error Number:       5
Error Description:  Invalid procedure call or argument

需要注意的一个子类型是固定数组(用 Dim a(ConstNumber) 定义).元素相当不受限制,但如果您能将固定数组放入集合中,我会感到惊讶.

A subtype to be careful with is Fixed Array (defined with Dim a(ConstNumber)). The elements are fairly unrestricted, but I'd be surprised if you could put a Fixed Array into a collection.

更新(写入固定数组):

固定数组及其元素以某种特殊方式存储以提高性能.我对细节一无所知,但它是一种特殊类型的数组(无法增长).

A fixed array and its elements are stored in some special way to improve performance. I don't know anything about the details, but it is a special type of array (which can't grow).

将某物放入一个集合中意味着:将某物的副本(类型 + 值)放入其中.如果某物是一个简单的变量或数组,则您会失去与原件的联系(再次考虑指出这一点,@Ansgar).如果某个东西是一个对象,则引用(对象)的副本仍然可以让您访问原始数据.

Putting something into a collection means: putting a copy of something (type + value(s)) in. If the something is a simple variable or an array, you loose the connection to the original (thinks again for pointing this out, @Ansgar). If the something is an object, the copy of the reference (object) still gives you access to the data of the original.

进入集合的固定数组的副本(好吧,测试仅使用字典)丢失了特殊类型:

The copy of a Fixed Array that goes into the collection (ok, the test uses only a dictionary) looses the special type:

Option Explicit

Dim Af(0)   : Af(0) = "fixed"
ReDim Ad(0) : Ad(0) = "dyn"
WScript.Echo 0, Af(0), Ad(0)

Dim dicX : Set dicX = CreateObject("Scripting.Dictionary")
dicX(0) = Af
dicX(1) = Ad
WScript.Echo 1, dicX(0)(0), dicX(1)(0)
Af(0)    = UCase(Af(0))
Ad(0)    = UCase(Ad(0))
WScript.Echo 2, Af(0), Ad(0)
WScript.Echo 3, dicX(0)(0), dicX(1)(0)
grow dicX(0)
grow dicX(1)
WScript.Echo 4, Join(dicX(0)), Join(dicX(1))
grow Ad
WScript.Echo 5, Join(Ad)
grow Af

Sub grow(a)
  ReDim Preserve a(Ubound(a) + 1)
  a(1) = "array"
  WScript.Echo "**", Join(a)
End Sub

输出:

cscript doa.vbs
0 fixed dyn
1 fixed dyn
2 FIXED DYN
3 fixed dyn
** fixed array
** dyn array
4 fixed dyn
** DYN array
5 DYN array
E:\trials\SoTrials\answers\15008949\vbs\doa.vbs(23, 3) Microsoft VBScript runtime error: This array is fixed or temporarily locked

** 固定数组 行证明传递给grow 的数组不再固定;它与不能生长的Af 不是同一类型.

The ** fixed array line proves that the array passed to grow is not fixed anymore; it's not of the same type as Af that can't grow.

这篇关于数组是否可以具有不同数据类型的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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