Type Mismatch - Array of Type Long 
Author Message
 Type Mismatch - Array of Type Long

Hi,

I posted a question some time ago about a dll that was returning an array of
variants. Michael answered my post explaining that I needed to convert the
data that was sent because VBScript was not capable of handling this type of
an array. That worked very well. Now I have another problem in the reverse
way, I have an object that is expecting an Array of Longs and I was
wondering if anyone had an Idea of how to do this. Is it possible to create
an Array, and then pass it to a DLL that would convert it to an Array of
Longs and then return that array to VBScript so that I can pass it to the
object expecting this array ?

I hope this post is not to confusing.

Thanks for any Help !

Jim



Sun, 03 Jul 2005 09:49:21 GMT  
 Type Mismatch - Array of Type Long
Hi Jim

Array is not a problem. Problem is data type. VBS can send/receive only
variant to/from callee. So no matter to what type the first callee change
the data type, the return value should be variant. In short, unfortunately,
your plan is impossible as far as I know.

--
Have a nice day.
Han Pohwan, Microsoft MVP, Korea


Quote:
> Hi,

> I posted a question some time ago about a dll that was returning an array
of
> variants. Michael answered my post explaining that I needed to convert the
> data that was sent because VBScript was not capable of handling this type
of
> an array. That worked very well. Now I have another problem in the reverse
> way, I have an object that is expecting an Array of Longs and I was
> wondering if anyone had an Idea of how to do this. Is it possible to
create
> an Array, and then pass it to a DLL that would convert it to an Array of
> Longs and then return that array to VBScript so that I can pass it to the
> object expecting this array ?

> I hope this post is not to confusing.

> Thanks for any Help !

> Jim



Mon, 04 Jul 2005 11:35:32 GMT  
 Type Mismatch - Array of Type Long

Quote:
> ... Is it possible to create an Array, and then pass
> it to a DLL that would convert it to an Array of Longs and then
> return that array to VBScript so that I can pass it to the object
> expecting this array ?

Yes...

set o = createobject("myserver.myclass")
dim ar(1)
ar(0) = 1
ar(1) = 2

arLongs = o.VariantArrayToLongArray(ar)

msgbox typename(arLongs) 'displays Long() - an array of Long integers

====================================

Where myclass has a public function like this simple sample.  Robust code
would include sanity check the passed vArray with IsArray and check each
element with IsNumeric as well...

Public Function VariantArrayToLongArray(ByRef vArray As Variant) As Variant

  Dim i As Integer
  ReDim LongArray(LBound(vArray) To UBound(vArray)) As Long

  For i = LBound(vArray) To UBound(vArray)
    LongArray(i) = CLng(vArray(i))
  Next

  VariantArrayToLongArray = LongArray

End Function

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Mon, 04 Jul 2005 12:21:52 GMT  
 Type Mismatch - Array of Type Long
WOW I've never thought about that, Micahael. Looks like magic.

By the way one more magicical behavior in dark side is,

arLongs = o.VariantArrayToLongArray(ar) 'Now long type array is set.

So I made second callee which restricts the parameter to long.
Public Function SECONDCALLEE(ByRef vArray As LONG) As Variant

Now I called it,
secondCall=SECONDCALLEE(arLongs)

Then the secondCallee still rejects the parameter like any other variant
parameter. Mysterious.
--
Have a nice day.
Han Pohwan, Microsoft MVP, Korea



Quote:
> > ... Is it possible to create an Array, and then pass
> > it to a DLL that would convert it to an Array of Longs and then
> > return that array to VBScript so that I can pass it to the object
> > expecting this array ?

> Yes...

> set o = createobject("myserver.myclass")
> dim ar(1)
> ar(0) = 1
> ar(1) = 2

> arLongs = o.VariantArrayToLongArray(ar)

> msgbox typename(arLongs) 'displays Long() - an array of Long integers

> ====================================

> Where myclass has a public function like this simple sample.  Robust code
> would include sanity check the passed vArray with IsArray and check each
> element with IsNumeric as well...

> Public Function VariantArrayToLongArray(ByRef vArray As Variant) As
Variant

>   Dim i As Integer
>   ReDim LongArray(LBound(vArray) To UBound(vArray)) As Long

>   For i = LBound(vArray) To UBound(vArray)
>     LongArray(i) = CLng(vArray(i))
>   Next

>   VariantArrayToLongArray = LongArray

> End Function

> --
> Michael Harris
> Microsoft.MVP.Scripting
> Seattle WA US



Mon, 04 Jul 2005 13:48:04 GMT  
 Type Mismatch - Array of Type Long

Quote:

> WOW I've never thought about that, Micahael. Looks like magic.

> By the way one more magicical behavior in dark side is,

> arLongs = o.VariantArrayToLongArray(ar) 'Now long type array is set.

> So I made second callee which restricts the parameter to long.
> Public Function SECONDCALLEE(ByRef vArray As LONG) As Variant

> Now I called it,
> secondCall=SECONDCALLEE(arLongs)

> Then the secondCallee still rejects the parameter like any other
> variant parameter. Mysterious.

Try this:

set o = createobject("myserver.myclass")
dim ar(1)
ar(0) = 1
ar(1) = 2

arLongs = o.VariantArrayToLongArray(ar)

msgbox typename(arLongs) 'displays Long() - an array of Long integers

arLong2 = o.LongArrayToVariantArray(o.VariantArrayToLongArray(ar))

msgbox typename(arLong2) 'displays Variant() - an array of variants

arLong3 = o.LongArrayToVariantArray((arLongs))

msgbox typename(arLong3) 'displays Variant() - an array of variantss

Where myclass now contains:

