In vb.net, defining a dictionary type in a class - vb.net

New to using a Dictionary list.
I have an api that is returning a "task object" defined as:
id As Integer
options As Dictionary(string, string)
So I am defining a class called Task to store the returned "task object".
Public Class Task
Private _id As Integer
Public Property Id() As Integer
Get
Return _id
End Get
Set(value As Integer
_id = value
End Set
End Property
Private _optionlist As New List(Of Option)
Public Property OptionList() As List(Of Option)
Get
Return _optionlist
End Get
Set(ByVal value As List(Of Option))
_optionlist = value
End Set
End Property
End Class
Public Class Option
Private _key As String
Public Property Key() As String
Get
Return _key
End Get
Set(value As String)
_key = value
End Set
End Property
Private _value As String
Public Property Value() As String
Get
Return _value
End Get
Set(value As String)
_value = value
End Set
End Property
End Class
I will assign the "task object" returned from the api to this Task class.
I defined options as a list. Is that correct?
I will also have to read the option in my class and later repopulate it to send back to the api via function call.
Could use some help...thanks.

You can add the property to the class directly:
Public Class Task
Private _id As Integer
Private options As Dictionary(of string, string)
You can also create Get and Set methods for the "options" parameter.

Related

How to use the following model class (Framework 4.7) in a Framework 2.0?

I have the following model class used in my .NET Framework 4.7.2 project which get and set data from an API.
I have to use the same model class in a .NET 2.0 Project which will have to get and set the same object get from a HTTP request (not API)
But if i try just to copy paste it i get the error which says that i need a setter on it.
Here is how my model looks like:
Public Class PDT
Public Class Documento
Public Property testata As Testata
Public Property corpo() As IEnumerable(Of Corpo)
End Class
Public Class Testata
Public Property id As Integer
Public Property cod As String
Public Property tipo As String
Public Property cod_fornitore As String
Public Property desc_fornitore As String
Public Property data As String
Public Property num_pv As String
Public Property desc_pv As String
Public Property num_pv_destinazione As String
Public Property desc_pv_destinazione As String
Public Property num_ordine As String
Public Property inviato As Boolean
End Class
Public Class Corpo
Public Property barcode As String
Public Property desc As String
Public Property um As String
Public Property qta As Single
Public Property id_testata As Integer
Public Property tipo_frontalino As String
Public Property timestamp As Long
End Class
End Class
PS I've tried setting getter and setters like this for each property:
Public Property testata() As Testata
Get
End Get
Set(ByVal value As Testata)
End Set
End Property
But by passing data in that object returns always Nothing.
UPDATE my class with getters and setters:
Public Class PDT
Public Class Documento
Private _testata As Testata = Nothing
Public Property testata As Testata
Get
Return _testata
End Get
Set(ByVal value As Testata)
_testata = value
End Set
End Property
Private _corpo As IEnumerable(Of Corpo) = Nothing
Public Property corpo As IEnumerable(Of Corpo)
Get
Return _corpo
End Get
Set(ByVal value As IEnumerable(Of Corpo))
_corpo = value
End Set
End Property
End Class
Public Class Testata
Private _id As Integer = Nothing
Public Property id As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property
Private _cod As String = Nothing
Public Property cod As String
Get
Return _cod
End Get
Set(ByVal value As String)
_cod = value
End Set
End Property
Private _tipo As String = Nothing
Public Property tipo As String
Get
Return _tipo
End Get
Set(ByVal value As String)
_tipo = value
End Set
End Property
Private _cod_fornitore As String = Nothing
Public Property cod_fornitore As String
Get
Return _cod_fornitore
End Get
Set(ByVal value As String)
_cod_fornitore = value
End Set
End Property
Private _desc_fornitore As String = Nothing
Public Property desc_fornitore As String
Get
Return _desc_fornitore
End Get
Set(ByVal value As String)
_desc_fornitore = value
End Set
End Property
Private _data As String = Nothing
Public Property data As String
Get
Return _data
End Get
Set(ByVal value As String)
_data = value
End Set
End Property
Private _num_pv As String = Nothing
Public Property num_pv As String
Get
Return _num_pv
End Get
Set(ByVal value As String)
_num_pv = value
End Set
End Property
Private _desc_pv As String = Nothing
Public Property desc_pv As String
Get
Return _desc_pv
End Get
Set(ByVal value As String)
_desc_pv = value
End Set
End Property
Private _num_pv_destinazione As String = Nothing
Public Property num_pv_destinazione As String
Get
Return _num_pv_destinazione
End Get
Set(ByVal value As String)
_num_pv_destinazione = value
End Set
End Property
Private _desc_pv_destinazione As String = Nothing
Public Property desc_pv_destinazione As String
Get
Return _desc_pv_destinazione
End Get
Set(ByVal value As String)
_desc_pv_destinazione = value
End Set
End Property
Private _num_ordine As String = Nothing
Public Property num_ordine As String
Get
Return _num_ordine
End Get
Set(ByVal value As String)
_num_ordine = value
End Set
End Property
Private _inviato As Boolean = Nothing
Public Property inviato As Boolean
Get
Return _inviato
End Get
Set(ByVal value As Boolean)
_inviato = value
End Set
End Property
End Class
Public Class Corpo
Private _barcode As String = Nothing
Public Property barcode As String
Get
Return _barcode
End Get
Set(ByVal value As String)
_barcode = value
End Set
End Property
Private _desc As String = Nothing
Public Property desc As String
Get
Return _desc
End Get
Set(ByVal value As String)
_desc = value
End Set
End Property
Private _um As String = Nothing
Public Property um As String
Get
Return _um
End Get
Set(ByVal value As String)
_um = value
End Set
End Property
Private _qta As Single = Nothing
Public Property qta As Single
Get
Return _qta
End Get
Set(ByVal value As Single)
_qta = value
End Set
End Property
Private _id_testata As Integer = Nothing
Public Property id_testata As Integer
Get
Return _id_testata
End Get
Set(ByVal value As Integer)
_id_testata = value
End Set
End Property
Private _tipo_frontalino As String = Nothing
Public Property tipo_frontalino As String
Get
Return _tipo_frontalino
End Get
Set(ByVal value As String)
_tipo_frontalino = value
End Set
End Property
Private _timestamp As Long = Nothing
Public Property timestamp As Long
Get
Return _timestamp
End Get
Set(ByVal value As Long)
_timestamp = value
End Set
End Property
End Class
End Class
I'm pretty sure VB.NET didn't have auto-properties back then, so you will need to change your code to use the classic properties with a backing field. For example:
' Backing field
Private _cod As String = "Empty"
' Classic property getter and setter
Property cod As String
Get
Return _cod
End Get
Set(ByVal value As String)
_cod = value
End Set
End Property

How To Include Header Information in ToJSON Output

Hi there I have a requirement to provide a JSON array for delivery to one of our clients. The JSON is based on the following 'Model' components:
Public Class PIValues
Inherits List(Of PIValue)
Public Sub New()
End Sub
End Class
Public Class PIValue
Private _PointName As String
Private _value As Double
Public Property PointName() As String
Get
PointName = _PointName
End Get
Set(value As String)
_PointName = value
End Set
End Property
Public Property Value() As Double
Get
Value = _value
End Get
Set(value As Double)
_value = value
End Set
End Property
End Class
Its a breeze to generate the required JSON output however I also need to provide some header information as part of the package in the form:
TimeStamp: <yyyyMMdd hh:mm:ss>
Where the contents within the chevrons are a variable time stamp of the specified format.
How can I insert this information into the JSON package?
Kind Regards
Paul.

How to get all json values with newtonsoft

I'm having a hard time getting through all the JSON values ​​of a string, can anyone help me?
I'm only getting a single value, where am I going wrong?
My codes
Dim address As String = "http://wsloterias.azurewebsites.net/api/sorteio/getresultado/1"
Dim client As WebClient = New WebClient()
Dim reader As StreamReader = New StreamReader(client.OpenRead(address))
Dim json = (reader.ReadToEnd)
Dim objs As RootObject = JsonConvert.DeserializeObject(Of RootObject)(json)
Dim objsSorteio As Sorteio = JsonConvert.DeserializeObject(Of Sorteio)(json)
For Each nums In objsSorteio.Premios
MsgBox(nums.ToString)
Next
Classes
Public Class Premio
Public Property Faixa() As String
Get
Return m_Faixa
End Get
Set(value As String)
m_Faixa = Value
End Set
End Property
Private m_Faixa As String
Public Property NumeroGanhadores() As Integer
Get
Return m_NumeroGanhadores
End Get
Set(value As Integer)
m_NumeroGanhadores = Value
End Set
End Property
Private m_NumeroGanhadores As Integer
Public Property Valor() As Double
Get
Return m_Valor
End Get
Set(value As Double)
m_Valor = Value
End Set
End Property
Private m_Valor As Double
End Class
Public Class Sorteio
Public Property NumSorteio() As Integer
Get
Return m_NumSorteio
End Get
Set(value As Integer)
m_NumSorteio = Value
End Set
End Property
Private m_NumSorteio As Integer
Public Property Numeros() As List(Of Integer)
Get
Return m_Numeros
End Get
Set(value As List(Of Integer))
m_Numeros = Value
End Set
End Property
Private m_Numeros As List(Of Integer)
Public Property Premios() As List(Of Premio)
Get
Return m_Premios
End Get
Set(value As List(Of Premio))
m_Premios = Value
End Set
End Property
Private m_Premios As List(Of Premio)
Public Property Ganhadores() As List(Of Object)
Get
Return m_Ganhadores
End Get
Set(value As List(Of Object))
m_Ganhadores = Value
End Set
End Property
Private m_Ganhadores As List(Of Object)
End Class
Public Class RootObject
Public Property NumeroConcurso() As Integer
Get
Return m_NumeroConcurso
End Get
Set(value As Integer)
m_NumeroConcurso = Value
End Set
End Property
Private m_NumeroConcurso As Integer
Public Property Acumulou() As Boolean
Get
Return m_Acumulou
End Get
Set(value As Boolean)
m_Acumulou = Value
End Set
End Property
Private m_Acumulou As Boolean
Public Property EstimativaPremio() As Double
Get
Return m_EstimativaPremio
End Get
Set(value As Double)
m_EstimativaPremio = Value
End Set
End Property
Private m_EstimativaPremio As Double
Public Property ValorAcumulado() As Double
Get
Return m_ValorAcumulado
End Get
Set(value As Double)
m_ValorAcumulado = Value
End Set
End Property
Private m_ValorAcumulado As Double
Public Property Data() As String
Get
Return m_Data
End Get
Set(value As String)
m_Data = Value
End Set
End Property
Private m_Data As String
Public Property RealizadoEm() As String
Get
Return m_RealizadoEm
End Get
Set(value As String)
m_RealizadoEm = Value
End Set
End Property
Private m_RealizadoEm As String
Public Property DescricaoAcumuladoOutro() As String
Get
Return m_DescricaoAcumuladoOutro
End Get
Set(value As String)
m_DescricaoAcumuladoOutro = Value
End Set
End Property
Private m_DescricaoAcumuladoOutro As String
Public Property ValorAcumuladoOutro() As Double
Get
Return m_ValorAcumuladoOutro
End Get
Set(value As Double)
m_ValorAcumuladoOutro = Value
End Set
End Property
Private m_ValorAcumuladoOutro As Double
Public Property DataProximo() As String
Get
Return m_DataProximo
End Get
Set(value As String)
m_DataProximo = Value
End Set
End Property
Private m_DataProximo As String
Public Property ValorAcumuladoEspecial() As Double
Get
Return m_ValorAcumuladoEspecial
End Get
Set(value As Double)
m_ValorAcumuladoEspecial = Value
End Set
End Property
Private m_ValorAcumuladoEspecial As Double
Public Property Arrecadacao() As Double
Get
Return m_Arrecadacao
End Get
Set(value As Double)
m_Arrecadacao = Value
End Set
End Property
Private m_Arrecadacao As Double
Public Property Sorteios() As List(Of Sorteio)
Get
Return m_Sorteios
End Get
Set(value As List(Of Sorteio))
m_Sorteios = Value
End Set
End Property
Private m_Sorteios As List(Of Sorteio)
End Class
I can not go through all the "Numeros" and also "Premios"
You are over complicating this unnecessarily.
using the URL provided the exposed json data was copied and plugged into a utility site like http://jsonutils.com/. It generated the models with auto properties. Note that it converted collections to array as apposed to Lists like you have in your classes.
Public Class Premio
Public Property Faixa As String
Public Property NumeroGanhadores As Integer
Public Property Valor As Double
End Class
Public Class Sorteio
Public Property NumSorteio As Integer
Public Property Numeros As Integer()
Public Property Premios As Premio()
Public Property Ganhadores As Object()
End Class
Public Class RootObject
Public Property NumeroConcurso As Integer
Public Property Acumulou As Boolean
Public Property EstimativaPremio As Double
Public Property ValorAcumulado As Double
Public Property Data As String
Public Property RealizadoEm As String
Public Property DescricaoAcumuladoOutro As String
Public Property ValorAcumuladoOutro As Double
Public Property DataProximo As String
Public Property ValorAcumuladoEspecial As Double
Public Property Arrecadacao As Double
Public Property Sorteios As Sorteio()
End Class
Basically the same as what you had originally with better readability. Feel free to convert tha arrays back to list if that is your preference.
The first desrialization is already giving you the necessary object. Drill into the properties in order to access the values you need.
'''other code removed for brevity
Dim objs As RootObject = JsonConvert.DeserializeObject(Of RootObject)(json)
Dim objsSorteioList As List(Of Sorteio) = objs.Sorteios.ToList()
For Each objsSorteio In objsSorteioList
For Each prems In objsSorteio.Premios
MsgBox(prems.ToString)
Next
For Each nums In objsSorteio.Numeros
MsgBox(nums.ToString)
Next
Next

Combine two tables into one entity framework? (SQLite)

I am using the EntityFramework.SQLite library and for the life of me can not figure out how to combine two tables together into a temporary table for display purposes in xaml code.
Here's the code for my two tables I want to combine which is about all I am using right now besides a class for my temp table called CategoryList (this is part of the business/data logic dll library):
Partial Public Class CategoryList
Public Sub New()
Me.CategoryInfo = New CategoryReference
'Me.CategoryCode = New HashSet(Of CategoryCodes)
Me.CategoryCodes = New CategoryCodes
End Sub
Public Property MyId As Integer
<Key, ForeignKey("CodeID")>
<Required>
Public Property CodeID As Integer
<Key, ForeignKey("CategoryID")>
Public CategoryID As Integer
'Public Property CategoryCode As ICollection(Of CategoryCodes)
Public Property CategoryInfo As CategoryReference
' Public Property CategoryInfo As ICollection(Of CategoryReference)
Public Property CategoryCodes As CategoryCodes
End Class
<Table("CategoryCodes")>
Public Class CategoryCodes 'category shortnames/codes
<MaxLength(100)>
<Required>
Public Property CategoryCode As String
Get
Return _CategoryCode
End Get
Set(value As String)
_CategoryCode = value
End Set
End Property
Private _CategoryCode As String
'<NotNull>
' <PrimaryKey>
'<Unique(Name:="UQ__CategoryCodes__0000000000000081_CategoryCodes", Order:=0)>
<Key>
<Required>
Public Property CodeID As Integer
Get
Return CategoryCode
End Get
Set(value As Integer)
_CodeID = value
End Set
End Property
Private _CodeID As Integer
End Class
<Table("CategoryReference")>
Partial Public Class CategoryReference 'table design for category data
<MaxLength(100)>
Public Property CategoryName As String
Get
Return _CategoryName
End Get
Set(value As String)
_CategoryName = value
End Set
End Property
Private _CategoryName As String
<MaxLength(100)>
Public Property CategoryDescription As String
Get
Return _CategoryDescription
End Get
Set(value As String)
_CategoryDescription = value
End Set
End Property
Private _CategoryDescription As String
'<Unique(Name:="UQ__CatagoryReference__000000000000005F_CatagoryReference", Order:=0)>
<ForeignKey("CodeID")>
<Required>
Public Property CodeID As Integer
Get
Return _CodeID
End Get
Set(value As Integer)
_CodeID = value
End Set
End Property
Private _CodeID As Integer
'<Unique(Name:="UQ__CatagoryReference__000000000000005A_CatagoryReference", Order:=0)>
<Key>
<Required>
Public Property CategoryID As Integer
Get
Return _CategoryID
End Get
Set(value As Integer)
_CategoryID = value
End Set
End Property
Private _CategoryID As Integer
End Class
It may seem long but the tables are very simple and the get/set blocks make it look long (A vb.net editor can turn them into simple property's if she/he wishes). I may be using the CategoryList class wrong but here's where I use it in my xaml datasource in my main application code (I have business logic/data processing in a dll library):
Private Property ViewModel As List(Of UIELLUWP.DataAccess.CategoryList)
Dim categories As New UIELLUWP.DataAccess.SQLiteDb
ViewModel = categories.Categories.ToList
Errors received with current code:
I receive an error that Table "CategoryList" does not exist when I run the above code.
my solution I found out to this question was to add asnotracking because tracking was taking place.

OData made from objects

Lets say I have a class called "Item" and "SubItems"
Public Class SubItem
Private _SubItemName As String
Public Property SubItemName() As String
Get
Return _SubItemName
End Get
Set(ByVal value As String)
_SubItemName = value
End Set
End Property
End Class
<DataServiceKey("Name")> _
Public Class Item
Private _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Private _SubItems As List(Of SubItem)
Public Property SubItems() As List(Of SubItem)
Get
Return _SubItems
End Get
Set(ByVal value As List(Of SubItem))
_SubItems = value
End Set
End Property
End Class
How would I create a service that would return a list of Items and upon looking up an individual item I would be able to see the sub items.
You can check out WCF Data Servies reflection provider, e.g.,
http://blogs.msdn.com/b/alexj/archive/2010/06/11/tip-56-writing-an-odata-service-using-the-reflection-provider.aspx
http://msdn.microsoft.com/en-us/library/dd728281.aspx
Hope this helps.