For each is printing same Item - vb.net

The variable ListofBloodType is returning the correct values not sure why in the loop I am getting the first value 4 times ( the count 4 is correct also but printing the first value 4 times is not)
Dim tempTreatmentForBloodType As List(Of BloodType) = New List(Of BloodType)()
Dim ListofBloodType = getPatientBloodType(i.PatientID)
For Each i As PatientBloodType In ListofBloodType
tempTreatmentForBloodType.Add(([Enum].Parse(GetType(BloodType), getReference().Description)))
Next

The problem was exactly what GSerg pointed at, I was not using the loop variable. I created new getReference method with parameter to get the data for a particular patient. Here is the code that worked for me! I hope it helps new VB programmers like me!
Dim tempTreatmentForBloodType As List(Of BloodType) = New List(Of BloodType)()
Dim ListofBloodType = getPatientBloodType(i.PatientID)
For Each i As PatientBloodType In ListofBloodType
tempTreatmentForBloodType.Add(([Enum].Parse(GetType(BloodType),
getReference(i.PatientID).Description)))
Next
Many Thanks to the above helpful comments!

Related

How can I read the value of a given item in a tuple in a given row?

I'm new to tuples in VB.Net, did a search here before posting this but could not find anything that could lead me to what I'm trying to do...
I have a public tuple that was declared as a list of two strings...
After populating the tuple, at a certain point, I want to check every row of the tuple and find if the value of the 2nd item of that given row is an empty string... I want to use a For Loop so I can use the i variable to loop through each row of the tuple...
So in the line If MyTuple.(i.Item2) <> String.text Then, the pseudocode would be something like this:
Get the tuple's Item2 value for the row i and check if it is empty. Of course, the code above is garbage, I'm just trying to express what I'm after...
Public MyTuple As New List(Of Tuple(Of String, String))
MyTuple.Add(Tuple.Create("John", "Claire"))
MyTuple.Add(Tuple.Create("Dave", "Juan"))
For i As Integer = 0 To MyTuple.Count - 1
If MyTuple.(i.Item2) <> String.Empty Then
'Do something
End If
Next
Any ideas on how I could achieve that?
Thank you!
I went with #jimi suggestion and it worked.
Public MyTuple As New List(Of (SomeName As String, OtherName As String))
Closing. Thanks, everyone for the input.

Cannot access the data from a List(Of List)

I am working on a Revit Add-in and in that add-in I am trying to use a List(Of List(Of Curve)), however I'm having issue accessing the data from the sublists.
Dim ClosedCurveList As New List(Of List(Of Curve))
Dim ClosedCurve As new List (Of Curve)
For i=0 To FinalWallLines.Count-1
If FinalWallLines(i+1).GetEndPoint(0).X = FinalWallLines(i).GetEndPoint(1).X And _
FinalWallLines(i+1).GetEndPoint(0).Y = FinalWallLines(i).GetEndPoint(1).Y And _
FinalWallLines(i+1).GetEndPoint(0).Z = FinalWallLines(i).GetEndPoint(1).Z Then
ClosedCurve.Add(FinalWallLines(i))
Else
TaskDialog.Show("A",ClosedCurve.Count)
ClosedCurveList.Add(ClosedCurve)
TaskDialog.Show("B", ClosedCurveList(ClosedCurveList.Count-1).Count)
ClosedCurve.Clear()
End if
Next
TaskDialog.Show("C", ClosedCurveList.Count)
For i=0 To ClosedCurveList.Count-1
TaskDialog.Show(i,ClosedCurveList(i).Count)
next
So when I run that code, the first TaskDialog.Show("A",ClosedCurve.Count) shows me that all of ClosedCurve are made of 4 curves, which makes sense as all my curves are forming rectangles.
My second TaskDialog.Show("B", ClosedCurveList(ClosedCurveList.Count-1).Count) also return 4 as the count for each of the sublists, as expected.
My third TaskDialog.Show("C", ClosedCurveList.Count) returns 23.
So from that, we can gather than ClosedCurveList is a list of 23 lists of 4 curves.
However, during my loop For i=0 To ClosedCurveList.Count-1, my TaskDialog.Show(i,ClosedCurveList(i).Count) returns 23 0s.
Would anyone know why I am not getting 23 4s as expected when trying to access the count of each of my sublists?
Instead of ClosedCurve.Clear() you should have ClosedCurve = new List(Of Curve).
When you add it to ClosedCurveList you are not adding a copy. You are adding the reference to the object CLosedCurve. And so, when you clear ClosedCurve, it also clears the one which was added in ClosedCurveList because they are references to the same object. By re-assigning a new List(Of Curve) to ClosedCurve, you will now have separate references, like you were originally expecting.

LINQ Count problems while trying to access to subList from a List of Objects (VB.NET)

