vb.net Formatting data structure input for web service - arrays

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

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

Deserialize a JSON array (I know - not array) in VB.net using JSON.NET

I've been killing myself for days on this and I can't figure it out. I'm by no means a programmer and this is my first attempt at working with JSON.
I have a GUI that I'm writing in VB.NET which gathers information for a script. I am using JSON.NET. I have it capturing the data entered into the GUI and exporting it as a JSON file, but I now need the functionality of reading the JSON file and putting the data back into the GUI.
I'm reading the contents of the datafile like this:
Dim Json As String = File.ReadAllText(fileDlg.FileName)
Dim data As JObject = JObject.Parse(Json)
Then going through each element of the file pulling the data in like this:
AD_DomainNameTB.Text = data.SelectToken("ActiveDirectory.DomainName")
AD_FQDNTB.Text = data.SelectToken("ActiveDirectory.FQDN")
AD_DomainControllerTB.Text = data.SelectToken("ActiveDirectory.DomainController")
AD_SVCUsernameTB.Text = data.SelectToken("ActiveDirectory.SVCUsername")
AD_SVCPasswordTB.Text = data.SelectToken("ActiveDirectory.SVCPassword")
AD_BaseDNTB.Text = data.SelectToken("ActiveDirectory.BaseDN")
Now, I need to cycle through an array of Sites and Site details and enter those into a datagridview on the GUI. I know I can do a for next loop for each of the items, but I don't know how to identify the number of items in the array.
The JSON data looks like this:
{
"ActiveDirectory": {
"DomainName": "CORP",
"FQDN": "corp.company.local",
"DomainController": "DC01",
"SVCUsername": "SVC_AD",
"SVCPassword": "SuperPass1",
"BaseDN": "OU=Active,DC=CORP,DC=Ccompany,DC=Local",
"CreateOUs": true,
"CreateGPOs": true,
"Sites": [
{
"Site": "Prod",
"HSA": "True",
"HSD": "True",
"HVD": null
},
{
"Site": "Test",
"HSA": "True",
"HSD": null,
"HVD": "True"
}
]
}
}
I've looked at the documentation on the site pretty extensively, but I can't find what I'm looking for. I've tried reading it into a dataset, like https://www.newtonsoft.com/json/help/html/DeserializeDataSet.htm, and I've tried creating a Class like Deserialize json array in vb.net but this doesn't make sense to me. It's way over my head.
Any help would be greatly appreciated. Please understand, that like I said, this is extremely new to me, so I need full details (don't assume that I know anything....because I don't!!)
(Using Newtonsoft.Json.Linq)
The ActiveDirectory.Sites node is of type JArray, which has a Count property.
You could iterate over the sites like this:
Dim Sites As JArray = data.SelectToken("ActiveDirectory.Sites")
For I = 0 To Sites.Count - 1
Dim Site = Sites(I)
' use `Site` and `I` in this loop body
Next
But if you don't care about the indexes, you should use a For Each loop:
Dim Sites As JArray = data.SelectToken("ActiveDirectory.Sites")
For Each Site In Sites
' use `Site` in this loop body
Next
JObject also lets you access properties using the following property lookup syntax:
Dim ActiveDirectory = data("ActiveDirectory")
Dim Sites = ActiveDirectory("Sites")
Dim SiteCount = Sites.Count
So another way you could write it is:
For Each Site In data("ActiveDirectory")("Sites")
' use `Site` in this loop body
Next
Based on the recommendations, I've gotten the code to work as I needed.Posting the full block below:
Dim Json As String = File.ReadAllText(fileDlg.FileName)
Dim data As JObject = JObject.Parse(Json)
Dim site
Dim SiteName
Dim HSA
Dim HSD
Dim HVD
AD_DomainNameTB.Text = data.SelectToken("ActiveDirectory.DomainName")
AD_FQDNTB.Text = data.SelectToken("ActiveDirectory.FQDN")
AD_DomainControllerTB.Text = data.SelectToken("ActiveDirectory.DomainController")
AD_SVCUsernameTB.Text = data.SelectToken("ActiveDirectory.SVCUsername")
AD_SVCPasswordTB.Text = data.SelectToken("ActiveDirectory.SVBPassword")
AD_BaseDNTB.Text = data.SelectToken("ActiveDirectory.BaseDN")
AD_CreateOUsCB.Checked = data.SelectToken("ActiveDirectory.CreateOUs")
AD_CreateGPOCB.Checked = data.SelectToken("ActiveDirectory.CreateGPOs")
Dim Sites As JArray = data.SelectToken("ActiveDirectory.Sites")
For Each site In Sites
SiteName = site.item("Site").ToString()
HSA = site.item("HSA").ToString()
HSD = site.item("HSD").ToString()
HVD = site.item("HVD").ToString()
If HSA = "" Then
HSA = False
End If
If HSD = "" Then
HSD = False
End If
If HVD = "" Then
HVD = False
End If
AD_SitesDatagrid.Rows.Add({SiteName, HSA, HSD, HVD})
Next
Now, there might be a better, more efficient way of going through the data in the array, but this worked.

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

