Creating a property as a list or array with properties - vb.net

I would like to create a array or list property with theses results:
team(1).name = "Falcons"
team(1).totalPoints = 167
team(2).name = "Jets"
team(2).totalPoints = 121
and so on....
I know how to make properties, but not as an array or list. Thanks.

There is no sub-properties in .net, but you can achieve your target by creating a List of objects of a class that having properties. try the following:
Public Class Team
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 _TotalPoints As Integer
Public Property TotalPoints() As Integer
Get
Return _TotalPoints
End Get
Set(ByVal value As Integer)
_TotalPoints = value
End Set
End Property
End Class
Then you can create a list of objects of the class Team as follows:
Dim TeamList As New List(Of Team)
TeamList.Add(New Team() With {.Name = "Falcons", .TotalPoints = 167})
TeamList.Add(New Team() With {.Name = "Jets", .TotalPoints = 121})
So that ;
TeamList(0).Name Gives "Falcons"
TeamList(0).TotalPoints Gives 167
TeamList(1).Name Gives "Jets"
TeamList(1).TotalPoints Gives 121

Related

Predefined class properties if need expand or shrink quantity of them on each app start

Public Class Products
Private zCriteria As String
Public Property Criteria As String
Get
Return zCriteria
End Get
Set(value As String)
zCriteria = value
End Set
End Property
Private zProductList As New List(Of Product)
Public Property ProductList As List(Of Product)
Get
Return zProductList
End Get
Set(value As List(Of Product))
zProductList = value
End Set
End Property
End Class
Public Class Product
Private zCriteriaList As List(Of Criterias)
Public Property CriteriaList As List(Of Criterias)
Get
Return zCriteriaList
End Get
Set(value As List(Of Criterias))
zCriteriaList = value
End Set
End Property
End Class
Public Class Criterias
Private zCrPropName As String
Public Property CrPropName As String
Get
Return zCrPropName
End Get
Set(value As String)
zCrPropName = value
End Set
End Property
Private zCritCode As String
Public Property CritCode As String
Get
Return zCritCode
End Get
Set(value As String)
zCritCode = value
End Set
End Property
Private zcrPropValue As String
Public Property crPropValue As String
Get
Return zcrPropValue
End Get
Set(value As String)
zcrPropValue = value
End Set
End Property
End Class
For Each oProducts As Products In oAssigment.ProductsList
For Each oproduct As Product In oProducts.ProductList
For Each cr As Criterias In oproduct.CriteriaList
cr.CrPropName = "Product Name" 'some object property name
cr.CritCode = "PN"
cr.crPropValue = "" ' Value of property "Product Name"
Next
Next
Next
It is all made to have different properties of some object depending on options set in text file. It is only a sample o usage.
conditions:
Criterias on applications start is same for all objects (=Product) i want to read.
Every time on start, application read options file where is defined few property names and codes (values i want get from objects). So every run can be initiated different quantity of properties to read. It means i can not have hard-coded names of properties.
Someone can advice me how to predefine "CrPropName" and "CritCode" on app start so many times how much property names defined in options and after that populate it so many times as how many objects exist from which i read these property values.
p.s. I am not very professional at coding and sorry for my language

"Expression of type "" is not queryable" error

