Assigning values to an object and its nested object - vb.net

I am a novice and am missing something simple. I have two Classes
Public Class Param
Public Property temperature As String
Public Property display As Boolean
Public Property storage As Boolean
Public Property reason As Boolean
Public Property stats As Object
Public Property errors As Object
End Class
Public Class getTemperature
Public Property method As String
Public Property params As Param()
Public Property id As String
End Class
I want to declare and assign values to the objects but I keep getting the error "Object reference not set to an instance of an object" when trying to assign values to items within param. I don't understand, I have created both the object GetTemperature and the object Params, what am I missing?
Dim GetTemp As New getTemperature
GetTemp.method = TextBoxMethod.Text
GetTemp.id = TextBoxID.Text
Dim params As New Param
params.temperature = "true"
'GetTemp.params(0) = params
I have also tried, but get the same response:
Dim GetTemp As New getTemperature
GetTemp.method = TextBoxMethod.Text
GetTemp.id = TextBoxID.Text
GetTemp.params(0).temperature = "True"

Try this:
Public Class Param
Public Property temperature As String
Public Property display As Boolean
Public Property storage As Boolean
Public Property reason As Boolean
Public Property stats As Object
Public Property errors As Object
End Class
Public Class getTemperature
Public Property method As String
Public Property params As List(Of Param)
Public Property id As String
Public Sub New()
params = New List(Of Param)
End Sub
End Class
So you could write something like:
Dim a As New getTemperature
a.params.Add(New Param)

It will work if you remove the parenthesis
Public Class getTemperature
Public Property method As String
Public Property params As Param
Public Property id As String
End Class
and then
Dim GetTemp As New getTemperature
GetTemp.method = TextBoxMethod.Text
GetTemp.id = TextBoxID.Text
Dim params As New Param
params.temperature = "true"
GetTemp.params = params

Related

Unable to cast object of type 'SubmitPO.VARCOMMERCE.PO.LineRequest[]' to type 'System.String[] using AddObject in RestSharp

When I try to create a RestSharp Request by adding an object I get this error: (It happens on request.AddObject(po))
Unable to cast object of type 'SubmitPO.VARCOMMERCE.PO.LineRequest[]' to type 'System.String[]
My code is like such:
'''
Public Function submitPO(ByRef po As OrderRequestJson, ByVal test As Boolean) As IRestResponse
'get bearer token
Dim client = New RestClient("https://api.ingrammicro.com:443/sandbox/resellers/v6/orders")
Dim request As RestRequest = New RestRequest() With {
.Method = Method.POST
}
request.AddHeader("Content-Type", "application/x-www-form-urlencoded")
request.AddHeader("authorization", "Bearer " + Me.BearerAccessToken)
request.AddHeader("Accept", "application/json")
request.AddHeader("IM-CustomerNumber", Me.AccountNumber)
request.AddHeader("IM-CountryCode", "US")
request.AddHeader("IM-CorrelationID", Guid.NewGuid.ToString())
request.AddObject(po)
Dim response = client.Execute(request)
End Function
'''
And the model is as such
'''
#Region "Request"
Public Class OrderRequestJson
Public Property customerOrderNumber As String
Public Property endCustomerOrderNumber As String
Public Property notes As String
Public Property acceptBackOrder As Boolean
Public Property shipToInfo As Shiptoinfo
Public Property lines As LineRequest()
Public Property shipmentDetails As ShipmentDetails
Public Property additionalAttributes As Additionalattribute()
End Class
Public Class Shiptoinfo
Public Property contact As String
Public Property companyName As String
Public Property addressLine1 As String
Public Property addressLine2 As String
Public Property city As String
Public Property state As String
Public Property postalCode As String
Public Property countryCode As String
Public Property phoneNumber As String
Public Property email As String
End Class
Public Class LineRequest
Public Property customerLineNumber As String
Public Property ingramPartNumber As String
Public Property quantity As Integer
End Class
Public Class Additionalattribute
Public Property attributeName As String
Public Property attributeValue As String
End Class
Public Class ShipmentDetails
Public Property carrierCode As String
Public Property freightAccountNumber As String
Public Property shipComplete As String
Public Property requestedDeliveryDate As String
Public Property signatureRequired As Boolean
Public Property shippingInstructions As String
End Class
#End Region
'''

