Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

passing an array to a vb function

Posted on 2003-03-28
13
Medium Priority
?
252 Views
Last Modified: 2010-05-01
I have a vb function that I want to pass an array to.  The array can be an array of Int or Booleans.  I defined the function like this:

Public Function PutAllValues1D(hKey As Long, strPath As String, keyValue As String, values() As Variant)

Then I tried to call it like this:

Call PutAllValues1D(HKEY_CURRENT_USER, "\StatusGridConfiguration\", "ArrVisible", ReturnArrFieldsShown)

where ReturnArrFieldsShown is like this:

Public Function ReturnArrFieldsShown() As Boolean()
    ReturnArrFieldsShown = m_ArrFieldsShown
End Function

m_ArrFieldsShown is a Boolean array of 11 Boolean values.


I'm still learning VB as I go but I was under the impression that using variant would be similar to using a void* in c++ in that it would take values of any type?

The error that I am getting is this:
---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Type mismatch: array or user-defined type expected
---------------------------
OK   Help  
---------------------------
0
Comment
Question by:emily_hall
13 Comments
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 750 total points
ID: 8226769
pass the values 'array' as a 'simple' variant, and then, in the Code  treat it as the ARRAY that you 'know' it is.

Public Function PutAllValues1D(hKey As Long, strPath As String, keyValue As String, values As Variant)

now, you assign the ARRAY to the Variant output parameter of the Function, and use Values, in the calling code, as an ARRAY;



Call PutAllValues1D(HKEY_CURRENT_USER, "\StatusGridConfiguration\", "ArrVisible", ReturnArrFieldsShown)

Debug.print ReturnArrFieldsShown(1) and so on...

since a VARIANT type can hold 'anything', even a reference to an array.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8226786
in the other part of the code:

Public Function ReturnArrFieldsShown() As Boolean()
   ReturnArrFieldsShown = m_ArrFieldsShown
End Function
 

change it to:

Public Function ReturnArrFieldsShown As Variant
   ReturnArrFieldsShown = m_ArrFieldsShown
End Function
0
 
LVL 1

Author Comment

by:emily_hall
ID: 8226813
I've tried changing it to As Variant instead of As Boolean() and it didn't work, neither did As Variant().  I will try the suggestion with taking the () out of the called function.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:emily_hall
ID: 8227130
The Variant thing works in that now I am not getting that error anymore, but my function is still doing nothing.  I am trying to copy some values into that array that i pass in yet it doesn't happen.

0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8227322
could you post some more complete code?
0
 
LVL 1

Author Comment

by:emily_hall
ID: 8227361
What more would you like to see?  

PutAllValues1D is just a for loop and some string parsing:

    Dim i As Integer
    Dim j As Integer
    Dim textString As String
     
    ' All values are saved in one comma-delimited
    ' string. Note that this means the values
    ' cannot contain a comma.
    ' Get the values and split them into an array.
    textString = getstring(hKey, strPath, keyValue)
   
    If (textString = "" Or IsNull(textString) Or IsEmpty(textString)) Then
        Exit Function
    End If
       
    textString = sGetToken(textString, ",")
    For i = LBound(values, 1) To UBound(values, 1)
        For j = LBound(values, 2) To UBound(values, 2)
            values(i, j) = CInt(sGetToken("", ","))
        Next j
    Next i



values is the array that is passed in ByRef...  It gets updated fine but nothing changes in the array that is passed in.
0
 
LVL 1

Author Comment

by:emily_hall
ID: 8227367
Sorry - the CInt is the int function - the boolean one just leaves the value as a string
0
 

Expert Comment

by:WayneHRichmond
ID: 8227959
Try using the Join function to convert the array to a string then pass it to the function.  Then use the split function on the other side to make it back into an array.

You can find the syntax for both of these functions in the VB help files.

I have a text document here that explains the use...
------------------------------------------------------
These functions allow you to convert single dimensional arrays into strings and then back to arrays.
The Join function takes an array and, optionally, a delimiter as arguments and returns a string containing each element of the array with the specified delimiter between them. For instance, the code below results in this string: 1|2|3|4|5.
Dim vntTest(0 To 4) As Variant

vntTest(0) = 1
vntTest(1) = 2
vntTest(2) = 3
vntTest(3) = 4
vntTest(4) = 5
Call MsgBox(Join(vntTest, "|"))
The Split function takes a string and, optionally, a delimiter, the number of substrings to return, and the compare method as arguments. It returns a zero-based array with each substring as an element. The code below shows how to take the delimited string produced above and convert it back to an array.
Dim strTest As String
Dim vntSplit As Variant

strTest = "1|2|3|4|5"
vntSplit = Split(strTest, "|")
MsgBox vntSplit(0)
The Join and Split functions can be very useful for converting the contents of an array into strings and back into arrays. For instance, use these functions to save column widths from a grid to the registry and then restore the column widths. Using these functions makes it easier to save and work with array content.
0
 
LVL 1

Author Comment

by:emily_hall
ID: 8227990
I don't have a problem converting the array to string and back - the problem I have is with getting the function to return the array once it has been setup.  If I look in debug, the array is made up properly but then when I go back after the function call and look at the array that I passed in, it has no values.
0
 

Expert Comment

by:SunShines
ID: 8229874
Hi Emily,

Why dont u use Collections instead?? It is much more prgrammer friendly.. Try hands on Collections..

-SunShines
0
 
LVL 6

Expert Comment

by:VK
ID: 8229971
hello, simply

Public Function PutAllValues1D(hKey As Long, strPath As String, keyValue As String, values As Variant)

instead of

Public Function PutAllValues1D(hKey As Long, strPath As String, keyValue As String, values() As Variant)
0
 
LVL 1

Author Comment

by:emily_hall
ID: 8287330
The problem where I was not getting anything returned from the functions turned out to be a problem with private member variables.  If I copy the variables and use the copies instead of the actual member variables then it works fine.  Like I said, I am new to VB...

Anyway - I will give the points to Arthur_Wood because that suggestion got the errors out of my code so that I could actually run it and step in and figure out the other bit.

0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8288696
thank you and good luck in the future.  You are learning.

AW
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

578 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