Link to home
Start Free TrialLog in
Avatar of FalconMaster
FalconMasterFlag for United States of America

asked on

Passing array created with split function out of dll

In my dll,

let's say myobj.dll, with a class myClass


I am trying to pass back out an array created using the split function.

Ex.
-------------------------------
Public Property Get MyProperty() As Variant
  Dim vtArray
  vtArray = Split("1,2,3",",")
  MyProperty=vtArray
End Property

Public Property Get MyOtherProp() As Variant
  Dim vtArray
  vtArray = Array("1","2","3")
  MyOtherProp=vtArray
End Property
-------------------------------


If I have code like this in VBScript on a web page
---------------------------------------------------
Dim MyOb , vtArray1, vtArray2
Dim s1, s2
Set MyOb = CreateObject("myobj.myClass")

With MyOb
  vtArray1 = .MyProperty
  vtArray2 = .MyOtherProp
End With

s2 = vtArray2(1)
s1 = vtArray1(1)

------------------------------------
In this code, I will get an error on the line where
s1=vtArray1(1) saying that I have a type mismatch x800A000D

When I debug the code, both vtArray1, and vtArray2 show that I have arrays with "1","2", and "3" in them when I look in the locals window,

but somehow vtArray1 (the one created using the split function) doesn't want to act like an array as far as retrieving contents goes.  I can do a Ubound on it and everything, but I can't seem to get any of the individual items.

vtArray2 works fine.

Is anyone aware of this problem?  Do you know if there is a fix, or something I am doing wrong?

Please note.  I will usually have a comma delimited string of unknown size in the property that uses the split.

I am converting this dll from a VBScript class which did not have this problem at all.

I am hoping not to have to change a lot of pages, so telling me to just pass the string out of the dll and split it on the outside isn't really the answer for me.

Speed is key for me on this issue, so I am putting a lot of points down.
Avatar of rpai
rpai

I executed the exact same code and I had absolutely no issues.
Avatar of FalconMaster

ASKER

I'm running on Windows 2000, using VB 6.0

Does anyone know if that would make the difference?
Take a look at this article, might be of some help
http://www.15seconds.com/issue/010525.htm
I checked those sites out, and even tried some variations on what they said, but unfortunately they are only giving my information on how to pass arrays into a VB Dll, not back out.

There does not seem to be a specific way to pass the data out ByVal from a property or function.  I always assumed that they just did it this way by default.

Ex.

Function ByVal MyVal() As Variant
End Function

Property Get ByVal MyVal() As Variant
End Property
?????

The example above would not work., so if that is the problem, then there does not seem to be a solution.

I just checked it out on a Winnt 4 sp6 system, and it is having the same problems, so Win2k does not seem to be the problem.  I am not certain how it is that it is working fine on your system.

I noticed that it doesn't tell you of the error if you do not have script debugging on, it just doesn't work.  In asp, it always shows the error.

I noticed that on the debugger, the property that is created with the Split function says it is an Array of String, whereas the Array say it is an Array of Variant.

Maybe since the type of the property with the split function is variant, it is getting a type mismatch when it tries to assign a String, but it is my understanding that a Variant should be able to hold a string, so this still confuses me.

If I change the type to String, it bombs.  If I try to change it to a dynamic string array, it bombs.

Nothing seems to work.
I went ahead and maxed the points out on this to see if it will drive any more interest.
I can't seem to find that anyone has actually encountered this problem before, so chances are there is some sort of bug going on in my system(s).  Maybe I have a bad version of VB6 or something.

Anyway, I guess I'm just going to bite the bullet and spit the string out and split it on the outside.  I have a feeling that will take less time than trying to figure this one out.

I will leave the question up a couple more days to see if someone knows the answer.  

If not, I will remove the question.

thanks for the help rpai
ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Richie_Simonetti
Just for clarification: why did you use a variant as property return value?
Hello Experts !
I tried to implement this code to vb6 to check the values:

Option Explicit
Option Base 1

Private Sub Form_Load()
    Dim MyOb, vtArray1, vtArray2
    Dim s1(0 To 2), s2(0 To 2)
    Dim i As Byte
    Set MyOb = CreateObject("myobj.myClass")

    With MyOb
        vtArray1 = .MyProperty
        vtArray2 = .MyOtherProp
    End With

    For i = 0 To 2
        s2(i) = vtArray2(i)
        s1(i) = vtArray1(i)
        Debug.Print "s1(" & i & ") = '" & s1(i) & "'" & vbNewLine & "s2(" & i & ") = " & "'" & s2(i) & "'"
    Next
     
End Sub

Option Explicit

Public Property Get MyProperty() As Variant
    Dim vtArray
    vtArray = Split("1,2,3", ",")
    MyProperty = vtArray
End Property

Public Property Get MyOtherProp() As Variant
    Dim vtArray
    vtArray = Array("1", "2", "3")
    MyOtherProp = vtArray
End Property

Copy of the Debug Window:

s1(0) = '1'
s2(0) = '1'
s1(1) = '2'
s2(1) = '2'
s1(2) = '3'
s2(2) = '3'

Regards

V.K.
P.S.

Although i wrote "Option Base 1
" the app diidn't get an error.
AzraSound - I may try something like that, it looks good

Richie Simonetti - I don't know of any other way to pass an array besides variant.  There is no Array type that I am aware of.

VK - I may try your code to see if it is just the way I am doing it that is causing the problem.  

I checked, and I'm on SP 5 for VB6, so I guess I have the latest ver.