How to parse this JSON string properly

I have some API That returns the following JASON, and im trying to parse it to be able to pull from any attributes i want.
JSON im trying to parse: https://pastebin.com/i06GRD0f
I'm assuming the proper way would be to load everything into an array but i can't get it to work right as it's a little confusing with all different parameters.
Imports System.Web.Script.Serialization
Public Class ServerAttributes
Public Property assists() As String
Public Property bestRankPoint() As String
Public Property boosts() As String
Public Property dBNOs() As String
Public Property dailyKills() As String
Public Property dailyWins() As String
Public Property damageDealt() As String
Public Property days() As String
Public Property headshotKills() As String
Public Property heals() As String
Public Property killPoints() As String
Public Property kills() As String
Public Property longestKill() As String
Public Property longestTimeSurvived() As String
Public Property losses() As String
Public Property maxKillStreaks() As String
Public Property mostSurvivalTime() As String
Public Property rankPoints() As String
Public Property rankPointsTitle() As String
Public Property revives() As String
Public Property rideDistance() As String
Public Property roadKills() As String
Public Property roundMostKills() As String
Public Property roundsPlayed() As String
Public Property suicides() As String
Public Property swimDistance() As String
Public Property teamKills() As String
Public Property timeSurvived() As String
Public Property top10s() As String
Public Property vehicleDestroys() As String
Public Property walkDistance() As String
Public Property weaponsAcquired() As String
Public Property weeklyKills() As String
Public Property weeklyWins() As String
Public Property winPoints() As String
Public Property wins() As String
End Class
Public Class ServerData
Public Property data() As ServerAttributes
End Class
Module StaterMainClass
Public Sub GetInfo()
Dim jss As New JavaScriptSerializer()
Dim DataResponse As ServerData = jss.Deserialize(Of ServerData)(JSON)
Debug.Print(DataResponse.data.assists(0))
End Sub
End Module

LINQ Query returning Ienumerable

I have the following 2 classes
Public Class LookupsModel
Implements IEnumerable(Of LookupModel)
Public _LookupModel() As LookupModel
Public Sub New(pArray As ArrayList)
_LookupModel = New LookupModel(pArray.Count - 1) {}
Dim i As Integer
For Each l As LookupModel In pArray
_LookupModel(i) = l
i += 1
Next
End Sub
Public Function GetEnumerator() As IEnumerator(Of LookupModel) Implements IEnumerable(Of LookupModel).GetEnumerator
Return New LookupEmum(_LookupModel)
End Function
Public Function GetEnumerator1() As IEnumerator Implements IEnumerable.GetEnumerator
Return New LookupEmum(_LookupModel)
End Function
Public Property Heading1 As String
Public Property Heading2 As String
Public Property Heading3 As String
Public Property Heading4 As String
Public Property Heading5 As String
Public Property Heading6 As String
Public Property CalledBy As String
Public Property ParmName1 As String
Public Property ParmName2 As String
Public Property ParmName3 As String
Public Property ParmName4 As String
Public Property ParmName5 As String
Public Property ParmName6 As String
Public Property ValueFieldGetter() As Func(Of LookupModel, String)
Public Property DescriptionFieldGetter() As Func(Of LookupModel, String)
End Class
Public Class LookupModel
Public Sub New(ByVal Key As String, Optional ByVal Desc As String = Nothing, Optional Extra_1 As String = Nothing, Optional Extra_2 As String = Nothing, Optional Extra_3 As String = Nothing, Optional Extra_4 As String = Nothing)
Me.Field1 = Key
Me.Field2 = Desc
Me.Field3 = Extra_1
Me.Field4 = Extra_2
Me.Field5 = Extra_3
Me.Field6 = Extra_4
End Sub
Public Sub New()
Me.Field1 = Nothing
Me.Field2 = Nothing
Me.Field3 = Nothing
Me.Field4 = Nothing
Me.Field5 = Nothing
Me.Field6 = Nothing
End Sub
Public Property Field1 As String
Public Property Field2 As String
Public Property Field3 As String
Public Property Field4 As String
Public Property Field5 As String
Public Property Field6 As String
End Class
This is the linq query:
Dim lm As LookupsModel = GetLookupsModel(FieldID, lookup, LookupPage:=1, SearchField:=SearchField, SearchFields:=searchFields, SearchString:=String.Empty)
Dim lm2 As IEnumerable(Of LookupModel) = lm.Where(Function(p) p.Field1.Contains("A"))
I'm trying to query LookupsModel and only get the LookupModel collection where LookupModel.Field1 starts with "A". But the following query returns an Ienumerable of LookupModel, not the LookupsModel object with those items excluded. How do we accomplish this and get a LookupsModel object as a result?
You can't directly cast from an IEnumerable(Of LookpupModel) to a LookupsModel. You have a few options:
Add a constructor that takes an existing IEnumerable(Of LookupModel)
Convert the output of Where to an ArrayList and use your existing constructor.
But what's the point of LookupsModel in the first place? All you're doing is wrapping an array of LookupModel and not adding any additional functionality. Why not just stick with IEnumerable(Of LookupModel)?

