Class object property which references another object instance of same object type - vb.net

I have a class object which looks like this:
Public Class item
Public Property ID
Public Property Name
Public Property Description
Public Property Type
Public Property Alias
End Class
I am currently storing these as a dictionary like this:
Public Class Items
Public ReadOnly dict Dictionary(Of String, item) From {
{"A", New item With {.Name = "Object A", .Description = "Object A description"}},
{"B", New item With {.Name = "Object B", .Description = "Object B description"}},
{"C", New item With {.Alias = "A"}}
}
Public Function GetItem(ByVal ID As String) As item
Return If(dict.ContainsKey(ID), idct.Item(ID), Nothing)
End Function
End Class
The complexity is that sometimes an item will not have any properties itself but instead has an .Alias property which says "All of my properties are the same as item with this ID, check that object instead".
How should I write my class object item so that this code returns "Object A":
Dim newItem As item = GetItem("C")
Debug.WriteLine(item.Name)
Object C is an alias of Object A so I should return some properties (not always all of them) for Object A instead of Nothing.
A way around this is by adding the below function to the Items class:
Public Function GetItemDescription(ByVal ID As String) As String
If dict.ContainsKey(ID) Then
If dict.Item(ID).Description = "" Then
Return GetItemDescription(dict.Item(ID).Alias)
Else
Return dict.Item(ID).Description
End If
Else
Return ""
End If
End Function
However this doesn't feel like the correct way as then I have to repeatedly call a set of Items.GetPropertyXYZ functions rather than directly referencing the object (e.g. item.Description would have to be GetItemDescription("C")
Is my solution acceptable from a design persepctive, or is there a better way to achieve this?

Try this:
Public Function GetItem(ByVal [alias] As String) As item
Return dict.Where(Function(a) a.Key = [alias]).Select(Function(b) b.Value).FirstOrDefault
End Function
Edit 1
Certainly it returns the "C" item because its wrong. Sorry.
This one works (Tested):
Public Function GetItem(ByVal ID As String) As item
Dim itm As item = dict.Where(Function(a) a.Key = ID).Select(Function(b) b.Value).FirstOrDefault
Return If(itm IsNot Nothing, If(itm.Alias IsNot Nothing, dict(itm.Alias), itm), Nothing)
End Function

JQSOFT's answer does achieve a similar thing, however I've since realised a more granular way I can achieve the same result.
Private Property _description As String
Public Property Description As String
Get
If _Description = "" Then
If [Alias] IsNot Nothing Then
Return dict.Item([Alias]).Description
Else
Return ""
End If
Else
Return _description
End If
End Get
Set(value As String)
_description = value
End Set
End Property
This way allows me to specify whether I return the data from Object A at property level, rather than returning an entirely different object.
Also, .Alias is a terrible property name as it's also a keyword, I'm going to use .Synonym.

Related

Can't iterate through each property of a custom object

I have this class:
Public Class clsServCasam
Public ID As Long
Public CANT As Decimal
Public PRICE As Decimal
End Class
I create a variable of that type and get the object from an API result:
Dim myObj As clsServCasam()
Dim rsp As HttpWebResponse = CType(rq.GetResponse(), HttpWebResponse)
If rsp.StatusCode = HttpStatusCode.OK Then
Using sr = New StreamReader(rsp.GetResponseStream())
myObj = JsonConvert.DeserializeObject(Of clsServCasam())(sr.ReadToEnd())
End Using
Then I try to get the field names from the object:
For Each p As System.Reflection.PropertyInfo In myObj.GetType().GetProperties()
Debug.Print(p.Name, p.GetValue(myObj, Nothing))
Next
But, instead of class fields (ID, PRICE, ...) I got:
- Length
- LongLength
- Rank
Update
As Steven Doggart pointed out, the above loop won't work because it looks for properties rather than fields. So, I tried changing the loop to this:
For Each p As FieldInfo In myObj.GetType.GetFields()
Debug.Print(p.Name)
Next
But now I'm getting no results at all.
In your code, myObj is not declared as clsServCasam. Rather, it is declared as clsServCasam(), which means it's an array of clsServCasam objects. So, when you use reflection to iterate over its properties, you're getting the properties of the array rather than the actual clsServCasam type.
For instance, this would work more like you're expecting:
For Each item As clsServCasam in myObj
For Each p As PropertyInfo In item.GetType().GetProperties()
Debug.Print(p.Name, p.GetValue(item, Nothing))
Next
Next
However, I think you'll find that that still won't work because it iterates over the properties rather than the fields. In the definition of the clsServCasam class, all of the members are fields rather than properties, so the only properties that it have would be ones that are inherited from Object. You will need to either iterate over the fields using GetFields, like this:
For Each item As clsServCasam in myObj
For Each f As FieldInfo In item.GetType().GetFields()
Debug.Print(f.Name, f.GetValue(item))
Next
Next
Or you'll need to change them to properties:
Public Class clsServCasam
Public Property ID As Long
Public Property CANT As Decimal
Public Property PRICE As Decimal
End Class
Or, if you are using an older version of the VB compiler which doesn't support auto-properties:
Public Class clsServCasam
Public Property ID As Long
Get
Return _id
End Get
Set(value As Long)
_id = value
End Set
End Property
Public Property CANT As Decimal
Get
Return _cant
End Get
Set(value As Decimal)
_cant = value
End Set
End Property
Public Property PRICE As Decimal
Get
Return _price
End Get
Set(value As Decimal)
_price = value
End Set
End Property
Private _id As Long
Private _cant As Decimal
Private _price As Decimal
End Class