VBA - Naming of variables from array

Good Morning.
Is possible naming of variables with composition of text and another variables ?
Only for an example (not functional):
Components = "AA,BB,CC"
Values = "101,102,103"
ComponentsArr = Split(Components)
ValuesArr = Split(Values)
For i = 1 To UBound(ComponentsArr)
Let "Var" & ComponentsArr(i) = ValuesArr(i)
Next i
You might be able to use a dictionary to accomplish your purpose. You add key/value pairs to the dictionary. Then you can use the key to get the value.
Set a reference to MS Scripting runtime ('Microsoft Scripting Runtime')
Components = "AA,BB,CC"
Values = "101,102,103"
ComponentsArr = Split(Components, ",")
ValuesArr = Split(Values, ",")
Dim dict As New Scripting.Dictionary
For i = LBound(ComponentsArr) To UBound(ComponentsArr)
Call dict.Add(ComponentsArr(i), ValuesArr(i))
Next i
If dict.Exists("AA") Then
Debug.Print dict.Item("AA") 'prints 101
End If
See also: https://stackoverflow.com/a/915333/2559297
It might be easier to do it like the following:
Components = Array("AA", "BB", "CC")
Values = Array("101", "102", "103")
Then you wouldn't need ComponentsArr and ValuesArr.
For i = LBound(Components) To UBound(Components)
dict.Add(Components(i), Values(i))
Next i

reference dictionary within array output to listbox

Title kind of states my problem.
I'm using an Api (For Vertical Response, an email list manager) which works fine. But for a particular method returns an Array where the information I need to reference is within a Dictionary inside that Array.
Array[list_id, list_name, list_type, member_data] <- member_data being the dictionary housing all my goodies.
Best I've managed to get is the listbox outputting "com.verticalresponse.api.NVPair[]" for each member.
Code
Protected Sub GetBounces()
Dim listID As Integer = 284662333
Dim isMember As Boolean = False
Dim newSession As New loginArgs()
newSession.username = username
' Your VerticalResponse username
newSession.password = password
newSession.session_duration_minutes = "120"
Dim VRUser As New VRAPI()
Try
sessionID = VRUser.login(newSession)
Dim GetMembers As New getListMembersArgs()
Try
GetMembers.session_id = sessionID
GetMembers.list_id = listID
GetMembers.max_records = 8
Try
Dim listmembers As Array = VRUser.getListMembers(GetMembers)
lstBounces.DataSource = listmembers
lstBounces.DataValueField = ("member_data").ToString()
lstBounces.DataBind()
Catch ex As Exception
lstBounces.Text = "One: " + ex.ToString()
End Try
Catch listex As Exception
lstBounces.Text = "Two: " + listex.ToString()
End Try
Catch ex As System.Exception
lstBounces.Text = "Three: " + ex.ToString()
End Try
End Sub
Edit
I have taken the suggestion of Keith Mifsud and added a breakpoint just before the Databind. It shows me that "listmembers" has eight indices (currently only testing by searching for 8 (total list will be close to 8000, of which about 1900 are needed.))
Each of those 8 indices contains the 4 columns I am looking at, so when I use listmembers(3) as the datasource I'm really only searching the fourth result, not the member_data column...
Is there a correct way to reference the column of results?
Something like:
lstbounces.DataSource = listmembers( ,3)
But instead of that, a correct one?
I think you should set the datasource as the dictionary not the array holding it
Try
Dim listmembers As Array = VRUser.getListMembers(GetMembers)
lstBounces.DataSource = listmembers(3)
'As the datasource is a dictionary, I don't think you have to set up the display and value fields
'lstBounces.DataValueField = ("member_data").ToString()
lstBounces.DataBind()
Catch ex As Exception
lstBounces.Text = "One: " + ex.ToString()
End Try
UPDATE
Based on what the collection type (dictionary within every element of an array) I would recommend you use the .net DataView with allows for expandable rows. There are some very good tutorials which can guide you to create your view

Resources