Use the string stored in a Variant in an Array - arrays

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

Related

VBA6 type mismatch on parameter array

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.

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"))

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()

How to perform a count using LINQ?

I am using SQL server and in my program I am currently using a count like this:
Function NumberOfApplications(inCandidateID As Integer) As Integer
Dim iCount As Integer = 0
Dim oApplication As DataModels.Application
For Each oApplication In Me.Applications
If oApplication.CandidateID = inCandidateID Then
iCount = iCount + 1
End If
Next
Return iCount
End Function
I have created my entities and bought in the correct Imports but I need to know how to perform a count using LINQ and VB. As you can see above I am using the Candidate ID to count. Any help would be appreciated. Thank you.
As #Pleun mentions, you can use the Count method, but the method itself has a predicate option, so you can use:
return Me.Applications.Count(app => app.CandidateID == inCandidateID);
In C#. I always have to think hard what that would look like in VB, this is what my brain tells me. The compiler migt disagree though ;):
Return Me.Applications.Count(Function(app) app.CandidateID = inCandidateID)
If you prefer the sql-like syntax, you could also do this:
Function NumberOfApplications(inCandidateID As Integer) As Integer
Return (From app As DataModels.Application In Me.Applications
Where app.CandidateID = inCandidateID).Count
End Function

VBA. Array for search or replace

Need to found any symbol of array.
For example:
replace(string,[a,b,c,e,f,g],"a1b2c3d4e567");
result = "1234567"
How do it ?
If your goal is to remove all non-numeric characters, the following will work:
' Added reference for Microsoft VBScript Regular Expressions 5.5
Const s As String = "a1b2c3d4e567"
Dim regex2 As New RegExp
Dim a As String
regex2.Global = True
regex2.Pattern = "[^0-9]"
Dim a As String = regex2.Replace(s, "")
MsgBox (a) ' Outputs 1234567
If you are looking for specific characters, change the pattern.
AFAIK you are going to have to do this by consecutive calls to replace
result = "a1b2c3d4e567"
result = replace(result,"a","")
result = replace(result,"b","")
result = replace(result,"c","")
etc

Resources