I am having the weirdest of errors.
Dim lines As List(Of NewOrderCsv) = (From record In _records Where record.Ticket2 = orderHeaderRow.Ticket2)
Their error is happening here on my declaration of the lines object
For Each line As NewOrderCsv In lines
CreateSopOrderLine(orderHeaderRow, newMcsOrder, newMcsOrder.AnalysisCode1, orderHeaderRow.String15, newMcsOrder.AnalysisCode1, newMcsOrder.ContactName, orderHeaderRow.String3, line)
Next
I am mostly a c# programer so forgive me if this is easy in vb.net
{"Unable to cast object of type
'WhereListIterator1[AveryIntegration.Common.CSV.Records.NewOrderCsv]'
to type
'System.Collections.Generic.List1[AveryIntegration.Common.CSV.Records.NewOrderCsv]'."}
The class for the NewOrderCSV is a file helpers
Namespace CSV.Records
<DelimitedRecord(",")>
Public Class NewOrderCsv
<FieldConverter(GetType(CustomStringConvertor))>
<FieldQuoted>
Public String2 As String
<FieldConverter(GetType(CustomStringConvertor))>
<FieldQuoted>
Public String3 As String
<FieldConverter(GetType(CustomStringConvertor))>
<FieldQuoted>
Public String4 As String
<FieldConverter(GetType(CustomStringConvertor))>
<FieldQuoted>
Public String5 As String
<FieldConverter(GetType(CustomStringConvertor))>
<FieldQuoted>
Public String10 As String
<FieldConverter(GetType(CustomStringConvertor))>
<FieldQuoted>
Public String15 As String
Public Decimal1 As Decimal
Public Decimal5 As Decimal
Public Decimal8 As Decimal
Public Decimal9 As Decimal
Public Decimal10 As Decimal
Public Date1 As DateTime
Public Ticket2 As Integer
<DebuggerNonUserCode>
Public Sub New()
MyBase.New()
End Sub
End Class
My method was returning a single item I replaced the line with when it should been cast as a list cant believe I didn't see that.
Dim lines As List(Of NewOrderCsv) = (From record In _records Where record.Ticket2 = orderHeaderRow.Ticket2).ToList()
Related
I have the dubious task of upgrading an old vb app which is using file helpers but for some reason, it appears to be ignoring the fact that I have marked it as a delimited record.
In the form it is using the following to attach the csv file to the record which is simlar to how c# does it.
Private Sub browseButton_Click(sender As System.Object, e As System.EventArgs) _
Handles browseButton.Click
Try
Dim file = GetFile()
'' Errors
Dim errors As New Dictionary(Of Integer, String)
If IO.File.Exists(file) Then
Dim engine As New FileHelperEngine(Of AveryOrderCsv)
_records = CType(engine.ReadFile(file), AveryOrderCsv()).ToList()
Dim count As Integer = 0
Dim success As Integer = 0
For Each averyOrderCsv As AveryOrderCsv In _records
Try
ImportProgressBar.Value = count
ImportProgressLabel.Text = String.Format("Importing {0} of {1} dockets",
count + 1, _records.Count())
System.Windows.Forms.Application.DoEvents()
If CreateSop(averyOrderCsv) Then
success = success + 1
End If
Catch ex As Exception
errors.Add(count, ex.Message)
End Try
count = count + 1
Next
End If
Catch ex As Exception
ExceptionManager.HandleUnexpectedException(ex)
End Try
The Class
Imports AveryIntegration.Common.CSV
Imports FileHelpers
Imports System
Imports System.Diagnostics
Namespace AveryIntegration.Common.CSV.Records
<DelimitedRecord(",")>
Public Class AveryOrderCsv
<FieldConverter(GetType(CustomStringConvertor))>
<FieldQuoted>
Public String2 As String
<FieldConverter(GetType(CustomStringConvertor))>
<FieldQuoted>
Public String3 As String
<FieldConverter(GetType(CustomStringConvertor))>
<FieldQuoted>
Public String4 As String
<FieldConverter(GetType(CustomStringConvertor))>
<FieldQuoted>
Public String5 As String
<FieldConverter(GetType(CustomStringConvertor))>
<FieldQuoted>
Public String10 As String
<FieldConverter(GetType(CustomStringConvertor))>
<FieldQuoted>
Public String15 As String
Public Decimal1 As Decimal
Public Decimal5 As Decimal
Public Decimal8 As Decimal
Public Decimal9 As Decimal
Public Decimal10 As Decimal
Public Date1 As DateTime
Public Ticket2 As Integer
<DebuggerNonUserCode>
Public Sub New()
MyBase.New()
End Sub
End Class
End Namespace
The error that I am getting is the following but as you see from above i have declared it as a delimited record and imported the imports FileHelpers
In VB the attribute must be on the same logical line.
<DelimitedRecord(",")> Public Class AveryOrderCsv
or
<DelimitedRecord(",")> _
Public Class AveryOrderCsv
Same thing for the properties.
The article Applying Attributes says:
In Visual Basic, the attribute is surrounded by angle brackets and must be on the same logical line; the line continuation character can be used if a line break is desired.
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
So here is part of my class structure:
Public Class CParticle
Public Feature As Double
Public AreaName As String
......
Public ElementsWT As SElements 'Elements in wt%
Public ElementsAT As SElements 'Elements in at%
Public Sub New()
ElementsWT = New SElements
ElementsAT = New SElements
End Sub
End Class
With this 'subclass':
Public Class SElements
Public B As Double
Public C As Double
Public N As Double
Public O As Double
Public F As Double
....
End Class
Now I want to access all variables within an instance of CParticle (e.g. called 'Particle') and also its instances of SElements by their name (String).
e.g.: "Feature" should give me access to Particle.Feature
Currently im doing it with reflection:
...
Dim myFieldInfo As FieldInfo
myFieldInfo = GetType(CParticle).GetField("Feature")
If Not myFieldInfo Is Nothing Then myFieldInfo.SetValue(Particle, value)
This works. But how can I access e.g. Particle.ElementsWT.B with the string "ElementsWT.B"? And is there an overall better way to do it besides using reflection?
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)?
I have the following class:
Public Class CodeList
Public Code As String
End Class
I am using Entity Framework.
I have the following table in a generated class:
Partial Public Class ContentTable
Public Property ID As Long
Public Property Type As Nullable(Of Integer)
Public Property Code As String
Public Property Active As Nullable(Of Boolean)
End Class
I am using this lambda expression to select distinct Code from ContentTable
Dim db As New ModelCodeEntities
Dim result = db.ContentTables.Select(Function(m As ContentTable) m.Code).Distinct().ToList
I want to convert the result of this lambda to a List(of CodeList)
Any help would be appreciated.
Dim result As List(Of CodeList)=db.ContentTables.Select(Function(item) item.Code).Distinct().Select(Function(code) new CodeList with{.Code=code}).ToList()