Solved

Passing array created with split function out of dll

Posted on 2002-06-05
29
310 Views
Last Modified: 2008-03-04
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.
0
Comment
Question by:FalconMaster
  • 13
  • 8
  • 3
  • +2
29 Comments
 
LVL 5

Expert Comment

by:rpai
ID: 7056731
I executed the exact same code and I had absolutely no issues.
0
 
LVL 2

Author Comment

by:FalconMaster
ID: 7056883
I'm running on Windows 2000, using VB 6.0

Does anyone know if that would make the difference?
0
 
LVL 5

Expert Comment

by:rpai
ID: 7057134
Take a look at this article, might be of some help
http://www.15seconds.com/issue/010525.htm
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 5

Expert Comment

by:rpai
ID: 7057178
0
 
LVL 2

Author Comment

by:FalconMaster
ID: 7057269
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.
0
 
LVL 2

Author Comment

by:FalconMaster
ID: 7057275
I went ahead and maxed the points out on this to see if it will drive any more interest.
0
 
LVL 2

Author Comment

by:FalconMaster
ID: 7057339
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
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 500 total points
ID: 7057442
>>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

Doesn't make much sense, but its worth checking into.  Try using this replacement Split function that will return an array of type Variant to see if it makes a difference:

Function Split(ByVal Text As String, Optional ByVal Delimiter As String = " ", _
    Optional ByVal Limit As Long = -1, Optional CompareMethod As _
    VbCompareMethod = vbBinaryCompare) As Variant
    ReDim res(0 To 100)
    Dim resCount As Long
    Dim length As Long
    Dim startIndex As Long
    Dim endIndex As Long
   
    length = Len(Text)
    startIndex = 1
   
    Do While startIndex <= length And resCount <> Limit
        ' get the next delimiter
        endIndex = InStr(startIndex, Text, Delimiter, CompareMethod)
        If endIndex = 0 Then endIndex = length + 1
       
        ' make room in the array, if necessary
        If resCount > UBound(res) Then
            ReDim Preserve res(0 To resCount + 99)
        End If
        ' store the new element
        res(resCount) = Mid$(Text, startIndex, endIndex - startIndex)
        resCount = resCount + 1
       
        startIndex = endIndex + Len(Delimiter)
    Loop
   
    ' trim unused values
    ReDim Preserve res(0 To resCount - 1)

    ' return the array inside a Variant
    Split = res()

End Function
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7057487
Just for clarification: why did you use a variant as property return value?
0
 
LVL 6

Expert Comment

by:VK
ID: 7057597
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.
0
 
LVL 6

Expert Comment

by:VK
ID: 7057601
P.S.

Although i wrote "Option Base 1
" the app diidn't get an error.
0
 
LVL 2

Author Comment

by:FalconMaster
ID: 7057665
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
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7057725
maybe with this?
public property get SomeName() as string()
end property
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7057752
'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
0
 
LVL 2

Author Comment

by:FalconMaster
ID: 7057995
Richie_Simonetti - Tried that, and it didn't seem to like it.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7059144
"...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.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7059205
Optionally, i could send the little sample.
0
 
LVL 2

Author Comment

by:FalconMaster
ID: 7059574
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.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7059606
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.
0
 
LVL 2

Author Comment

by:FalconMaster
ID: 7059671
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
0
 
LVL 2

Author Comment

by:FalconMaster
ID: 7059691
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
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7059694
Unfortunatelly, i don't use ASP so i will send the dll to a friend that could try it.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7059736
Glad you found a solution (even if it wasn't ideal).  Good luck with everything.
0
 
LVL 2

Author Comment

by:FalconMaster
ID: 7059882
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.
0
 
LVL 2

Author Comment

by:FalconMaster
ID: 7059898
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
0
 
LVL 2

Author Comment

by:FalconMaster
ID: 7059914
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
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7059940
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
0
 
LVL 2

Author Comment

by:FalconMaster
ID: 7060065
Yup,

Thanks for the help.  

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

Expert Comment

by:Richie_Simonetti
ID: 7063836
:))
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question