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()
Related
(revamped question)
I need to format an input data structure for an RPG web service. I've tried using a list and it does not work. When I try this code:
Dim clist As New List(Of LabelView.PASSBACK.dscustomers)
clist.Add(New LabelView.PASSBACK.dscustomers() With {.BALANCEDUE = 185.42, .CREATIONDATE = 20200101, .CUSTOMERID = 1, .CUSTOMERTYPE = "ACTIVE", .FIRSTNAME = "Pat", .LASTNAME = "Smith"})
clist.Add(New LabelView.PASSBACK.dscustomers() With {.BALANCEDUE = 185.42, .CREATIONDATE = 20200101, .CUSTOMERID = 2, .CUSTOMERTYPE = "ACTIVE", .FIRSTNAME = "Jordan", .LASTNAME = "Jones"})
input.DSCUSTOMERS = clist
// ^^ Intellisense error:
// Value of type 'List (Of dsCustomers)' cannot be converted to 'dscustomers()'.
I can't figure out how to programmatically build something compatible. When I try this:
Dim client As PASSBACK.PASSBACK = New PASSBACK.PASSBACK()
Dim input As PASSBACK.passbackInput = New PASSBACK.passbackInput
input.DSCUSTOMERS(0).FIRSTNAME = "Hello"
It compiles but when I run I get 'Object reference not set to an instance of an object.'
Your issue with this line:
input.DSCUSTOMERS = clist
Is because DCUSTOMERS is an array (the "dcustomers()" of your error message) and a List(Of dcustomer) can't be directly assigned to an array.
There is a conversion function on List(Of T) that will give you an array T() called ToArray. It requires only a small change to your code:
input.DSCUSTOMERS = clist.ToArray()
This will make a new array with the same contents as the List (if dcustomers is a Class then the contents of the array will be exactly the same objects, whereas if it's a Structure then they will be copies) and assign it to DCUSTOMERS.
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"))
I'm trying to create an array of strings. In the watches window, it always says that the value of my array items are "", instead of the values I typed.
I tried to find answers online but none of them fixed my problem. Is there anything wrong with my code?
Thanks!
Dim fish18(0 To 2) As String
fish18(0) = "$I$5"
fish18(1) = "$I$9"
fish18(2) = "$I$10"
Check your values in the Locals window.
In VBE: View > Locals Window > Expand on Array (fish18)
Dim fish18(2) As String
fish18(0) = "$I$5"
fish18(1) = "$I$9"
fish18(2) = "$I$10"
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
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