Finding Element in a Linked List of Type Structure (VB.NET) - vb.net

I have declared a structure:
Public Structure MyStructure
Public name As String
Public dataType As String
Public address As String
End Structure
Then a Linked List:
Private MyList As New LinkedList(Of MyStructure)
What's the best way to find an element in the list given the value of an element in the struct. For example if I want to find the instance of MyStruct where the field name is "readings" in the list, how should I do it? Is there a way to avoid looping through the linked list elements?

You'd probably be better off using a class instead of a Structure:
Public Class MyFoo
Public Property name As String
Public Property dataType As String
Public Property address As String
End Class
This allows the following:
Dim lst As New LinkedList(Of MyFoo)
lst.AddFirst(New MyFoo With {.name = "Ziggy", .address = "here", .dataType = "foo"})
lst.AddFirst(New MyFoo With {.name = "Zoey", .address = "there", .dataType = "bar"})
lst.AddFirst(New MyFoo With {.name = "Curly", .address = "nowhere", .dataType = "any"})
Dim item = lst.FirstOrDefault(Function(x) x.name = "Ziggy")
If item IsNot Nothing Then
' do something
End If
There really isnt a way to avoid looping - something somewhere has to iterate the collection to find "Ziggy" (or "readings"); here, we just don't have to write code for it.
FirstOrDefault will return Nothing if the item cannot be found. Since a Structure is a value type, it cannot ever be Nothing (though it can contain Nothing). As a result, the line If item IsNot Nothing results in a syntax error.
I suppose with a Structure, you could use String.IsNullOrEmpty to test if the name is filled in, but I would just use a class.

Related

Class object property which references another object instance of same object type

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.

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

VB.NET overload () operator

I am new to VB.NET and search for a method to copy the behaviour of a DataRow for example.
In VB.NET I can write something like this:
Dim table As New DataTable
'assume the table gets initialized
table.Rows(0)("a value") = "another value"
Now how can I access a member of my class with brackets? I thought i could overload the () Operator but this seems not to be the answer.
It's not an overload operator, this known as a default property.
"A class, structure, or interface can designate at most one of its properties as the default property, provided that property takes at least one parameter. If code makes a reference to a class or structure without specifying a member, Visual Basic resolves that reference to the default property." - MSDN -
Both the DataRowCollection class and the DataRow class have a default property named Item.
| |
table.Rows.Item(0).Item("a value") = "another value"
This allows you to write the code without specifying the Item members:
table.Rows(0)("a value") = "another value"
Here's a simple example of a custom class with a default property:
Public Class Foo
Default Public Property Test(index As Integer) As String
Get
Return Me.items(index)
End Get
Set(value As String)
Me.items(index) = value
End Set
End Property
Private ReadOnly items As String() = New String(2) {"a", "b", "c"}
End Class
Dim f As New Foo()
Dim a As String = f(0)
f(0) = "A"
Given the example above, you can use the default property of the string class to get a character at specified position.
f(0) = "abc"
Dim c As Char = f(0)(1) '<- "b" | f.Test(0).Chars(1)

.net - Using Class as one parameter

