VBA6 type mismatch on parameter array - arrays

The following (highly simplified) code produces a type mismatch: array expected on the call to evalexp for parameter xabc. Why?
Public nfa() As String
Public rules() As String
Sub BuildNFA(xabc () As String)
Dim x
ReDim xnfa(5, 10)
x = evalexp(0, xabc)
End Sub
Function evalexp(rnb As Integer, xyz() As String) As String
xyz(rnb, replnode).name = "replnode"
evalregexp = ""
End Function

Xyz is declared as () but used as(,). I.e you declare a 1d array then try to use it as a 2d array. Additionally xyz is declared as string,but you try to access a name property which will not exist. Please install the Rubberduck VBA addin and learn to use its code inspections.

Related

How to filter String Array in VB.Net?

What is VB.Net code to filter a String Array ?
I use following code
Imports System.Reflection
Dim ass As Assembly = Assembly.GetExecutingAssembly()
Dim resourceName() As String = ass.GetManifestResourceNames()
that return a String array
How can I filter resourceName() variable ?
I tried following lines of code
Dim sNameList() As String
= resourceName.FindAll(Function(x As String) x.EndsWith("JavaScript.js"))
but compiler return following error
BC36625: Lambda expression cannot be converted to 'T()' because 'T()' is not a delegate type
How can I correct this error ?
Is there another solution to solve my problem ?
Dim sNameList = resourceName.Where(Function(s) s.EndsWith("JavaScript.js"))
In that case, sNameList is an IEnumerable(Of String), which is all you need if you intend to use a For Each loop over it. If you genuinely need an array:
Dim sNameList = resourceName.Where(Function(s) s.EndsWith("JavaScript.js")).ToArray()
The reason that your existing code didn't work is that Array.FindAll is Shared and so you call it on the Array class, not an array instance:
Dim sNameList = Array.FindAll(resourceName, Function(s) s.EndsWith("JavaScript.js"))

Weird data type mismatch error VBA [duplicate]

I need to pass a String array to set a class property using its setter method. Array was declared in Module and it's perfectly set the values using Redim Preserve. When calling the Class setter method it gives error. If I continued to without setting array data type it worked. But I need to declare the data type as String.
I received a compile error "Type mismatch: array or user defined type expected."
Module
Dim sPageDetails() As String
' set some values to array
' Declare class instance
dim x as new X
with x
.SetPageNumberDetails(sPageDetails) ' assume SetPageNumberDetails is the setter method in class X
end with
End module
'class module start X
Private pageDetails() as String
' Setter for pageDetails array
Public Sub SetPageNumberDetails(ByRef sPageDetails() As String)
pageDetails= sPageDetails
End Sub
' Getter for pageDetails array
Public Function GetPageNumberDetails() As String()
GetPageNumberDetails= pageDetails
End Function
.SetPageNumberDetails(sPageDetails)
Remove the parentheses:
.SetPageNumberDetails sPageDetails
Remain the parentheses and add explicit 'Call':
Call .SetPageNumberDetails(sPageDetails)
It took 5-10 CPU ticks (~30 nsec), but code wil be more readable, editable, auditable etc. And it is simply comme il faut...
.

Converting a returned array from an async function to a regular array

It's been a while since I've written absolutely any code at all so I'm a little rusty. I've recently started writing a bot and am currently stuck on converting an array within an async function to a regular array to be parsed within another function.
Private Async Function getmsgs(ByVal num As Integer) As Task(Of Array)
Dim msgids As Array = Nothing
Try
Dim msg = Await Libx.DownloadMessages(num)
'starting loop to pull each message and assign to array
For i As Integer = msg.Count - 1 To 0 Step -1
If (msg(i).Id > 0) Then
msgids(i) = msg(i).Id
End If
Next
'returning array
Return msgids
Catch ex As Exception
Return msgids
End Try
End Function
Private Sub deletemsgs(ByVal ids As Array)
If Not ids(0) = Nothing Then
'incase library failed to retrieve messages
Try
For i As Integer = ids.Length - 1 To 0 Step -1
'function called per item in array
libx.DeleteMessages(ids(i))
Next
Catch ex As Exception
End Try
End If
End Sub
The issue occurs when trying to call deletemsgs(getmsgs(argint)).
I'm getting the error message
Value of type 'Task(Of Array)' cannot be converted to 'Array'
I've searched through a few forums with every relating search term I can think of. Any help would be greatly appreciated, cheers in advance.
You need to await async functions.
An example being:
Dim arr As Array() = await getmsgs(1)
This will need to be inside of an async function for it to work correctly.

Use the string stored in a Variant in an Array

I'm trying to use the values inside of a Variant.
An example of the code:
Dim Holder as Variant
Holder = "1,1,1,1,1"
Later I will be using this Variant inside of an Array().
The Declaration is like this:
.TextFileColumnDataTypes = Array(Holder)
There was an error right after this
Run-Time Error '5':
Invalid Procedure call or argument*
Is there a way to insert the Variant's values into Array(<Here?).
Holder's value is not constant, it will change depending on a function I created.
A way to accomplish this is as suggested in comments, using the split
If you are getting an error I am guessing that you are not using it correctly.
This is how the code should look.
dim i as long
dim var
Holder = "1,1,1,1,1"
var = Split(Holder,",")
for i = 0 to 4
.TextFileColumnDataTypes = var(i)
next i

VB 6.0 unable to create array

I am not able to create an array in VB 6.0 and I'm going crazy trying to figure it out. First of all VB is not my native language, I am a PHP programmer so arrays are not a new concept for me... Hopefully someone can help with this.
This is my function:
Function get_plant() As String
Dim arrPlant(1 To 10) As String
arrPlant(1) = "PL1"
arrPlant(2) = "PL2"
arrPlant(3) = "PL3"
arrPlant(4) = "PL4"
arrPlant(5) = "PL5"
arrPlant(6) = "PL6"
arrPlant(7) = "PL7"
arrPlant(8) = "PL8"
arrPlant(9) = "PL9"
arrPlant(10) = "PL0"
get_plant = arrPlant
End Function
Then I tried calling it with this (and about 10 other variations...):
Dim plant_code() As String
plant_code = get_plant()
MsgBox plant_code(1)
When I try and use the array I get this:
Question mark in the array index
What the heck am I missing here?
The return type of the function you have given is string not string() and you are trying to return string array. Try giving this
Function get_plant() As String()

Resources