Anyway, I'm leaving for the evening.  I'll try to check these tomorrow morning and see what I can find.

ttfn
maybe with this?
public property get SomeName() as string()
end property
'This is the property in class module:
private m_arr() As String

Public Property Get SomeName() As String()
arr = Split("1,2,3,4", ",", , vbTextCompare)
SomeName = arr 'Array("1", "2", "3")
End Property


'This is test code:
Private Sub Form_Load()
Dim c As New dllTest.clsTest
Dim arr() As String
arr = c.SomeName
Dim i As Integer
For i = 0 To UBound(arr)
    Debug.Print arr(i)
Next i

End Sub
Richie_Simonetti - Tried that, and it didn't seem to like it.
"...and it didn't seem to like it. "
A little bit explanation? who doesn't like it?
I created the dll and a group of project adding a standard exe and instantiate the object just like i posted  and i can see the results in Inmediate Window.
Optionally, i could send the little sample.
Simonetti - "it" would be the system.  It gives me an error.  I looked at how you are doing yours, and it does not look as if you are running the dll in a VBScript type of environment.  That is where I am having the problem.

There seems to be plenty of information out there on how to convert data so that it is possible to pass an array from VBScript into a VB Dll, but passing an array created using Split in a VB Dll out to a VBScript page seems to be giving me lots of problems.

Apparently not everyone experiences these problems, so I cannot seem to pinpoint what is different about our system.

I have the same problems in the following environments

1. Use PWS and run ASP script with Debugging on in VB6 for Object class on Win2K system running IIS5

2. Compile Dll and run on Win2k system in ASP file

3. Use VB6 to compile dll, and then use DLL in Client Side VBScript on Win2K running IE 5.5

4. Use VB6 to compile dll, and then use DLL in Client Side VBScript on Winnt4 sp6 machine running IE6

All of these situations throw me back an error whenever I try to use the array that was passed back by the property using the split function to create the array.  The arrays work fine inside the VB6 environment and between VB6 applications.  The only problem seems to be the translation to VBScript.

The only common factor in any of these situations seems to be the Compiler we have been using to create the DLL, but we are using VB6 sp5, which I understand to be the latest SP.  So none of it really makes a whole lot of sense.
I think it may be due to your discovery regarding the Split function returning an array of type String, versus an array of type Variant.  I have had problems with the infamous "Type Mismatch" error in my ASP pages passing things as simple as Long data types between page and dll.  It will be interesting to see if the replacement Split function, which explicitly returns a type of Variant array as opposed to String, solves our problem.
Actually, Azra, I liked your idea, but found it would be even simpler to just create a function wrapper for the split function.

We went ahead and did this(see example below), and it seems to work just fine(I'm not sure how efficient it is, but this is VB, who am I kidding, of course it isn't efficient) It's probably still better than splitting it in uncompiled code.

I will award you the points for this one.


Example Split Wrapper/Converter:
Private Function locSplit(ByVal Text As String, Optional ByVal Delimiter As String = " ", _
   Optional ByVal Limit As Long = -1, Optional CompareMethod As _
   VbCompareMethod = vbBinaryCompare) As Variant
   Dim aNewArray()
   Dim aSplit as Variant
   Dim lSize as Long
   Dim lCount as Long
   
   aSplit = Split(Text, Delimiter, Limit, CompareMethod)  

   lSize = UBound(aSplit)

   Redim aNewArray(lSize)

   For lCount = 0 To lSize
     aNewArray(lCount) = aSplit(lCount)
   Next

   locSplit = aNewArray
End Function
Azra, It's not the perfect answer, but I think it's the best one possible, and it solves the problem, so I'm giving you an A.

Thanks for everyone's help and the quick replies
Unfortunatelly, i don't use ASP so i will send the dll to a friend that could try it.
Glad you found a solution (even if it wasn't ideal).  Good luck with everything.
Richie,

You could use VBScript inside of Internet Explorer Client side.

Here is what you do

After you make the dll, register it on your machine

[Start]:[Run]:
  regsvr32 "C:\SomeDirectory\MyFile.dll"
(or you could do the same thing from the DOS command shell)

Then in a web page add this
<HTML>
 <HEAD>
  <SCRIPT type="text\vbscript">
    Dim oObject

    Set oObject = CreateObject("MyFile.MyObj")

    'Where MyFile is the object dll you registered and
    'MyObj is the Object class in that file you want to use

    msgBox oObject.MyTextData

    'When you run this page, it will ask you if you
    'actually want to do that, as dlls from third parties
    'are potentially dangerous, and IE doesn't know if
    'your dll is from a third party or not

    'Just say yes

    'Assuming that the property or function MyTextData
    'returns valid string data, it should work
   </SCRIPT>
  </HEAD>
  <BODY>
    Some text
  </BODY>
</HTML>

when you are done, make sure to close the browser so it doesn't hold on to the object you created, then do the following

regsvr32 "C:\SomeDirectory\MyFile.dll" /u

to unregister the dll

then you can delete it if you want.
Richie -

Note: I don't know how much you know about object creation and registration, so I went through the whole process.

I hope it did not come across as condescending in any way.

Later
Richie -

Note: I don't know how much you know about object creation and registration, so I went through the whole process.

I hope it did not come across as condescending in any way.

Later
No problem. I just did it when you are posting previous one and ... know what? i had the same problem as YOU!!
It appears like VBScript is not so "smart" as VB itself converting data types. To me it appears like a bug... er... another bug in the product.
Cheers
Yup,

Thanks for the help.  

Also, thanks for confirming that I wasn't just crazy.