Searching a custom object - vb.net

As a follow-up to my previous question, and continuing with the sample code in the answer there, how can I e.g. find out if a ParameterSet exists in my ParameterSetCollection with a Y parameter of "foobar"? I can write code to iterate over the ParameterSet, but it seems there should be a better way.

Dim parameterSets As New ParameterSetCollection
'...
If parameterSetCollection.Any(Function(ps) ps.Y = "foobar") Then
'At least one item in the collection has a Y value of "foobar".
End If
Any is a LINQ method and LINQ excels at compressing loops. That code is functionally equivalent to this:
Dim parameterSets As New ParameterSetCollection
'...
Dim match = False
For Each ps In parameterSetCollection
If ps.Y = "foobar" Then
match = True
Exit For
End If
Next
If match Then
'At least one item in the collection has a Y value of "foobar".
End If

Related

How do I check if the string "Edgar Allan Poe" contains any items of List(Of String)?

I have a list of string:
Dim n As New List(Of String)
n.Add("Poe")
n.Add("Shakespeare")
Dim sAuthor As String = "Edgar Allan Poe"
How could I check if sAuthor contains any item of "n"?
I am missing the second argument:
If sAuthor.Contains(n, ...) Then
Thank you!
You could use the Any method (documentation):
If (n.Any(Function(item) sAuthor.Contains(item))) Then
' success!
End If
Fiddle: https://dotnetfiddle.net/r7y5Xw
What this does is return a Boolean value based on if any item in the collection evaluates to true based on the predicate.
In this case we are saying: Is any item in my List contained in the variable?
Update
Since the OP indicated that the above is too difficult for them to read/debug, this is how you would do the same thing without using LINQ:
Dim anyMatch As Boolean = False
For Each item In n
If (sAuthor.Contains(item)) Then
anyMatch = True
Exit For
End If
Next
If (anyMatch) Then
' success!
End If
Fiddle: https://dotnetfiddle.net/DPWo9m
The only (practical) difference between the two is that the above uses LINQ which provides a more concise mechanism of what is used below.
This
If (From a In n Where sAuthor.Contains(a) Select True Take 1).FirstOrDefault Then
Stop
End If

Compare two datatables, if anything is different show MessageBox

I have two datatables, one of them is populated when application starts and the other one is populated on button click. How can i check (fastest way) if anything changed in second datatable?
I have tried this but it does not work:
For Each row1 As DataRow In dtt.Rows
For Each row2 As DataRow In dtt1.Rows
Dim array1 = row1.ItemArray
Dim array2 = row2.ItemArray
If array1.SequenceEqual(array2) Then
Else
End If
Next
Next
The problem is that your loops are nested. This means that the inner For Each loops through each row of dtt1 for each single row of dtt. This is not what you want. You want to loop the two tables in parallel. You can do so by using the enumerators that the For Each statements use internally
Dim tablesAreDifferent As Boolean = False
If dtt.Rows.Count = dtt1.Rows.Count Then
Dim enumerator1 = dtt.Rows.GetEnumerator()
Dim enumerator2 = dtt1.Rows.GetEnumerator()
Do While enumerator1.MoveNext() AndAlso enumerator2.MoveNext()
Dim array1 = enumerator1.Current.ItemArray
Dim array2 = enumerator2.Current.ItemArray
If Not array1.SequenceEqual(array2) Then
tablesAreDifferent = True
Exit Do
End If
Loop
Else
tablesAreDifferent = True
End If
If tablesAreDifferent Then
'Display message
Else
'...
End If
The enumerators work like this: They have an internal cursor that is initially placed before the first row. Before accessing a row through the Current property, you must move to it with the MoveNext function. This function returns the Boolean True if it succeeds, i.e. as long as there are rows available.
Since now we have a single loop statement and advance the cursors of enumerator1 and enumerator2 at each loop, we can compare corresponding rows.
Note that the Rows collection implements IEnumerable and thus the enumerators returned by GetEnumerator are not strongly typed. I.e. Current is typed as Object. If instead you write
Dim enumerator1 = dtt.Rows.Cast(Of DataRow).GetEnumerator()
Dim enumerator2 = dtt1.Rows.Cast(Of DataRow).GetEnumerator()
Then you get enumerators of type IEnumerator(Of DataRow) returning strongly typed DataRows.

Looping Class Property with Do While Loops

