Error code using Linq to add to a variable - vb.net

I'll start with the problem, and then describe how the procedure works.
I'm getting a an error saying:
Value of type 'System.Collections.Generic.IEnumerable(Of LunchMoneyGame.LunchMoneyMainForm.Group)' cannot be converted to 'LunchMoneyGame.LunchMoneyMainForm.Group'.
The blue underline pinpointing the problem is on Dim obj As LunchMoneyGame.LunchMoneyMainForm.Group = From r In temp Where r.ID = Number Select r on the right side of the operator.
I know it is something to do with explicitness
--
This is for a table top card game i am converting to a text game.
The goal of this sub procedure is to subtract a card from a players hand and add it to the discard pile. Once added to the discard pile I then call the discard pile to move the cards from the discard pile to the card deck.
The problem starts with the number generator.
What I'm doing is randomly picking a card from the deck. by generating a the card id of a card in the deck. Once the ID is generated using linq the number generated then takes a card, then adds the card to the players inventory (quantity integer), and subtracts from the deck.
T represents whose turn it is. Currently for testing purposes I have coded it for only 2 players in terms of the T variable.
Private Sub GrabFromDeckAndDiscard()
Dim CardCheckBoxInteger As Integer
'ReDeclare CheckBox Array for Private sub
Dim CardCheckBoxArray() As CheckBox = {CardCheckBox1, CardCheckBox2, CardCheckBox3, CardCheckBox4, CardCheckBox5}
'Discard
Select Case T
Case 0
Player1HandGroup(NumberArray(Checks)).QuantityInteger -= 1
Case 1
Player1HandGroup(NumberArray(Checks)).QuantityInteger2 -= 1
Case 2
Player1HandGroup(NumberArray(Checks)).QuantityInteger3 -= 1
Case 3
Player1HandGroup(NumberArray(Checks)).QuantityInteger4 -= 1
Case 4
Player1HandGroup(NumberArray(Checks)).QuantityInteger5 -= 1
End Select
'Add Card to Discard Pile
DiscardGroup(NumberArray(Checks)).QuantityInteger += 1
'Shuffle Deck from Discard Pile if Deck is out of cards
Call DiscardPile()
'Reset Number Generator, unless weapon isn't discard
Dim temp As IEnumerable(Of LunchMoneyGame.LunchMoneyMainForm.Group) = From r In DeckGroup Where r.QuantityInteger > 0 Select r
If temp IsNot Nothing AndAlso temp.count > 0 Then
Number = (temp(Rnd.Next(0, temp.Count)).ID)
' ** Edit **: This will give you the actual object to be manipulated
Dim obj As LunchMoneyGame.LunchMoneyMainForm.Group = (From r In temp Where r.ID = Number Select r).Single
Dim PlayerQuantitySubtractionInteger As Integer
For PlayerQuantitySubtractionInteger = ChecksDynamicA To ChecksDynamicB
' ** Edit **
obj.QuantityInteger -= 1
'Select the Player depending value of T
Select Case T
Case 0
Player1HandGroup(Number).QuantityInteger += 1
Case 1
Player1HandGroup(Number).QuantityInteger2 += 1
Case 2
Player1HandGroup(Number).QuantityInteger3 += 1
Case 3
Player1HandGroup(Number).QuantityInteger4 += 1
Case 4
Player1HandGroup(Number).QuantityInteger5 += 1
End Select
CardTypeArray(PlayerQuantitySubtractionInteger) = Player1HandGroup(Number).CardType
CardCheckBoxArray(TextBoxInteger).Text = Player1HandGroup(Number).CardNameString
NumberArray(PlayerQuantitySubtractionInteger) = Number
Next PlayerQuantitySubtractionInteger
End If
'Switch to next player
Select Case T
Case 0
For CardCheckBoxInteger = 0 To 4
CardCheckBoxArray(CardCheckBoxInteger).Text = Player1HandGroup(NumberArray(CardCheckBoxInteger + 5)).CardNameString
Next
T += 1
Case 1
If GameSize = 2 Then
For CardCheckBoxInteger = 0 To 4
CardCheckBoxArray(CardCheckBoxInteger).Text = Player1HandGroup(NumberArray(CardCheckBoxInteger)).CardNameString
Next CardCheckBoxInteger
T -= 1
End If
If GameSize > 2 Then
T += 1
End If
Case 2
Case 3
Case 4
End Select
Label1.Text = T.ToString
'Clear Check Boxes when turn is finished
For CardCheckBoxInteger = 0 To 4
CardCheckBoxArray(CardCheckBoxInteger).Checked = False
Next
'Turn off play button
PlayButton.Enabled = False
End Sub

