Retrive Multiple Data Firebase in VB.Net - vb.net

How to retrieve multiple data from firebase. I only know how to retrieve one data.
Here is my sample code.
Friend Class Employee_No_Fingerprint
Public Property code As Integer
Public Property last_name As String
Public Property first_name As String
Public Property middle_name As String
Public Property suffix As String
Public Property department_name As String
Public Property picture As String
Public Property fingerprint As String
Public Property upload As Integer
End Class
Private Sub GetAllEmployee()
Dim datEmployeeNoFingerprint As New Employee_No_Fingerprint()
Dim response As FirebaseResponse = clientFingerprint.Get("Employee_No_Fingerprint")
datEmployeeNoFingerprint = response.ResultAs(Of Employee_No_Fingerprint)()
End Sub

Private Sub GetAllEmployee()
Dim load As FirebaseResponse = client.Get("Employee_No_Fingerprint")
Dim data As Dictionary(Of String, POJO_Export) = JsonConvert.DeserializeObject(Of Dictionary(Of String, POJO_Export))(load.Body.ToString())
End Sub

Related

How to access object in multidimensional array in Visual Basic

In Visual Basic, I am using a List to store an array of objects. However, I haven't figured out how to access a particular object once I have added it to my list.
I am using Microsoft Visual Studio
Private Results As New List(Of Object)
Dim Results As Object()
Dim CaseStatus as String = "Closed"
...
Results.Add(New Object() {CaseStatus, FlagsStr, OBTotal, OBPaid})
MessageBox.Show(Result(0).ToString)
Displays: System.Object[]
I have also tried MessageBox.Show(Results(0,0).ToString), but don't get the expected results.
When you can .ToString on an object you get the fully qualified name of the object unless the class overrides .ToString. Creating a class and a List (Of T) gives you easy access to any of the properties of the class.
Public Class ProjectCase
Public Property CaseStatus As String
Public Property FlagsString As String
Public Property OBTotal As Decimal
Public Property OBPaid As Decimal
Public Sub New()
'include a default constructor
End Sub
Public Sub New(cs As String, fs As String, total As Decimal, paid As Decimal)
CaseStatus = cs
FlagsString = fs
OBTotal = total
OBPaid = paid
End Sub
Public Overrides Function ToString() As String
Return $"{CaseStatus}, {FlagsString}, Total = {OBTotal}, Paid = {OBPaid}"
End Function
End Class
Private Results As New List(Of ProjectCase)
Private Sub OPCode()
Results.Add(New ProjectCase("Closed", "Some string", 74.36D, 22D))
MessageBox.Show(Results(0).ToString)
MessageBox.Show(Results(0).CaseStatus) 'or any property of your class
End Sub

Class with property that is a List Of items in a subclass