I'm having a pretty tough time figuring it out why it doesn't work properly but I'm asking for it.
Dim goodCount As Integer = (From item In equipmentTagList
Where item.Importance = "Critique"
Where item.Status <> TargetRange.OutOfRange
Select item).Count()
Dim badCount As Integer = (From item In equipmentTagList
Where item.Importance = "Critique"
Where item.Status.Contains(TargetRange.OutOfRange)
Select item).Count()
EquipmentTagList is a List(Of MachineTag) (custom object) so I want to get how many MachineTag from the EquipmentTagList matches the criteria. I'm still confused about why the first one works but not the other one. I know by debugging that the first one returns at least one result while the other returns nothing... I've searched a lot to get help for this error but unfortunately found nothing...
Thanks for helping me out.
EDIT:
The error I get is :
System.InvalidOperationException with Object reference not set to an instance of an object
Assuming for the moment that item.Status is an Integer data type (inferred from your use of TargetRange.OutOfRange as though it's an Enum), the syntax in your second snippet would be expected to fault.
The .Contains() method is reserved for use with IEnumerable objects, not Integer values.
If you modify your code slightly, to this:
Dim badCount = (From item In equipmentTagList
Where item.Importance = "Critique"
Where item.Status.Contains(TargetRange.OutOfRange)
Select item)
...and then set a breakpoint at some point after this call, you'll note that badCount is Nothing. Since Nothing can't have a .Count, the call fails.
Your first snippet is correct—as you've already pointed out.
EDIT
Something's not right here. Your code shouldn't even compile.
Here's what I'm getting:
So item.Status certainly can't be an Integer.
Maybe this will make it a bit easier. You can group by Status after filtering by Importance:
Dim items = From i In equipmentTagList Where i.Importance = "Critique"
Dim counts = items.ToLookup(Function(i) i.Status <> TargetRange.OutOfRange)
Dim goodCount = counts(True).Count()
Dim badCount = counts(False).Count()

VB.Net Dynamically Referencing a Variable or Control

It would take too long to try an explain the actual application I am building.
Lets just say, I have 3 textboxes on my form. I want to set each one of them to a value of the numerical index. This is how I would normally do it.
txt1.Text = "1"
txt2.Text = "2"
txt3.Text = "3"
Now, if I had 100 of these textboxes, I would want to do something more like this.
For i as Integer = 1 to 3
txt[i].Text = i
Next
Is this possible?
This is how I would do it: first, create a list or an array (depending on whether the number of textboxes is fixed or not) and add you textboxes to it:
Dim txtList as new list(of textbox)
txtList.items.add(txtBox1...
This can be done automatically using jmcilhinney's method (with the same caveats):
Dim txtList = New List(Of TextBox)(Me.Controls.OfType(Of TextBox)().ToArray())
Then you can reference it as such:
txtList(i).text
I hope that's what you were asking and that it helps :)

Dictionary returning same value for every key only when value is also an enumerable or custom

i am a noob, so please take this question with that in mind. Here is very simple piece of code:
Sub main()
{
Dim m_Dictionary as new Dictionary(Of Integer, List(Of String))
Dim workingList as new List(Of String)
Dim workingKey as Integer
Dim keyStash as List(Of Integer)
Dim workingDict as new Dictionary(Of Integer, String)
For i=0 to 9
Do
workingKey = RandomInteger()
Loop While workingList.ContainsKey(workingKey)
For n=0 to 4
workingList.Add(RandomString())
Next
keyStash.Add(workingKey)
workingDict.Add(workingKey, workingList)
Next
' now I just want to play back the generators of random data
For each Key As Integer in keyStash
For each Entry as String in workingDict(Key).Value
Line(Entry)
Next
Next
Instead of everything playing back nicely as one might expect, I am left with a fully accurate stash of keys for the dictionary. However, the values for strings inside each list instance are ALL THE SAME FOR EVERY KEY. Those values are equal to the values in the last loop of random data generation. So instead of playing back 50 uniques entries, it writes out 9 times the last loop. I looked inside - everything looks good. Get this. All lists, collections, hash-tables, all of iterated types and also custom types demonstrate this behavior. I found the solution, but it does not explain anything. Can anyone help explaining this, please!??
The variable that keeps the strings generated by RandomString is created outside the loop. Inside that loop you add continuosly new strings to the same instance and add the same list instance to every new integer key. At the end of the loop every integer key added has its value pointing to the same reference of the list. Of course they are identical....
A first fix to your code could be
Dim m_Dictionary as new Dictionary(Of Integer, List(Of String))
Dim workingKey as Integer
For i=0 to 9
' Internal to the loop. so at each loop you get a new list
' to use for the specific key generated in the current loop
Dim workingList as new List(Of String)
Do
workingKey = RandomInteger()
Loop While m_Dictionary.ContainsKey(workingKey)
For n=0 to 4
workingList.Add(RandomString())
Next
m_Dictionary.Add(workingKey, workingList)
Next
For each k in m_Dictionary
For each Entry in k.Value
' Line(Entry)
Console.WriteLine("Key=" & k.Key & " Values = " & Entry)
Next
Next
Please, remember to use Option Strict ON, the current code treats quietly strings as if they were numbers, and this is not a good practice. Option Strict ON will force you to think twice when you work with different type of data.