|
Prev: WMI for Local Security Policy
Next: Terminate Process Problem with Option Explicit Breaks functionality
From: vorpal on 5 Jul 2008 15:57 In VBS there is a function CVErr I works like so (using Excel 2003) Sub Test() Dim vTest As Variant vTest = CVErr(0) Debug.Print CStr(vTest) & vbTab & VarType(vTest) & vbTab & TypeName(vTest) End Sub The output is: Error 0 10 Error Although the VarType constant is valid in VBScript, the function is not. Does this VarType ever occur in vbScript? If so, when and how would I find it? Cheers, --Vorpal
From: Richard Mueller [MVP] on 5 Jul 2008 17:40 Vorpal wrote: > In VBS there is a function CVErr > I works like so (using Excel 2003) > Sub Test() > Dim vTest As Variant > > vTest = CVErr(0) > > Debug.Print CStr(vTest) & vbTab & VarType(vTest) & vbTab & > TypeName(vTest) > > End Sub > > The output is: > Error 0 10 Error > > Although the VarType constant is valid in VBScript, the function is > not. > > Does this VarType ever occur in vbScript? > If so, when and how would I find it? My reference shows that VarType = 10 means vbError. CVErr is not available in VBScript, but in VB6 it creates an error object with the number specified. However, I don't think error number 0 makes sense. I made a few attempts in VBScript to get the VarType function to return 10, but failed. -- Richard Mueller MVP Directory Services Hilltop Lab - http://www.rlmueller.net --
From: vorpal on 5 Jul 2008 17:56 On Jul 5, 2:40 pm, "Richard Mueller [MVP]" <rlmueller- nos...(a)ameritech.nospam.net> wrote: > My reference shows that VarType = 10 means vbError. CVErr is not available > in VBScript, but in VB6 it creates an error object with the number > specified. However, I don't think error number 0 makes sense. I made a few > attempts in VBScript to get the VarType function to return 10, but failed.. > My conclusion was that the VarType 10 is there for use by VBA, and I've used it in Excel UDFs many times. I'm going to conclude that is has no function in VBS. I thought perhaps it had a special purpose use, but if an MVP can't find a use for it, I won't expend any more thought energy on it. :) I never used it in VB6. Instead, I used Err.Raise. --Vorpal
From: Old Pedant on 7 Jul 2008 20:45 > My reference shows that VarType = 10 means vbError. CVErr is not available > in VBScript, but in VB6 it creates an error object with the number > specified. Ummm...I think that technically it does *NOT* create "an error object". Per the docs: http://msdn.microsoft.com/en-us/library/aa262707(VS.60).aspx And I quote: "Returns a Variant of subtype Error containing an error number specified by the user." A Variant is *NOT* an object. It truly is just the fundamental implementation of a variable. So it's just a block of 16 bytes where the variant number is a 10 and the value is the given error number. Same as a block of 16 bytes with a variant number of 3 and a value of 0x00010001 is seen as a LONG (32-bit integer) with a value of 65537. And that's the fundamental difference in error handling between VB6 and VBScript: VBScript really *DOES* use an error *OBJECT*, which is much richer in implementation than VB6's simple vbError variant. You *COULD* get a vbError instance in VBScript, by calling a COM component that returned a VARIANT with the right values in the right spots in the 16 bytes. And I suppose I could even think of instances (VBS calling a COM component written in VB6) where that might make sense. But of course all VBScript could do with that variant would be find its VarType and then use CSTR( ) on it. [I'm pretty sure that VariantChangeTypeEx( ), if asked to change a vbError variant to a vbString variant would do the same thing the VB6 does, creating the string Error NNN where NNN is the numeric value of the variant. It's the same principal as having an ADODB.Field object that has a value that is a decimal variant. VBScript can't handle that particular variant, but you can call CDBL( ) or CSTR( ) on the value to get it converted to a variant that VBScript can handle.
From: Old Pedant on 8 Jul 2008 00:54
So I hadn't worked with Variant in C++ code since 2001 and went to refresh my memory. Indeed, VT_ERROR is a standard variant type, with a value that is an SCODE. And an SCODE uses the same numeric values as HRESULT values. So think of it as an HRESULT wrapped into a Variant so that it's usable by any COM component and thus by the scripting languages (as well as VBA and VB6). But that means that clearly you should be able to do CLNG(x) or CSTR(x) where x is a VT_ERROR variant. |