Public Function VariantArrayToLongArray(ByRef vArray As Variant) As Variant
  Dim i As Integer
  ReDim LongArray(LBound(vArray) To UBound(vArray)) As Long
  For i = LBound(vArray) To UBound(vArray)
    LongArray(i) = CLng(vArray(i))
  Next
  VariantArrayToLongArray = LongArray
End Function

Public Function LongArrayToVariantArray(ByRef lngArray() As Long) As Variant
  Dim i As Integer
  ReDim VariantArray(LBound(lngArray) To UBound(lngArray)) As Variant
  For i = LBound(lngArray) To UBound(lngArray)
    VariantArray(i) = CLng(lngArray(i))
  Next
  LongArrayToVariantArray = VariantArray
End Function

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Tue, 05 Jul 2005 11:27:55 GMT  
 Type Mismatch - Array of Type Long
Michael,

once again THANK YOU

Jim



Quote:

> > WOW I've never thought about that, Micahael. Looks like magic.

> > By the way one more magicical behavior in dark side is,

> > arLongs = o.VariantArrayToLongArray(ar) 'Now long type array is set.

> > So I made second callee which restricts the parameter to long.
> > Public Function SECONDCALLEE(ByRef vArray As LONG) As Variant

> > Now I called it,
> > secondCall=SECONDCALLEE(arLongs)

> > Then the secondCallee still rejects the parameter like any other
> > variant parameter. Mysterious.

> Try this:

> set o = createobject("myserver.myclass")
> dim ar(1)
> ar(0) = 1
> ar(1) = 2

> arLongs = o.VariantArrayToLongArray(ar)

> msgbox typename(arLongs) 'displays Long() - an array of Long integers

> arLong2 = o.LongArrayToVariantArray(o.VariantArrayToLongArray(ar))

> msgbox typename(arLong2) 'displays Variant() - an array of variants

> arLong3 = o.LongArrayToVariantArray((arLongs))

> msgbox typename(arLong3) 'displays Variant() - an array of variantss

> Where myclass now contains:

> Public Function VariantArrayToLongArray(ByRef vArray As Variant) As
Variant
>   Dim i As Integer
>   ReDim LongArray(LBound(vArray) To UBound(vArray)) As Long
>   For i = LBound(vArray) To UBound(vArray)
>     LongArray(i) = CLng(vArray(i))
>   Next
>   VariantArrayToLongArray = LongArray
> End Function

> Public Function LongArrayToVariantArray(ByRef lngArray() As Long) As
Variant
>   Dim i As Integer
>   ReDim VariantArray(LBound(lngArray) To UBound(lngArray)) As Variant
>   For i = LBound(lngArray) To UBound(lngArray)
>     VariantArray(i) = CLng(lngArray(i))
>   Next
>   LongArrayToVariantArray = VariantArray
> End Function

> --
> Michael Harris
> Microsoft.MVP.Scripting
> Seattle WA US



Tue, 05 Jul 2005 13:41:45 GMT  
 Type Mismatch - Array of Type Long
Limitless.

--
Have a nice day.
Han Pohwan, Microsoft MVP, Korea



Quote:

> > WOW I've never thought about that, Micahael. Looks like magic.

> > By the way one more magicical behavior in dark side is,

> > arLongs = o.VariantArrayToLongArray(ar) 'Now long type array is set.

> > So I made second callee which restricts the parameter to long.
> > Public Function SECONDCALLEE(ByRef vArray As LONG) As Variant

> > Now I called it,
> > secondCall=SECONDCALLEE(arLongs)

> > Then the secondCallee still rejects the parameter like any other
> > variant parameter. Mysterious.

> Try this:

> set o = createobject("myserver.myclass")
> dim ar(1)
> ar(0) = 1
> ar(1) = 2

> arLongs = o.VariantArrayToLongArray(ar)

> msgbox typename(arLongs) 'displays Long() - an array of Long integers

> arLong2 = o.LongArrayToVariantArray(o.VariantArrayToLongArray(ar))

> msgbox typename(arLong2) 'displays Variant() - an array of variants

> arLong3 = o.LongArrayToVariantArray((arLongs))

> msgbox typename(arLong3) 'displays Variant() - an array of variantss

> Where myclass now contains:

> Public Function VariantArrayToLongArray(ByRef vArray As Variant) As
Variant
>   Dim i As Integer
>   ReDim LongArray(LBound(vArray) To UBound(vArray)) As Long
>   For i = LBound(vArray) To UBound(vArray)
>     LongArray(i) = CLng(vArray(i))
>   Next
>   VariantArrayToLongArray = LongArray
> End Function

> Public Function LongArrayToVariantArray(ByRef lngArray() As Long) As
Variant
>   Dim i As Integer
>   ReDim VariantArray(LBound(lngArray) To UBound(lngArray)) As Variant
>   For i = LBound(lngArray) To UBound(lngArray)
>     VariantArray(i) = CLng(lngArray(i))
>   Next
>   LongArrayToVariantArray = VariantArray
> End Function

> --
> Michael Harris
> Microsoft.MVP.Scripting
> Seattle WA US



Wed, 06 Jul 2005 07:47:05 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Prob. w\ TYPE data type: err:Type Mismatch

2. Convert array of value type to array of reference type

3. Type mismatch when the types are correct ?

4. Type mismatch on user defined data type

5. Type Mismatch reading from Access 2K of type Date

6. Passing user data type to function causes type mismatch error

7. Type mismatch on array

8. type mismatch when modifying an array

9. Type Mismatch for Array

10. Type mismatch transferrring recordset to array?

11. Type mismatch in a private array defined in a class

12. Type Mismatch on my array elements.

 

 
Powered by phpBB® Forum Software