DropDownListFor using List of Class

I cant seem to get my HTML.DropDownListFor() to work properly with my structures.
CategoriesEditViewModel
Public Class CategoriesEditViewModel
Public Property Categories As List(Of cihCategoryOrgDef)
Public Property Category As cihCategoryOrgDef
Public Property lists As cihLists = New cihLists()
Public Property SelectedItem As String
Public Sub New(catId as Guid)
SelectedItem = codId.ToString
lists.loadClubWaivers(ZenCommon.CurrentOrgId)
End Sub
End Class
cihLists
Private orgWaiverList As List(of cihWaiver)
Public Sub loadClubWaivers(orgId as Guid)
'Go to database and populate orgWaiverList
End Sub
Public ReadOnly Property organizationClubWaivers() As List(Of cihWaiver)
Get
Return orgWaiverList
End Get
End Property
cihWaiver
Public Class cihWaiver
Public waiverId As Guid = Guid.Empty
Public waiverName As String = ""
Public waiverText As String = ""
End Class
Edit View Page
#Html.DropDownListFor(Function(m) m.SelectedItem, New SelectList(Model.lists.organizationClubWaivers, "waiverId", "waiverText", Model.lists.organizationClubWaivers))
The error i get is 'Library.cihWaiver' does not contain a property with the name 'waiverId'.
but the cihWaiver class clearly has an item for 'waiverId'. I haven't done MVC stuff in a while, so maybe i'm going about this all wrong.
The answer was as simple as changing my cihWaiver Class to this:
Public Class cihWaiver
Public Property waiverId As Guid = Guid.Empty
Public Property waiverName As String = ""
Public waiverText As String = ""
End Class
cane someone explain to me why the keyword Property is so important?

How to iterate through every property in an every Object in my list of Objects vb

So I have a list of Objects and I want to get every property in every object and save it as a string.
Object looks like
Public Class parameters
Public Property test As String
Public Property test_type As String
Public Property user_test_name As String
Public Property meas As String
Public Property spec_min As String
Public Property spec_max As String
Public Property spec_unit As String
Public Overrides Function ToString() As String
Return user_test_name
End Function
End Class
This is what i have so far
For Each parameters In param
Dim myType As Type = GetType(parameters)
Dim properties As System.Reflection.PropertyInfo() = myType.GetProperties()
For Each p As System.Reflection.PropertyInfo In properties
MsgBox(p.Name)
Next
Next