.Where method not defined on generic typed list?

When I try to use the .Where() method on a list, this does method does not seem to be defined if the list is of a generic type:
In my program, I have a class called Warning, and in another class, a list of warnings, defined as:
Dim warningList As List(Of Warning)
When I try to manipulate this list as:
Dim item = warningList.Where(Function(x) x.GetName() = "Foo").FirstOrDefault()
This works completely fine, but when I try it like this:
Dim itemList
if(type = "Warning") Then 'Please note that this condition is true...
itemList = warningList
End If
Dim item = itemList.Where(Function(x) x.GetName() = "Foo").FirstOrDefault()
I get an exception, stating that method .Where() is not defined for class Warning
Can anybody tell me why this is?
Thank you!
Now that you've edited your question it's clear.
You declare itemList without a type, so it's Object implicitly(in VB.NET with option strict set to Off which i strongly recommend against).
Now that you have declared a variable of type Object you can asssign any type to it. But you would have to cast it back to its real type List(Of Warning) to be able to use list or LINQ methods(which extend IEnumerable(Of T).
But instead declare it with the correct type:
Dim itemList As List(Of Warning)
if(type = "Warning") Then
itemList = warningList
End If
Dim item = itemList.Where(Function(x) x.GetName() = "Foo").FirstOrDefault()
Including to comment to explain why Warning is not related to this problem:
That's not the real code. If warningList is really a List(Of Warning)
you should be able to use Enumerable.Where(if LINQ is
imported). The fact that you assign this instance to another variable
(on declaration) doesn't change anything because that variable's type
is also a List(Of Warning). So itemList.Where should work too. Warning
has nothing to do with it because the type which is extended by Where
is IEnumerable(Of T), T can be any type(even Object). Since List(Of T)
implements IEnumerable(Of T) you can use Enumerable.Where on any list
(or array).
If you actually have multiple types and Warning is just one of it, you should implement a common interface. Here's an example:
Public Enum NotificationType
Warning
Info
[Error]
End Enum
Public Interface INamedNotification
ReadOnly Property Type As NotificationType
Property Name As string
End Interface
Public Class Warning
Implements INamedNotification
Public Sub New( name As String )
Me.Name = name
End Sub
Public Property Name As String Implements INamedNotification.Name
Public ReadOnly Property Type As NotificationType Implements INamedNotification.Type
Get
Return NotificationType.Warning
End Get
End Property
End Class
Now you can declare a List(Of INamedNotification) and fill it with whatever implements this interface, like the Warning class:
Dim notificationList As List(Of INamedNotification)
if type = "Warning" Then
itemList = warningList
Else If type = "Info"
itemList = infoList
End If
Dim item = notificationList.Where(Function(x) x.Name = "Foo").FirstOrDefault()

For a combobox with item = a datarow, how to point ValueMember to one of it's columns

I add items to a combobox like this:
For each R as DataRow in MyDataTable.Rows
If R("ID") > 10 then MyCombo.Items.Add(R)
Next
And now I need to set the DisplayMember and ValueMember to a column of the datarow:
MyCombo.ValueMember = R("ID")
MyCombo.DisplayMember = R("Name")
I know it doesn't make sence to to use "R" as it doesn't reference to anything at this point but it's just to make an indication of what I mean ;-)
The documentation for ValueMember says:
"A String representing a single property name of the DataSource property value, or a hierarchy of period-delimited property names that resolves to a property name of the final data-bound object"
I know I can add the rows to a new datatable and set it to the DataSource, but as you can add any object to the combobox items, it would be nice to use the rows directly, just can't figures out how to make a reference the particular column as a string.?
Maybe you cannot use a row object directly. I guess to use Valuemember you need your item objects to be wrapped in a collection which implement an ilist interface.
In the old MS-Access days combobox items had natively Display- and ValueMember properties, I've always missed that in the .Net combobox control.
My work-around is to use this class, which then can be used for all your ComboBoxes:
Class oComboItems
Public items As New List(Of oDVpairs)
Class oDVpairs
Implements IComparable(Of oDVpairs)
Private myDM As String
Private myVM As Object
Sub New(DM As String, VM As Object)
myDM = DM
myVM = VM
End Sub
Public ReadOnly Property DM() As String
Get
Return myDM
End Get
End Property
Public ReadOnly Property VM() As Object
Get
Return myVM
End Get
End Property
Public Function CompareTo(other As oDVpairs) As Integer Implements IComparable(Of oDVpairs).CompareTo
Return Me.myDM.CompareTo(other.myDM)
End Function
End Class
Public Sub AddItems(DisplayMember As String, ValueMemeber As Object)
items.Add(New oDVpairs(DisplayMember, ValueMemeber))
End Sub
Public ReadOnly Property DisplayMember() As String
Get
Return "DM"
End Get
End Property
Public ReadOnly Property ValueMember() As Object
Get
Return "VM"
End Get
End Property
End Class
And now add my datarows(or any other objects) to the ComboBox:
Dim CI As New oComboItems
For Each R As DataRow In DT_U.Rows
If R("medlnr") > 10 Then
CI.AddItems(R("name"), R("ID"))
end if
Next
CI.items.Sort()
MyCombo.DataSource = CI.Items
MyCombo.DisplayMember = CI.DisplayMember
MyCombo.ValueMember = CI.ValueMember

How to use instance of New Object in With... Block

Dim objects As New List(Of Object)
With New Object
.prop1 = "Property 1"
.prop2 = "Property 2"
objects.add(.instance) 'i mean instance of New Object
End With
is it possible.
I ask new question because last question has mislead information and I don't give right answer. so here code.
No it is not possible. The With statement basically creates an implicit variable. All you can do with that variable is access members and there is no member that returns a reference to the object itself.
If you want succinct code to create, populate and add an object to a list then do this:
myList.Add(New SomeType With {.SomeProperty = someValue,
.SomeOtherProperty = someOtherValue})
Interestingly, you can make it work the way you wanted if you create your own extension method. I was under the impression that you could not extend the Object class but either I was wrong or that has changed because I just tried in VB 2013 and it worked. You can write a method like this:
Imports System.Runtime.CompilerServices
Public Module ObjectExtensions
<Extension>
Public Function Self(Of T)(source As T) As T
Return source
End Function
End Module
and then do something like this:
With New SomeType
.SomeProperty = someValue
.SomeOtherProperty = someOtherValue
myList.Add(.Self())
End With
I'm not sure that that really provides any benefit though, given the availability of the object initialiser syntax that I demonstrated first.
Hmmm... I just realised that that's not actually extending the Object class. It was my original intention to try to do so but then I realised that a generic method was better because it would then return the same type as you call it on. I did just test it with a non-generic method extending type Object and it did still worked though.
You should to create your own class By example :
Public Class Car
Private _NumberCar As Integer
Public Property NumberCar() As Integer
Get
Return _NumberCar
End Get
Set(ByVal value As Integer)
_NumberCar = value
End Set
End Property
Private _ColorCar As Color
Public Property ColorCar() As Color
Get
Return _ColorCar
End Get
Set(ByVal value As Color)
_ColorCar = value
End Set
End Property
Private _OwnerName As String
Public Property OwnerName() As String
Get
Return _OwnerName
End Get
Set(ByVal value As String)
_OwnerName = value
End Set
End Property
End Class
and in the Class where you want to add the cars object do this :
Dim CarList As New List(Of Car)
Dim item As New Car
With item
.NumberCar = 1243
.ColorCar = Color.Red
.OwnerName = "Ibra"
End With
CarList.Add(item)
strong text

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