I want a class for a Customer, with a text property name.
Another property CustAddress will be a list of multiple addresses.
Each address will have two string properties.
Here is what I have.
I am not sure if I need something in the constructor of the class address.
And I'm even not sure what the code would look like to exploit this class.
Also, I can't get the F11 Step Into debug feature to step into the class code. If i put a break in the class code it does break and works fine. I have modified the option "Just My Code" to remove checkbox, but it does not help. I have a solution containing one class module and one Windows App together.
<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)>
Public Class ComClass1
#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "c8e723b4-f229-4368-9737-97c4c71d490a"
Public Const InterfaceId As String = "16275ddb-5cfe-47c0-995f-84a5f868ad1b"
Public Const EventsId As String = "dad73a5c-8ac4-4384-a5f9-8e2c388b5514"
#End Region
' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
'Fields
Private _name As String
Public _CustAddress As List(Of address)
'Constructor for class ComClass
Public Sub New()
_CustAddress = New List(Of address)
End Sub
Public Property CustName() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property
Public Property CustAddress() As List(Of address)
Get
Return _CustAddress
End Get
Set(value As List(Of address))
_CustAddress = value
End Set
End Property
Public Class address
Private _address1 As String
Private _address2 As String
Public Sub New()
'??????
End Sub
Public Property Address1 As String
Get
Return _address1
End Get
Set(value As String)
_address1 = value
End Set
End Property
Public Property Address2 As String
Get
Return _address2
End Get
Set(value As String)
_address2 = value
End Set
End Property
End Class
End Class
I took out the com stuff just to shorten the answer. Since you had no extra code in the Property Procedures I shortened that to automatic Properties. I also moved the address Class out on its own. This class could be useful else where in the program so a nested class is not really necessary.
Public Class ComClass1
Public Property CustName As String
Public Property CustAddress As List(Of address)
Public Sub New(cName As String, cAddresses As List(Of address))
CustName = cName
CustAddress = cAddresses
End Sub
End Class
Public Class address
Public Property Address1 As String 'Street Address
Public Property Address2 As String 'City and State
Public Sub New(a1 As String, a2 As String)
Address1 = a1
Address2 = a2
End Sub
End Class
Private Sub DeclareAComClass1()
Dim addrList As New List(Of address) From {
New address("12 Main Street", "Los Angeles, CA"),
New address("13 Park Avenue", "New York, NY")
}
Dim cc As New ComClass1("Big Company, Inc.", addrList)
End Sub
Here is what I have ended up with. #Mary got me further ahead. But because I am using a COM class, I can't have any public constructors with parameters.
I added a method called AddAddress which gives me the functionality I need.
In my original post I somehow left out MyBase.New which is required for a COM class.
I encourage comments with insights on this approach.
<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)>
Public Class ComClass1
#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "c8e723b4-f229-4368-9737-97c4c71d490a"
Public Const InterfaceId As String = "16275ddb-5cfe-47c0-995f-84a5f868ad1b"
Public Const EventsId As String = "dad73a5c-8ac4-4384-a5f9-8e2c388b5514"
#End Region
' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
'Fields
Private _name As String
Private _CustAddress As List(Of address)
'Constructor for class ComClass
Public Sub New()
MyBase.New
_CustAddress = New List(Of address)
End Sub
Public Sub AddAddress(a1 As String, a2 As String)
Dim addr As New address(a1, a2)
_CustAddress.Add(addr)
End Sub
Public Property CustName() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property
Public Property CustAddress() As List(Of address)
Get
Return _CustAddress
End Get
Set(value As List(Of address))
_CustAddress = value
End Set
End Property
Public Class address
Private _address1 As String
Private _address2 As String
Public Sub New(a1 As String, a2 As String)
_address1 = a1
_address2 = a2
End Sub
Public Property Address1 As String
Get
Return _address1
End Get
Set(value As String)
_address1 = value
End Set
End Property
Public Property Address2 As String
Get
Return _address2
End Get
Set(value As String)
_address2 = value
End Set
End Property
End Class
End Class
And the code to implement/test is as follows:
Dim TestClass As New ComClass1
Dim myint As Int32
TestClass.CustName = "John Smith"
TestClass.AddAddress("123 Main Street", "Los Angeles")
TestClass.AddAddress("13 Park Avenue", "New York")
Debug.Print(TestClass.CustAddress(0).Address1) '123 Main Stree'
Debug.Print(TestClass.CustAddress(1).Address1) '13 Park Avenue
TestClass.CustAddress.Remove(TestClass.CustAddress(0))
Debug.Print(TestClass.CustAddress(0).Address1) ' 13 Park Avenue

Deserialize to class

I have a class Person which I can serialize with the following code, but I can’t figure out how to deserialize the file back to the class.
I would be grateful for help on this. Thank you.
Imports Newtonsoft.Json
Imports Windows.Storage
Imports Windows.Storage.Streams
Public Class Person
Public Property Name As String
Public Property Age As Integer
Public Property Gender As String
End Class
Public NotInheritable Class MainPage
Inherits Page
Private p As Person
Private pList As New List(Of Person)
Private Async Sub Save()
Dim jsonContents As String = JsonConvert.SerializeObject(pList)
Dim localFolder As StorageFolder = ApplicationData.Current.LocalFolder
Dim textFile As StorageFile = Await localFolder.CreateFileAsync("a.txt", CreationCollisionOption.ReplaceExisting)
Using textStream As IRandomAccessStream = Await textFile.OpenAsync(FileAccessMode.ReadWrite)
Using textWriter As New DataWriter(textStream)
textWriter.WriteString(jsonContents)
Await textWriter.StoreAsync()
End Using
End Using
End Sub
End Class
I tried the following but it doesn’t work.
Private Async Sub GetData()
Dim localFolder As StorageFolder = ApplicationData.Current.LocalFolder
Dim textFile = Await localFolder.GetFileAsync("a.txt")
Dim readFile = Await FileIO.ReadTextAsync(textFile)
Dim obj As RootObject = JsonConvert.DeserializeObject(Of RootObject)(readFile)
End Sub
Public Class RootObject
'Public Property pList1() As List(Of Person)
Public Property Name() As String
Public Property Age() As Integer
Public Property Gender() As String
End Class
You should make sure your VB class object's property in accordance with the JSON key or JSON name.
For example using your sample JSON data in your comment:
Since your JSON data is not complete, I modify it as the following:
{"pList1":[{"Name":"Henrik","Age":54,"Gender":"Mand"},{"Name":"Lone","Age":50,"Gender":"Kvinde"},{"Name":"Niels","Age":24,"Gender":"Mand"},{"Name":"Pernille","Age":26,"Gender":"Kvinde"}]}
You can keep the above Json data in a file named my.txt, if you want to deserialize the Json data to VB object, your VB objects classes should be as the following two classes:
Public Class Person
Public Property Name As String
Public Property Age As Integer
Public Property Gender As String
End Class
Public Class RootObject
Public Property pList1() As List(Of Person)
End Class
Please pay attention to that: the pList1 property of RootObject class is corresponding to the pList1 key or name in the JSON data.
Then you should be able to use the JsonConvert class to deserialize to RootObject.
Private Async Sub GetData()
Dim localFolder As StorageFolder = ApplicationData.Current.LocalFolder
Dim textFile = Await localFolder.GetFileAsync("my.txt")
Dim readFile = Await FileIO.ReadTextAsync(textFile)
Dim obj As RootObject = JsonConvert.DeserializeObject(Of RootObject)(readFile)
End Sub

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

