I am trying to declare type Product and type Productlist
can anyone tell me if this is right way to do it ?
Public Class Product
Public Property name As String
Public Property price As Double
End Class
Public Class ProductsList
Public Property items() As New List(Of Product)
End Class
I mean can I just write
Public Class Product
Public Property name As String
Public Property price As Double
End Class
Public property ProductsList as new List(Of Product)
instead ?
The first approach seems like better practice. You would code with it like this:
Dim p as new Product()
p.name = "Apple"
p.price = 1
Dim pList as new ProductList()
pList.Items.Add(p)
The issue with the second approach is you would have a wrapper class, eg DemoWrapper:
Public Class DemoWrapper
Public Class Product
Public Property name As String
Public Property price As Double
End Class
Public ProductsList as new List(Of Product)
End Class
Your code would end up like this:
Dim p as new DemoWrapper.Product()
Related
I have: ClassUsser, ClassClient, ClassPerson.
ClassUsser
Public Property Name as String
ClassClient
Public Property Name as String
ClassPerson
Public Property KindPerson as Integer
Public Property Person as (ClassClient or ClassPerson)
I want delare Property Person as diferent type of class based on value of KindPerson.
Enum KindsPerson
{ User=1,
Client=2}
First, I would set value of KindPerson, and after, through set/get methods, obtain the Person
I don't Know If It si posible in VB.Net. It exists any way to do this?
Thanks
You could use the Factory Pattern:
Module Module1
Sub Main()
Dim p1 = Person.GetPerson(Person.PersonKind_ENUM.Client, "Adam")
Dim p2 = Person.GetPerson(Person.PersonKind_ENUM.User, "Barbara")
Dim personList As New List(Of Person)
personList.Add(p1)
personList.Add(p2)
For Each p In personList
Console.WriteLine($"{p.GetType.Name}: {p.name}")
Next
Console.ReadKey()
End Sub
Public Class User
Inherits Person
End Class
Public Class Client
Inherits Person
End Class
Public MustInherit Class Person
Property Name As String
Public Enum PersonKind_ENUM
User
Client
End Enum
Public Shared Function GetPerson(personKind As PersonKind_ENUM, name As String) As Person
Select Case personKind
Case PersonKind_ENUM.Client
Return New Client() With {.Name = name}
Case PersonKind_ENUM.User
Return New User() With {.Name = name}
Case Else
Throw New InvalidOperationException
End Select
End Function
End Class
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?
my question is how serializer the next string Json:
data:
{
"Customers":[{"**PhoneNumber**":"123123123"},{"**PhoneNumber**":"321321321"}],
"Expand":false,
"Include":false
}
I to have doubt, because in the string JSon I have 2 equal labels, therefore i not know as create my class with the same labels, i'm using VB.net.
My Classes in VB.net
Public Class Customers
Public as customer1
Public expand as boolean
Public include as boolean
End Class
Public Class customers1
Public PhoneNumber as String
End Class
Having the same field duplicated in this instance is okay because it is in a collection/array. In this JSON example you have two Customer objects for the Response's Customers collection.
Your object model would look like this:
Public Class Response
Public Property Customers As List(Of Customer)
Public Property Expand As Boolean
Public Property Include As Boolean
End Class
Public Class Customer
Public Property PhoneNumber As String
End Class
An example of deserializing it:
Dim objSerializer As New JavaScriptSerializer()
Dim objResponse As Response = objSerializer.Deserialize(Of Response)(strData)
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?
I have two simple viewmodels which are related. I can query the data via an linq include statement, but when i inject it into the viewmodel the icollection is nothing?
Viewmodels:
Public Class EventViewModel
Public Property EVENTID As Integer
Public Property TITLE As String
Public Overridable Property USERS() As ICollection(Of UserViewmodel)
End Class
Public Class UserViewModel
Public Property USERID As Integer
Public Property EVENTID As Integer
Public Property NAME As String
End Class
Query:
Dim dataObject As EVENTTABLE = db.EVENTTABLE.Include("USERTABLE").Single(Function(c) c.EVENTID= "1")
Users are in the Object!
Injection:
viewModel.InjectFrom(dataObject)
Users are Nothing?
Ok figured it out with help from this question: How to map lists with ValueInjector
I needed to query the USERTABLE into a list and inject it like stated below:
Query:
Dim dataSubObject = (From ....).toList()
Injection:
Dim viewModel = New EventViewModel
viewModel.USERS= dataSubObject.Select(Function(x) New ImageViewModel().InjectFrom(x)).Cast(Of ImageViewModel)().ToList()