I'm attempting to get an assignment done for class and have hit this error:
Error 1 Expression of type 'Homework_Code_Examples.Form1.State' is not queryable. Make sure you are not missing an assembly reference and/or namespace import for the LINQ provider. D:\Users\Stryke\Documents\Visual Studio 2013\Projects\Homework Code Examples\Homework Code Examples\Form1.vb 11 32 Homework Code Examples
I've never encountered this before and searching via Google, MSDN and these forums all tell me that I must target the correct .NET framework for my program, however I am completely lost as to which framework I should target as the recommended answers all give the same error upon being recompiled. Currently, I am using .NET Framework 4.5 in Visual Studio 2013.
I don't know if it helps at all or not, but here is my code so that you may see what it is I'm trying to achieve...
Public Class Form1
Dim states As State
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
states = New State()
Dim query = From st In states
Let name = states.Name
Let density = states.Density()
Order By density Descending
Select name, density
End Sub
Private Sub stateInfo_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles stateInfo.CellContentClick
End Sub
Class State
Private m_name, m_abbr, m_date As String
Private m_area, m_pop As Integer
Public Property Name()
Get
Return m_name
End Get
Set(value)
m_name = value
End Set
End Property
Public Property Abbreviation()
Get
Return m_abbr
End Get
Set(value)
m_abbr = value
End Set
End Property
Public Property joinDate()
Get
Return m_date
End Get
Set(value)
m_date = value
End Set
End Property
Public Property landArea
Get
Return m_area
End Get
Set(value)
m_area = value
End Set
End Property
Public Property Population
Get
Return m_pop
End Get
Set(value)
m_pop = value
End Set
End Property
Public Function Density()
Dim popDensity As Integer
popDensity = Population / landArea
Return popDensity
End Function
End Class
End Class
Linq needs to iterate through multiple objects. In your case, it can be list or collection of State class.
Example with List(Of Type)
Change line
Dim states As State
to
Dim states As List(Of State)
and
states = New State()
into
states = New List(Of State)
Example with array
Change line
Dim states As State
to
Dim states() As State
and
states = New State()
into
ReDim states(10) 'or use a variable inside
Here's a sample of how to populate your data. I also fix some error in your query. Compare your query to mine to see what I changed.
' initialize a list with some values
Dim states = New List(Of State) From {
New State With {.Name = "Ohio", .Population = 10000, .landArea = 10},
New State With {.Name = "Texas", .Population = 20000, .landArea = 300},
New State With {.Name = "Florida", .Population = 5000, .landArea = 1000}
}
' query the list
Dim query = From st In states
Let name = st.Name
Let density = st.Density()
Order By density Descending
Select name, density
I also improve your State class to use proper data type instead of object.
Class State
Private m_name, m_abbr, m_date As String
Private m_area, m_pop As Integer
Public Property Name As String
Get
Return m_name
End Get
Set (value As String)
m_name = value
End Set
End Property
Public Property Abbreviation As String
Get
Return m_abbr
End Get
Set(value As String)
m_abbr = value
End Set
End Property
Public Property joinDate As String
Get
Return m_date
End Get
Set(value As String)
m_date = value
End Set
End Property
Public Property landArea As Integer
Get
Return m_area
End Get
Set(value As Integer)
m_area = value
End Set
End Property
Public Property Population As Integer
Get
Return m_pop
End Get
Set(value As Integer)
m_pop = value
End Set
End Property
Public Function Density() As Double
Dim popDensity As Double
popDensity = Population / landArea
Return popDensity
End Function
End Class

Using Class Properties to return ODBC calls to another class

I have this class - CompLoc it returns two fields from a ODBC call.
I want to call the class like this.
dim myCompLoc as New CompLoc(company,location)
newCompany = mycompLoc.Company
newLocation = mycompLoc.Location
my Class.
Private _company As String
Public Property Company() As String
Get
Return _company
End Get
Set(ByVal value As String)
_company = value
End Set
End Property
Private _location As String
Public Property Location() As String
Get
Return _location
End Get
Set(ByVal value As String)
_location = value
End Set
End Property
Public Sub New(ByVal loc As String, ByVal comp As String)
'select company,location from mysqlTable Where location = loc and company = comp'
_company = 'field from ODBC'
_location = 'field from ODBC'
End Sub
I need to pass to the ODBC company and location, to return company and location (I know...not always in our hands). I have this class that I want to populate from the ODBC call.
corp.EmpId = currentRecord(0).ToString
corp.FirstName = currentRecord(1).ToString
corp.LastName = currentRecord(2).ToString
corp.BasePay = Decimal.Parse(currentRecord(3).ToString)
corp.corpCompany = currentRecord(4).ToString
corp.corpLocation = currentRecord(5).ToString
I need to add to the end of this class ---
corp.newCompany = myCompLoc.Company
corp.newLocation = myCompLoc.Location
Can I have some quidance to build my myCompLoc class properly so that I can call it from corp and receive both properties with values. I can pass this around as an array or other method, but i want to do it a better way.
Here is how I solved this. corpSQL inherits company and location from a properties class
corp inherits from corpMySql
Dim corporation as New Corporation
Dim corp As New corpMySql
Dim newcorp = corp.Getcorp(corp.Company, corp.Location)
corporation.Company = newcorp.NewCompany
corporation.Plant = newcorp.NewLocation
I hope this makes sense. I was able to do what I set out to acheive, but I think another way is more fluid. Maybe using inheritence?

Getting a NullReferenceException when adding something to a list