I have a code like below to find a category property with named "Model" and after that i will get the Model's name.
But as you can see "parent" property shows the upper level of parameter. My parameters in parameter groups and its like cascaded. And i don't know in which level they are currently i'm using below code but it's not sufficient because if i have a parameter very in lower levels i had to write this elseif conditions.
Is there any quick solution to make it easier and wise way?
Public Class ParameterInfoClass
Public Shared Sub GetSubvar(ByVal ParameterGroups As IScrNamedObjectList)
Dim ParameterGroup As IScrParameterGroup
Dim nParameterGroup As Integer
Dim ParameterClass As String
nParameterGroup = ParameterGroups.count
For i As Integer = 0 To nParameterGroup - 1
ParameterGroup = ParameterGroups.item(i)
If ParameterGroup.parent.category.name = "Model" Then
ParameterClass = ParameterGroup.parent.name
ElseIf ParameterGroup.parent.parent.category.name = "Model" Then
ParameterClass = ParameterGroup.parent.parent.name
ElseIf ParameterGroup.parent.parent.parent.category.name = "Model" Then
ParameterClass = ParameterGroup.parent.parent.parent.name
'...
'This should be continue like this because i don't know in which level i will find the category name as "Model"
'.
End If
DataGridView1.Rows.Add(ParameterClass, ParameterGroup.name)
Next
End Sub
End Class
It would be great to make this section with correct solution. I thought like do-while loops can be one option but i dont know how to apply because focus is in here to look upper levels of parameter to find "Model" category after that i'm writing that Model's name.
For i As Integer = 0 To nParameterGroup - 1
ParameterGroup = ParameterGroups.item(i)
If ParameterGroup.parent.category.name = "Model" Then
ParameterClass = ParameterGroup.parent.name
ElseIf ParameterGroup.parent.parent.category.name = "Model" Then
ParameterClass = ParameterGroup.parent.parent.name
ElseIf ParameterGroup.parent.parent.parent.category.name = "Model" Then
ParameterClass = ParameterGroup.parent.parent.parent.name
'...
'This should be continue like this because i don't know in which level i will find the category name as "Model"
'.
End If
I don't have a lot of time to dig into the details, but recursion is something I love so I'm giving you a hint (I would help more but that's the time I have right now).
Instead of iterating through every possible parent level, you should create a simple recursive function which will look if it finds the "Model", then either return it or else look for it's parent by calling itself to look for it.
You would have to use this new function instead of your If ElseIf ElseIf... potentially infinite function.
I drafted something which kinda looks like what I mean:
Private Function GetParameterClass(rootClassName As rootClass) As String
If ParameterGroup.category.name = "Model" Then
Return ParameterGroup.category.name
End If
If ParameterGroup.parent IsNot Nothing Then
Return GetParameterClass(ParameterGroup.parent)
End If
Return ""
End Function
By calling a similar function, you will iterate recursively through every parent until it finds none, and the first time it finds "Model", it'll stop the recursion and return it.
Sorry for not being more precise, as I have to get back to work myself! I'll look on your thread this evening when I can, just in case. Have fun!
EDIT:
I'm not familiar with the class you're working with, so there's a good amount of guesswork in this edit. Here's how I would try to solve your issue:
Public Class ParameterInfoClass
Public Shared Sub GetSubvar(ByVal ParameterGroups As IScrNamedObjectList)
For Each parameterGroup As IScrParameterGroup In ParameterGroups
Dim parameterClass As String = GetParameterClassName(parameterGroup)
If parameterName <> "" Then
DataGridView1.Rows.Add(parameterClass, parameterGroup.Name)
End If
Next
End Sub
Private Shared Function GetParameterClassName(parameterGroup As IScrParameterGroup) As String
If parameterGroup.category.name = "Model" Then
Return parameterGroup.name
End If
If parameterGroup.parent IsNot Nothing Then
Return GetParameterClass(parameterGroup.parent)
End If
Return ""
End Function
End Class
The main idea behind GetParameterClassName is that it'll either find the parameterGroup.category.name = "Model", or else it'll return an empty string. I replaced the way you were planning to iterate with a For Each loop, which should work well with most lists, but you might need to ajust that part is IScrNamedObjectList is not or doesn't contains a list or array or something.
Whenever a GetParameterClassName is found, then the parameterClass is not empty, so we can add these informations to the DataGridView1.
You can ask your questions in the comments if you have any, and I'll be happy to oblige. I love recursion!

Looping through Entity Framework complex type

Trying to loop through the results of a stored procedure using entity framework. I need to compare the list of communities to the value in a textbox so the user does not enter duplicate communities in the database, using a duplicate flag. I can retrieve my list of communities but I'm having difficulty looping through that list.
Dim duplicate = False
Dim list = Accommodations.GetCommunities
For Each n As String In list.CommunityName
If n = txtCommunities.Text Then
duplicate = True
End If
Next n
While debugging I can hover over Accommodations.GetCommunities and see all of the values I need to loop through under the field "CommunityName" but when I step through the loop, the value for n shows up as a single character. Is there a way to turn this result set into a list so that I can loop through each value under "CommunityName"
I've also tried the below code and it sets com equal to the name of the complex type for some reason, but it properly loops through the correct number of items in the list. How can I extract that field to compare it to the textbox?
Dim duplicate = False
Dim com As String = String.Empty
Dim list = Accommodations.GetCommunities
For i As Integer = 0 To list.count - 1
com = list(i).ToString
If com = txtCommunities.Text Then
duplicate = True
End If
Next
Return duplicate
FINAL EDIT:
This is the function I used after. Used the String.Compare() method to compare the strings to ignore the case as well.
Private Function checkDuplicates()
Dim duplicate = False
Dim list = Accommodations.GetCommunities 'Put items in a list
For i As Integer = 0 To list.count - 1 'loop through the list
If String.Compare(list(i).CommunityName, txtCommunities.Text, True) = 0 Then 'Compare the two strings, comparrison is not case sensitive
duplicate = True 'set dup flag to true
Exit For 'exit loop
End If
Next
Return duplicate
End Function
In the first case you are comparing n to each character in CommunityName. You should do something like:
For Each n As String In list
If n.CommunityName = txtCommunities.Text Then
In the second case list(i) is a community in the list. Thus list(i).ToString() shows the name of the variable.
I think it should look something more like
Dim duplicate = False
Dim list = Accommodations.GetCommunities
For Each community As [something] In list
If community.CommunityName = txtCommunities.Text Then
duplicate = True
End If
Next n
or
Dim duplicate = False
Dim list = Accommodations.GetCommunities
For i As Integer = 0 To list.count - 1
If list(i).CommunityName = txtCommunities.Text Then
duplicate = True
End If
Next
Return duplicate
Side note: Try to use good variable names instead of just n. Also, when you find a duplicate, you can exit for loop or just return True right away.
Sample 1
Ther is also option using dictionary, If u have your list as a dictionary u don't need looping after if u wana check add or remove by unique key value
Public Class TestClass
Property Name As String
End Class
Private Function TestFun() As Boolean
'Sample List to convert use Accommodations.GetCommunities
Dim List As New List(Of TestClass)
List.Add(New TestClass With {.Name = "a"})
List.Add(New TestClass With {.Name = "b"})
'If at Begining all elements by key are unique u can convert to dictionary
'
'From This Point u can map your list in to dictionary Use Accommodations.GetCommunities instant List. and type of what use in this collection replece as TestClass
Dim CheckInDictionary As Dictionary(Of String, TestClass) = List.ToDictionary(Function(p) p.Name, Function(p) p)
' After you can us if some key or Id exist
Return CheckInDictionary.ContainsKey("a")
End Function 'Return True
Sample 2
Ther is Also Another Possibility to Check if List have duplicate, but this is abit more complex.
List have method Contains that check if element exist in said list, so before add new element you can check if list have it. To Compare is used Method Equal so if you overide it in your base class you can do special rules for equal.
Like
Public Class TestClass
Property Name As String
Public Overrides Function Equals(obj As Object) As Boolean
'IMPORTEND Input is as object if anny case will be somthing diffrent then this type it can meak exception
If DirectCast(obj, TestClass).Name = Me.Name Then Return True
Return MyBase.Equals(obj)
End Function
End Class
and after when u atempt to add new element u dolike that
Dim NewEle = New TestClass With {.Name = "a"}
If Not List.Contains(NewEle) Then
List.Add(NewEle)
End If

Checking if a value is a member of a list

I have to check a piece of user input against a list of items; if the input is in the list of items, then direct the flow one way. If not, direct the flow to another.
This list is NOT visible on the worksheet itself; it has to be obfuscated under code.
I have thought of two strategies to do this:
Declare as an enum and check if input is part of this enum, although I'm not sure on the syntax for this - do I need to initialise the enum every time I want to use it?
Declare as an array and check if input is part of this array.
I was wondering for VBA which is better in terms of efficiency and readability?
You can run a simple array test as below where you add the words to a single list:
Sub Main1()
arrList = Array("cat", "dog", "dogfish", "mouse")
Debug.Print "dog", Test("dog") 'True
Debug.Print "horse", Test("horse") 'False
End Sub
Function Test(strIn As String) As Boolean
Test = Not (IsError(Application.Match(strIn, arrList, 0)))
End Function
Or if you wanted to do a more detailed search and return a list of sub-string matches for further work then use Filter. This code would return the following via vFilter if looking up dog
dog, dogfish
In this particular case the code then checks for an exact match for dog.
Sub Main2()
arrList = Array("cat", "dog", "dogfish", "mouse")
Debug.Print "dog", Test1("dog")
Debug.Print "horse", Test1("horse")
End Sub
Function Test1(strIn As String) As Boolean
Dim vFilter
Dim lngCnt As Long
vFilter = Filter(arrList, strIn, True)
For lngCnt = 0 To UBound(vFilter)
If vFilter(lngCnt) = strIn Then
Test1 = True
Exit For
End If
Next
End Function
Unlike in .NET languages VBA does not expose Enum as text. It strictly is a number and there is no .ToString() method that would expose the name of the Enum. It's possible to create your own ToString() method and return a String representation of an enum. It's also possible to enumerate an Enum type. Although all is achievable I wouldn't recommend doing it this way as things are overcomplicated for such a single task.
How about you create a Dictionary collection of the items and simply use Exist method and some sort of error handling (or simple if/else statements) to check whether whatever user inputs in the input box exists in your list.
For instance:
Sub Main()
Dim myList As Object
Set myList = CreateObject("Scripting.Dictionary")
myList.Add "item1", 1
myList.Add "item2", 2
myList.Add "item3", 3
Dim userInput As String
userInput = InputBox("Type something:")
If myList.Exists(userInput) Then
MsgBox userInput & " exists in the list"
Else
MsgBox userInput & " does not exist in the list"
End If
End Sub
Note: If you add references to Microsoft Scripting Runtime library you then will be able to use the intelli-sense with the myList object as it would have been early bound replacing
Dim myList As Object
Set myList = CreateObject("Scripting.Dictionary")
with
Dim myList as Dictionary
Set myList = new Dictionary
It's up to you which way you want to go about this and what is more convenient. Note that you don't need to add references if you go with the Late Binding while references are required if you want Early Binding with the intelli-sense.
Just for the sake of readers to be able to visualize the version using Enum let me demonstrate how this mechanism could possibly work
Enum EList
item1
item2
item3
[_Min] = item1
[_Max] = item3
End Enum
Function ToString(eItem As EList) As String
Select Case eItem
Case EList.item1
ToString = "item1"
Case EList.item2
ToString = "item2"
Case EList.item3
ToString = "item3"
End Select
End Function
Function Exists(userInput As String) As Boolean
Dim i As EList
For i = EList.[_Min] To EList.[_Max]
If userInput = ToString(i) Then
Exists = True
Exit Function
End If
Next
Exists = False
End Function
Sub Main()
Dim userInput As String
userInput = InputBox("type something:")
MsgBox Exists(userInput)
End Sub
First you declare your List as Enum. I have added only 3 items for the example to be as simple as possible. [_Min] and [_Max] indicate the minimum value and maximum value of enum (it's possible to tweak this but again, let's keep it simple for now). You declare them both to be able to iterate over your EList.
ToString() method returns a String representation of Enum. Any VBA developer realizes at some point that it's too bad VBA is missing this as a built in feature. Anyway, you've got your own implementation now.
Exists takes whatever userInput stores and while iterating over the Enum EList matches against a String representation of your Enum. It's an overkill because you need to call many methods and loop over the enum to be able to achieve what a simple Dictionary's Exists method does in one go. This is mainly why I wouldn't recommend using Enums for your specific problem.
Then in the end you have the Main sub which simply gathers the input from the user and calls the Exists method. It shows a Message Box with either true or false which indicates if the String exists as an Enum type.
Just use the Select Case with a list:
Select Case entry
Case item1,item2, ite3,item4 ' add up to limit for Case, add more Case if limit exceeded
do stuff for being in the list
Case Else
do stuff for not being in list
End Select