I have a class with several properties.
Public Class test
Public Property a As String
Public Property b As String
Public Property c As String
Public Property d As String
Public Property e As String
Public Property f As String
Public Property g As String
End Class
In my VB.net code, I am assigning a value to each property.
I want to send the whole test class as one parameter, and use all the values inside it.
So that if I add extra parameters later on, I want them to be used dynamically, instead of writing this everytime:
Textbox1.text= test.a & test.b & test.c .......
Any way to do this?
Im not really writing the values in a textbox, but this is just an simplified example.
I think what you want is a property. You'll need to add a property to your class like:
Public Property Combination() As String
Get
Return a & b & c & d & e ...
End Get
End Property
Then to get the value you'd use
Textbox1.text = test.combination
(for more details you can see http://www.dotnetperls.com/property-vbnet)
I recommend you override the built-in ToString function. Also, to further simplify this, add a CType operator.
Public Class test
Public Property a As String
Public Property b As String
Public Property c As String
Public Property d As String
Public Property e As String
Public Property f As String
Public Property g As String
Public Shared Widening Operator CType(obj As test) As String
Return If((obj Is Nothing), Nothing, obj.ToString())
End Operator
Public Overrides Function ToString() As String
Return String.Concat(Me.a, Me.b, Me.c, Me.d, Me.e, Me.f, Me.g)
End Function
End Class
The you could just do:
Textbox1.text = test
There is a way to dynamically get and set the value of properties on any object. Such functionality in .NET is collectively referred to as Reflection. For instance, to loop through all of the properties in an object, you could do something like this:
Public Function GetPropertyValues(o As Object) As String
Dim builder As New StringBuilder()
For Each i As PropertyInfo In o.GetType().GetProperties
Dim value As Object = Nothing
If i.CanRead Then
value = i.GetValue(o)
End If
If value IsNot Nothing Then
builder.Append(value.ToString())
End If
Next
Return builder.ToString()
End Function
In the above example, it calls i.GetValue to get the value of the property, but you can also call i.SetValue to set the value of the property. However, reflection is inefficient and, if used inappropriately, it can lead to brittle code. As such, as a general rule, you should avoid using reflection as long as there is any other better way to do the same thing. In other words, you should typically save reflection as a last resort.
Without more details, it's difficult to say for sure what other options would work well in your particular situation, but I strongly suspect that a better solution would be to use a List or Dictionary, for instance:
Dim myList As New List(Of String)()
myList.Add("first")
myList.Add("second")
myList.Add("third")
' ...
For Each i As String In myList
Textbox1.Text &= i
Next
Or:
Dim myDictionary As New Dictionary(Of String, String)()
myDictionary("a") = "first"
myDictionary("b") = "first"
myDictionary("c") = "first"
' ...
For Each i As KeyValuePair(Of String, String) In myDictionary
Textbox1.Text &= i.Value
Next

Difficulties sending a type to a generic list

I'm trying to use a generic list without knowing the type when loading the page. I have a typePropertyCollection which inherits from List(Of PropertyData). The usercontrol that uses this collection doesn't know what type of data is used (which objects). So when the page is loaded, I pass along the type to the usercontrol using a dependencyproperty. This type ends up in this method:
Private Shared Sub OnObjectTypeChanged(ByVal obj As DependencyObject, ByVal args As DependencyPropertyChangedEventArgs)
Dim objectType As Type = TryCast(args.NewValue, Type)
Dim aList As List(Of PropertyData) = New TypePropertyCollection(Of objectType)
End Sub
I can succesfully retrieve the type from the EventArgs and put it in a variable. When I'm creating a new typePropertyCollection, I want to pass the type to the generic list, but it says the objectType isn't defined, although is is declared just the line above.
Any suggestions?
Edit
The class typepropertyCollection looks like this:
Public Sub New()
Dim properties = New List(Of PropertyInfo)(GetType(T).GetProperties(BindingFlags.Public Or BindingFlags.Instance))
For Each propertyToCheck In properties
Dim descriptionAttribute = propertyToCheck.GetCustomAttributes(GetType(DescriptionAttribute), True)
If Not descriptionAttribute Is Nothing AndAlso descriptionAttribute.Length > 0 Then
Add(New PropertyData() With {.Description = DirectCast(descriptionAttribute(0), DescriptionAttribute).Description, .PropertyName = propertyToCheck.Name})
Else
Add(New PropertyData() With {.Description = propertyToCheck.Name, .PropertyName = propertyToCheck.Name})
End If
Next
End Sub
To use this collection, I'm creating a new class which inherits from the typcollection:
Public Class CustomerTypePropertyCollection
Inherits TypePropertyCollection(Of Person)
End Class
I cannot do this because Person (I named it Person here to make it easier) is not known in that solution. It should also be possible to make collection of other types which or not known. That's why I wanted to pass the type of the object and use it that way.
Dim aList As List(Of PropertyData) = New TypePropertyCollection(Of Type)
This error is because you are attempting to create a TypePropertyCollection of Type 'objectType', objecttype is the variable name of a variable with a type of type .'. You would need a TypePropertyCollection of Type Type Type, not the variable name. Come back to me if there are other issues beyond this.