I get a null reference exception when I try to use this webservice I'm working on. I have two fields in the object ipadarticle called fullname and tags, which are declared to be lists, so that ipadarticle can return multiple tags and authors. The null reference exception points to
ipadarticle2.FullName.Add(a_var.firstname + " " + a_var.lastname)
ipadarticle2.Tag.Add(a_var.tagtext)
I'm pretty new to vb programming, so I'm not really to sure what is causing this. To clarify, what is going on is that this stored procedure is fetching entries from a db, which has a list of articles with -among other things- tags and authors associated with it. Since articles have multiple tags and authors there are multiple entries for each article. When I return the info in the web service I am trying to make it so that only one ipadarticle object is returned for reach article, and then that contains a list of the multiple tags and authors associated with each article. I'm having a headache trying to figure this out.
Dim lq As New lqDFDataContext
Dim var = lq.mobile_IpadGetSavedArticlesAR(simpuser.UserID, tempParamKW(0), tempParamKW(1), tempParamKW(2), tempParamKW(3), tempParamKW(4), pageNum, pageLen)
Dim ipadarticle2 As New ipadArticle()
For Each a_var In var
If a_var.articleID <> temp Then
If flag = 0 Then
result.add(ipadarticle2)
Dim ipadarticle1 As New ipadArticle()
ipadarticle2 = ipadarticle1
End If
ipadarticle2.ArticleID = a_var.articleID
ipadarticle2.PublishedOn = a_var.publicationdate
ipadarticle2.Title = a_var.title
ipadarticle2.MedAbbr = a_var.medabbr.Replace(" ", "-").ToLower()
ipadarticle2.FullName.Add(a_var.firstname + " " + a_var.lastname)
ipadarticle2.Tag.Add(a_var.tagtext)
flag = 1
Else
ipadarticle2.Tag.Add(a_var.tagtext)
ipadarticle2.FullName.Add(a_var.firstname + " " + a_var.lastname)
flag = 0
End If
temp = a_var.articleID
Next
End If
Return result
ipadArticle class:
Imports Microsoft.VisualBasic
Public Class ipadArticle
Inherits SimpleObject
Public Sub New()
End Sub
Private _ArticleID As Integer
Public Property ArticleID() As Integer
Get
Return _ArticleID
End Get
Set(ByVal value As Integer)
_ArticleID = value
End Set
End Property
Private _Title As String
Public Property Title() As String
Get
Return _Title
End Get
Set(ByVal value As String)
_Title = value
End Set
End Property
Private _PublishedOn As String
Public Property PublishedOn() As String
Get
Return _PublishedOn
End Get
Set(ByVal value As String)
_PublishedOn = value
End Set
End Property
Private _MedAbbr As String
Public Property MedAbbr() As String
Get
Return _MedAbbr
End Get
Set(ByVal value As String)
_MedAbbr = value
End Set
End Property
Private _Tag As List(Of String)
Public Property Tag() As List(Of String)
Get
Return _Tag
End Get
Set(ByVal value As List(Of String))
_Tag = value
End Set
End Property
Private _FullName As List(Of String)
Public Property FullName() As List(Of String)
Get
Return _FullName
End Get
Set(ByVal value As List(Of String))
_FullName = value
End Set
End Property
End Class
The most likely cause is that the objects FullName and Tag have not been created (are Nothing) in ipadarticle2. These should most likely be created as new objects in the class constructor.
EDIT:
Based on the posted class, the above assumption was correct: FullName and Tag are defined as List(Of String), but the backing members are never created.
This can be fixed in a couple of ways:
1) Instantiate the backing member variables directly in their definition, i.e.:
Private _FullName As New List(Of String)
2) Instantiate the backing member variables in the constructor:
Public Sub New()
_FullName = New List(Of String)
_Tag = New List(Of String)
End Sub
3) Instantiate the backing member variable in the getter if it is nothing:
Public Property Tag() As List(Of String)
Get
If _Tag Is Nothing Then
_Tag = New List(Of String)
End If
Return _Tag
End Get
Basically, any variable types other than simple data types must be instantiated before they can be used (unless you test them for Nothingness).

how to create this class

I have a class created from an XSD file in vb.net2010
Partial Public Class responseOperation
Private attributeField() As attribute
Public Property attribute() As attribute()
Get
Return Me.attributeField
End Get
Set(ByVal value As attribute())
Me.attributeField = value
End Set
End Property
how do i instantiate or fill up attribute property where attribute class is given as
Partial Public Class attribute
Private nameField As String
Private valueField As String
Public Property name() As String
Get
Return Me.nameField
End Get
Set(ByVal value As String)
Me.nameField = value
End Set
End Property
Public Property value() As String
Get
Return Me.valueField
End Get
Set(ByVal value As String)
Me.valueField = value
End Set
End Property
End Class
when i run
xml_req_obj.attribute(0).value = "abcd"
xml_req_obj.attribute(0).name = "efg"
I get runtime error as im assigning something to a class that is not created
This seems like a trivial task
If some can tell me how can I create this object and pass data to attribute property that would be wonderful
Thankyou
Hansen
You are not instantiating the array or array items.
Try
Private attributeField() As attribute = New attribute(20)
and
xml_req_obj.attribute(0) = New Attribute()
xml_req_obj.attribute(0).value = "abcd"
xml_req_obj.attribute(0).name = "efg"