VB.NET CType: How do I use CType to change an object variable "obj" to my custom class that I reference using a string variable like obj.GetType.Name?

The code below works for the class that I hard coded "XCCustomers" in my RetrieveIDandName method where I use CType. However, I would like to be able to pass in various classes and property names to get the integer and string LIST returned. For example, in my code below, I would like to also pass in "XCEmployees" to my RetrieveIDandName method. I feel so close... I was hoping someone knew how to use CType where I can pass in the class name as a string variable.
Note, all the other examples I have seen and tried fail because we are using Option Strict On which disallows late binding. That is why I need to use CType.
I also studied the "Activator.CreateInstance" code examples to try to get the class reference instance by string name but I was unable to get CType to work with that.
When I use obj.GetType.Name or obj.GetType.FullName in place of the "XCCustomers" in CType(obj, XCCustomers)(i)
I get the error "Type 'obj.GetType.Name' is not defined" or "Type 'obj.GetType.FullName' is not defined"
Thanks for your help.
Rick
'+++++++++++++++++++++++++++++++
Imports DataLaasXC.Business
Imports DataLaasXC.Utilities
Public Class ucCustomerList
'Here is the calling method:
Public Sub CallingSub()
Dim customerList As New XCCustomers()
Dim customerIdAndName As New List(Of XCCustomer) = RetrieveIDandName(customerList, "CustomerId", " CustomerName")
'This code below fails because I had to hard code “XCCustomer” in the “Dim item...” section of my RetrieveEmployeesIDandName method.
Dim employeeList As New XCEmployees()
Dim employeeIdAndName As New List(Of XCEmployee) = RetrieveIDandName(employeeList, "EmployeeId", " EmployeeName")
'doing stuff here...
End Sub
'Here is the method where I would like to use the class name string when I use CType:
Private Function RetrieveIDandName(ByVal obj As Object, ByVal idPropName As String, ByVal namePropName As String) As List(Of IntStringPair)
Dim selectedItems As List(Of IntStringPair) = New List(Of IntStringPair)
Dim fullyQualifiedClassName As String = obj.GetType.FullName
Dim count As Integer = CInt(obj.GetType().GetProperty("Count").GetValue(obj, Nothing))
If (count > 0) Then
For i As Integer = 0 To count - 1
'Rather than hard coding “XCCustomer” below, I want to use something like “obj.GetType.Name”???
Dim Item As IntStringPair = New IntStringPair(CInt(CType(obj, XCCustomers)(i).GetType().GetProperty("CustomerId").GetValue(CType(obj, XCCustomers)(i), Nothing)), _
CStr(CType(obj, XCCustomers)(i).GetType().GetProperty("CustomerName").GetValue(CType(obj, XCCustomers)(i), Nothing)))
selectedItems.Add(Item)
Next
End If
Return selectedItems
End Function
End Class
'+++++++++++++++++++++++++++++++
' Below are the supporting classes if you need to see what else is happening:
Namespace DataLaasXC.Utilities
Public Class IntStringPair
Public Sub New(ByVal _Key As Integer, ByVal _Value As String)
Value = _Value
Key = _Key
End Sub
Public Property Value As String
Public Property Key As Integer
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCCustomer
Public Property CustomerId As Integer
Public Property CustomerName As String
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCCustomers
Inherits List(Of XCCustomer)
Public Sub New()
PopulateCustomersFromDatabase()
End Sub
Public Sub New(ByVal GetEmpty As Boolean)
End Sub
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCEmployee
Public Property EmployeeId As Integer
Public Property EmployeeName As String
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCEmployees
Inherits List(Of XCEmployee)
Public Sub New()
PopulateEmployeesFromDatabase()
End Sub
Public Sub New(ByVal GetEmpty As Boolean)
End Sub
End Class
End Namespace
From MSDN
CType(expression, typename)
. . .
typename : Any expression that is legal
within an As clause in a Dim
statement, that is, the name of any
data type, object, structure, class,
or interface.
This is basically saying you can't use CType dynamically, just statically. i.e. At the point where the code is compiled the compiler needs to know what typename is going to be.
You can't change this at runtime.
Hope this helps.
Since List(Of T) implements the non-generic IList interface, you could change your function declaration to:
Private Function RetrieveIDandName(ByVal obj As System.Collections.IList, ByVal idPropName As String, ByVal namePropName As String) As List(Of IntStringPair)
And then your troublesome line would become (with also using the property name parameters):
Dim Item As IntStringPair = New IntStringPair(CInt(obj(i).GetType().GetProperty(idPropName).GetValue(obj(i), Nothing)), _
CStr(obj(i).GetType().GetProperty(namePropName).GetValue(obj(i), Nothing)))
Of course, you could still have the first parameter by Object, and then attempt to cast to IList, but that's up to you.
ctype is used to convert in object type.