Results 1 to 7 of 7

Thread: VBScript type mismatch error

  1. #1

    Thread Starter
    Addicted Member imbue's Avatar
    Join Date
    Aug 2002
    Location
    Midwest USA
    Posts
    155

    VBScript type mismatch error

    I have a Visual Basic program using the MScript control. This control runs some code that gets the value of a function from my VB program. This function returns an array to the VBS program. In the VBS program I can run isArray(), Lbound(), and Ubound() on the returned data, but as soon as I try to get the data (data(x)) it causes a type mismatch error. Why?

  2. #2
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    Any code?

  3. #3

    Thread Starter
    Addicted Member imbue's Avatar
    Join Date
    Aug 2002
    Location
    Midwest USA
    Posts
    155

    Sure.

    My full program is pretty large so I tried to make this as simple as possible.

    Create a new class with something like this in it:
    VB Code:
    1. Public Function f() As Variant
    2. 'this just returns an array
    3. Dim a(15) As String
    4. f = a
    5. End Function

    Now in your project create an object for that class and run the script control:
    VB Code:
    1. Dim clstests As New clsTest
    2. sc.AddObject "clstest", clstests
    3.  
    4. sc.AddCode CODE
    5. sc.Run "main"

    And the script should look like this:
    VB Code:
    1. sub main
    2. dim t
    3. t = clsTest.f
    4. msgbox t(0)
    5. end sub

    I'll attach a project to this post too.

    I've tried just about everything I can think of and this is driving me crazy.
    Attached Files Attached Files

  4. #4
    Hyperactive Member Anglo Saxon's Avatar
    Join Date
    Mar 2002
    Location
    Durham, UK
    Posts
    259
    Change the type declaration on this line :
    Code:
    Dim a(15) As String
    to :
    Code:
    Dim a(15) As Variant
    VBScript can only handle variants.


    ---
    Anglo Saxon

  5. #5

    Thread Starter
    Addicted Member imbue's Avatar
    Join Date
    Aug 2002
    Location
    Midwest USA
    Posts
    155

    Thumbs up

    Hey, thats fixes it. Thanks.

    I got a question though, I can make a function return a string and VBScript converts it, why can't I make an array of strings? Also won't this make my program run slower?

    Anyway, thanks for the help.

  6. #6
    Hyperactive Member Anglo Saxon's Avatar
    Join Date
    Mar 2002
    Location
    Durham, UK
    Posts
    259
    Good questions, not easily answered.
    Read this quote from MS :

    "The VBSCRIPT active scripting engine supplied by Microsoft only supports the indexing of SAFEARRAYs of VARIANTs. While VBSCRIPT is capable of accepting arrays of non-variant type for the purposes of boundary checking and passing it to other automation objects, the engine does not allow manipulation of the array contents at this time. "

    If you feel your life wouldnt be complete without knowing more then you can read the full article here :

    http://support.microsoft.com/default...b;en-us;165967


    As for the performance aspect,yes you will certainly have a loss , however I doubt that this is barely discernable, depending on your hardware,config, usage etc.

    ---
    Anglo Saxon

  7. #7

    Thread Starter
    Addicted Member imbue's Avatar
    Join Date
    Aug 2002
    Location
    Midwest USA
    Posts
    155

    Smile

    Wow, Thanks for the insight. This is a topic that is a little hard to find information on.

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width