From r In temp
Where r.ID = Number
Select r
Returns a collection of everything which matches the Where clause (see this for the MSDN documentation). .NET represents this as an IEnumerable, which means a collection of things which can be iterated over (e.g. using a For loop or something like that).
There is no automatic concept that you'll only get one record returned even though your condition is an = (there may also be conditions, broadly, where no elements are returned such as when nothing matches the equality condition or where multiple things match the equality condition).
In order to indicate that you only want one element you can use the First, Single, FirstOrDefault or SingleOrDefault methods.
For example:
Dim obj As LunchMoneyGame.LunchMoneyMainForm.Group = (From r In temp Where r.ID = Number Select r).Single()
Which will give you an object of type LunchMoneyGame.LunchMoneyMainForm.Group, which I think you're looking for in this case.
As an aside, it's not bad to know the difference between the methods I've outlined above. In my lay perspective (as I'm no expert):
First returns the first item in the collection or throws an Exception if the collection is empty
FirstOrDefault returns the first item in the collection or null if empty
Single returns the only item in the collection or throws an Exception if the collection does not have exactly one item
SingleOrDefault returns the first item in the collection, null if the collection is empty and throws an exception if the collection has more than 1 element
So in your case, since you should only match on a single item with the = condition I would suggest using Single or SingleOrDefault
PS. Sorry, when I say null above, that'll be Nothing in VB.NET speak

Related

I'm trying to sum the number of rows that are not nothing. IF the row count is 2, but LastNAme (row 2) is Nothing, i need to return 1

I'm trying to get an accurate count of borrowers on a loan. The system I'm working in automatically creates 2 rows for each application we enter. I want to return the sum of rows that are not nothing.
I'm looking at the borrower last name. So if we have two applications on the loan file, our system automatically creates 4 rows. However, the borrower last name field in row 4 is nothing. I want to return 3 since the borrower last name field in rows 1, 2 and 3 is not nothing
I'm fairly new to VB.NET and am trying to use a For Loop to count each row when the last name is not nothing.
Dim i As Integer
Dim BrwrCT = GetFieldRowCount(144)
For i = 1 To BrwrCT
If GetFieldValue(144,i) IsNot Nothing
Return 1 + BrwrCT
Else Return BrwrCT
End If
Next i
For this example, the loan file has 3 borrowers and the result should be returning 3, however, I am receiving 5 (BrwrCT + 1)
In your For/Next loop you set the max loop to be BrwrCT and then inside of the loop you are modifying the value of that same variable BrwrCT. You should avoid modifying the loop range inside of the loop doing that range. You should also not use the Return statement inside of the loop unless you actually want to leave that loop immediately without performing all of the iterations of that loop. In your sample code, this loop would only ever execute once and then immediately exit (aka Return).
I recommend creating a second variable that will have the count you are looking for:
Dim i As Integer
Dim BrwrCT = GetFieldRowCount(144)
Dim totalBorrowers As Integer
For i = 1 To BrwrCT
If GetFieldValue(144,i) IsNot Nothing Then
totalBorrowers += 1
End If
Next i
Return totalBorrowers

check if string contains specific integer

I have a grid view that has a column containing strings (Middle column).
On the rowDataBound event I want to loop through the column looking for the integer it contains and then display a value in the first column.
I know that the integer range will be 1 to 63 so I can use a FOR loop to loop through the numbers. Here is what I have so far.
For x As Integer = 1 To 63
If CType(e.Row.Cells(2).FindControl("lblTagName"), Label).Text Then
End If
Next
The problem I am having is using contains. I cant use the following as it would also be true for the number 1, 10, 11 etc when x = 1.
For x As Integer = 1 To 63
If CType(e.Row.Cells(2).FindControl("lblTagName"), Label).Text.Contains(x) Then
End If
Next
How do I make sure it only gets one result per number? i.e x = 6 would return UMIS.75OPTR6GROSSMARGIN.F_CV and not all the other strings that contain the number 6.
UPDATE - based on some answers I may not of explained this very well. I want to loop through the gridview and if the number 1 is found and only the number 1 in the second column, not 10 etc then I want to display "Run 1" in the first column. So when x = 10 it will show "Run 10" and so on.
UPDATE 2 - its definatley my explanation, apologies.
The resultant grid view would look like this.
The order of the second column is not set and is not in order.
You'd have to check the entire text of the label to determine whether it is only 1, and not 10, 11, 12, 13, ... as well.
Also, in this case you should use DirectCast rather than CType. CType is only used when converting to different types that include conversion operators, here you are always dealing with a label.
For x As Integer = 1 To 63
If String.Equals(DirectCast(e.Row.Cells(2).FindControl("lblTagName"), Label).Text, "UMIS.75OPTR" & x & "GROSSMARGIN.F_CV", StringComparison.OrdinalIgnoreCase) Then
'Do your stuff.
End If
Next
You might want to think if doing it the other way around. Get the list of numbers in your string with a regular expression match.
Dim s As String = "asd12asdasd.sdf3sdf"
For Each m As System.Text.RegularExpressions.Match In System.Text.RegularExpressions.Regex.Matches(s, "[\d]*")
If m.Success AndAlso Not String.IsNullOrEmpty(m.Value) Then
' m.Value
End If
Next
With that list of number, you can check if it's between 1 and 63.
If your string have the same suffix/prefix, just remove them to show you what the number is.
Dim s As String = "UMIS.75OPTR12GROSSMARGIN.F_CV"
Dim number As String = s.Replace("UMIS.75OPTR", "").Replace("GROSSMARGIN.F_CV", "")
Go backwards in a Do Until Loop:
Dim bolFoundMatch As Boolean = False
Dim intCursor As Integer = 63
Do Until (bolFoundMatch OrElse intCursor = 0)
If CType(e.Row.Cells(2).FindControl("lblTagName"), Label).Text.Contains(intCursor) Then
'Anything you want to do when you find your match.
'This will ensure your loop exits.
bolFoundMatch = True
End If
intCursor -= 1
Loop

InvalidArgument=Value of ' ' is not valid for 'index' (' ' inside number)

This is my first question. I can't solve this error for 2 weeks.
In order to solve the problem signed up.
This is my vb code.
Try
For i As Integer = 0 To ListBox1.Items.Count - 1 Step 1
For j As Integer = 0 To ListBox2.Items.Count - 1 Step 1
If ListBox1.Items(i).ToString().Equals(ListBox2.Items(j).ToString()) = True Then
ListBox1.Items.RemoveAt(i)
End If
Next
Next
Catch ex As Exception
MsgBox("LOAD ERROR: " + ex.Message, vbCritical, "ERROR")
End Try
error :
InvalidArgument=Value of '20' is not valid for 'index'(' ' is varient.)
Project has no problems except for this error
Try this:
Dim items = ListBox1.Items.Where(Function(item) ListBox2.Items.Contains(item)).ToList()
For Each item in items
ListBox1.Remove(item)
Next
When I run your code, I receive a different exception, argument out of range...and that is caused by deleting items from an indexed collection while you're iterating through it. For example, let's say listbox1 has 10 items in it. If you find item number 1 in listbox2 and delete it, now you only have 9 items left in listbox1. The problem is, when you entered your loop, you told it to loop 10 items, and it will still try to do that. At some point, if any items are deleted, this loop will throw an exception...so you will need to change that sooner or later. To mitigate this, step through the collection that you'll be deleting items from backward like this:
For i As Integer = ListBox1.Items.Count - 1 to 0 Step -1
When I run the code with the change shown above, it works as intended and removes the duplicate items from listbox1. Unfortunately, I was unable to reproduce your invalid argument exception. It's odd to see that because usually that exception likes to pop up when using listviews, not listboxes. Perhaps you can edit your post and add a screenshot of the data in your listboxes so it's easier for other people to troubleshoot.
As you remove items from ListBox1 the total item count will decrease (obviously), however the For loop does not respect that. A For loop will only have the right side of To set once, which is done prior to the first iteration.
What you're currently doing is actually equal to this:
Dim a As Integer = ListBox1.Items.Count - 1
For i As Integer = 0 To a Step 1
Dim b As Integer = ListBox2.Items.Count - 1
For j As Integer = 0 To b Step 1
...
Next
Next
The fix for this is simple; create a variable that holds how many items you have removed, then, in an If-statement, check if i is more or equal to the current item count subtracted with how many item's you've removed. If so, exit the loop.
Dim ItemsRemoved As Integer = 0
For i As Integer = 0 To ListBox1.Items.Count - 1 Step 1
If i >= ListBox1.Items.Count - ItemsRemoved Then Exit For
For j As Integer = 0 To ListBox2.Items.Count - 1 Step 1
If ListBox1.Items(i).ToString().Equals(ListBox2.Items(j).ToString()) = True Then
ListBox1.Items.RemoveAt(i)
End If
Next
Next
For future reference you should also always remove/comment out the Try/Catch-statement so you can see where the error occurs and get more detail about it.
The point of my answer is that when you iterating any collection, you should NOT try to modify this collection. In for-loops you run into such trouble. But you can iterate using while-loop with no issues
Try
Dim index As Integer = 0
While index < ListBox1.Items.Count '!! this code based on fact that ListBox1 item Count changes
For j As Integer = 0 To ListBox2.Items.Count - 1 ' <- this is ok because ListBox2 doesn't chage
If string.Equals(ListBox1.Items(index).ToString(), ListBox2.Items(j).ToString()) Then
ListBox1.Items.RemoveAt(index)
Continue While ' no index increase here because if you remove item N, next item become item N
End If
Next
index += 1
End While
Catch ex As Exception
MsgBox("LOAD ERROR: " + ex.Message, vbCritical, "ERROR")
End Try
This is good example of how things actually work. And it shows few techniques
I just selected Build-->Clean solution and it cleaned out the bad elements. This occurred as a result of adding and deleting menu items, without deleting the subroutines of the deleted menu items. As soon as I cleaned the solution, and then ran the project, the error was gone.

How Do I Count When Numbers Do Not Show Up

I have numbers from 1 to 25, four numbers will show up daily. I need to put a +1 on each of the four numbers and need to put a -1 on each of the 21 numbers didn't show up.
The four numbers that come up daily will be inputted in four different text boxes. The count being positive or negative needs to go on 25 separate text boxes labeled 1 thru 25.
I have tried "if textbox <> number, then count -= 1" but I get a count of -4 because it doesn't see the number in any of the four text boxes.
I only need a daily count not a textbox count. Sorry I don't have any code started and would greatly appreciate if someone can point me in the right direction. I'm doing this on Visual Studio 2012.
Thank you all for responding. Here is some code I've started but the count is not correct. My four input text boxes are in GroupBox2. Four numbers from 1 to 25 will draw daily like a lottery. The four numbers drawn will have a value of +1 each all others -1. I need to find the age of each number 1 thru 25. If a number has a +3 then that means that number has drawn 3 consecutive days. If a number has a -15 then that means that number has not drawn for the past 15 days.
Dim tb As New TextBox
Dim ctrl As Control
Dim Counter As Integer
For Each ctrl In GroupBox2.Controls
tb = ctrl
If tb.Text = 1 Then
Counter += 1
ElseIf tb.Text <> 1 Then
Counter -= 1
TextBox464.Text = Counter
End If
If tb.Text = 2 Then
Counter += 1
ElseIf tb.Text <> 2 Then
Counter -= 1
TextBox463.Text = Counter
End If
If tb.Text = 3 Then
Counter += 1
ElseIf tb.Text <> 3 Then
Counter -= 1
TextBox462.Text = Counter
End If
If tb.Text = 4 Then
Counter += 1
ElseIf tb.Text <> 4 Then
Counter -= 1
TextBox461.Text = Counter
End If
Next
We will need more information about how your going to approach it to be able to help you further, but as for your problem with this If Textbox <> number Then count -= 1 you can use something like this since your only going to be having numbers on the textboxes If Cint(Textbox.Text) <> number then count -= 1 since your using just Textbox its attempting to evaluate it as a control and not the property that your looking for, you need to read from its .Text Property, However since its evaluated as a String and not an Integer it will throw an error exception, thats why the Cint() is included (This may also be used to convert it to integer Ctype(number, Integer) Not sure if there is an execution speed difference or not, however Cint() is a faster way of writing it.) it will try and convert the String into an Integer and when its converted into an integer it can be evaluated like one using <>. No one is going to write a whole solution out for you, but while you attempt to create it yourself and more information can be added we are more than happy to help you with problems along the way.

Need help on figuring out dice game scoring logic

So, we are creating a game in VB.net based off the game, Farkle. Basically, you roll an amount of dice and depending on what you keep is your score. We have eight dice. Here's an example, say you roll 3 "3"s and you want to score them, a three of a kind. We want to check all the dice together to see if we do have three 3's. We have figured out the two of a kind, but the three of a kind we cannot.
For l = 0 To 5
For o = 1 To 6
For q = 2 To 7
If (DieScore(l) = DieScore(o) And DieScore(l) = DieScore(q)) Then
PlayerScore = 200 + PlayerScore
End If
Next
Next
Next
This is our checking for three of a kind on the dice. If it is true we add 200 to the score. The DieScore(x) refer to the dice. Where are we going wrong?
You really just need to loop and count how many times that number of pips (spots) appears in the array.
Dim count As Integer = 0
For pips As Integer = 1 To 6
count = 0 ' reset count for each Pip iteration
For n As Integer = 0 To DieScore.Length - 1
' If operator version:
count += If(DieScore(n) = pips, 1, 0)
' If Statement version:
'If DieScore(n) = pips Then
' count += 1
'End If
Next
' scoring
Select Case count
Case 2 ' pair
playerscore += 50
Case 3 ' trips
playerscore += 200
If pips = 6 Then
playerscore += 25 ' extra bonus for 666 (example)
End If
Case 4 ' quads
playerscore += 350
' etc
End Select
Next
Not for nothing, but these kinds of logic issues are easy to find using the Debugger. Plus, you will learn a lot about how code executes.
For starters, please learn to use different, more descriptive variable names than l and o, that are easily confused with 1 and 0. Some famous bugs have been caused by doing things like that.
One thing you can do is simply count how many dots or pips there are in a roll of the dice and store that in an array.
' index 0 = 1 spot, 5 = 6 spots.
Dim pipsCount(6) as Integer
' This counts the number of dice for each possible "pips"
For dieIndex as Integer = 0 To DieScore.Length - 1
' Increment (the -1 is because index starts at 0)
pipsCount(DieScore(dieIndex)-1) += 1
Next
Now that you have the number of dice that landed headsup with a given number of pips, you can do a number of different things with that.
' You can easily find out now how many sixes were thrown:
Dim numberOfSixes As Integer = pipsCount(6-1)
' Or score pairs, trips, quads, ...
For pipsIndex As Integer = 0 To 5
Select Case pipsCount(pipsIndex)
Case 2
PlayerScore += 50
Case 3
PlayerScore += 200
' ... etc
End Select
Next
' Or count the length of a straight
Dim straightLength As Integer = If(pipsCount(0) > 0, 1, 0)
Dim longestStraight As Integer = straightLength
For pipsIndex As Integer = 1 To 5
If pipsCount(pipsIndex) > 0 Then
straightLength += 1
Else ' straight ended
If straightLength > longestStraight Then
longestStraight = straightLength
End If
straightLength = 0
End If
Next