Number Expected, Got Boolean - dynamic

When I run this code, I get an error message that looks like this:
Runtime Error:
AI.lua:7: bad argument #2 to 'getlocal' (number
expected, got boolean)
What can I do to resolve this? I need the Boolean value.
Anything = not nil
Nothing = not Anything
Everything = Anything and not Nothing
Overtaking = Anything and Nothing
God = Everything and Overtaking
Divinity = God and not not God
Better = debug.getlocal(1, Divinity)
x = ""
repeat
i = 32
repeat
i = i + 1
a = string.char(i)
if Better == true then
x = x .. a
else x = x
end
until (i == 126)
until (x == x .. "")
print (x)

First of all, getlocal requires an integer as the second argument and you passing boolean, so it won't work for you. Second, getlocal returns the local variables declared in a certain scope, so that really depends on what your'e trying to do here. I cannot see any locals in your code, so using getlocal won't get you anywhere.
Take a look at getlocal usage.

Related

VB.Net Jumps to a wrong return statement within Select Case

I have a function that tests if a given Value is okay. In this Function different Cases are Handled, 2 of which always Return True or False, the third one only does a Return when the Value is not OK. If the Value is OK in this case, there's a 'Return True' after the Select.
The thing is, after executing that Case block, the debugger doesn't go to End Select but back to a Return Statement in that Case Block.
It's Easy to avoid this by just putting the last "Return True" into said Case-Block. I just wonder why this happens, since i couldn't find any such exceptions in the MSDN page.
Private Function IsValueOK(ByVal Value As String, ByVal Type As Integer) As Boolean
Select Case Type
Case 1 : Return (Value = "True")
Case 3 : Return Value.Contains("|*|True")
Case 2
Dim NominalValue As Double? = ... '10
Dim UpperTolerance As Double? = ... '0.1
Dim lowerTolerance As Double? = ... '-0.1
Dim Digits As Integer = ... '2
If NominalValue.HasValue AndAlso IsNumeric(Value) Then
Dim Part As Double = Math.Round(CDbl(Value), Digits)
If UpperTolerance.HasValue AndAlso Part > Math.Round(NominalValue + UpperTolerance, Digits) Then Return False
If lowerTolerance.HasValue AndAlso Part < Math.Round(NominalValue + lowerTolerance, Digits) Then Return False
Else
Return False
End If
End Select
Return True
End Function
Value is "9.9" and Type is 2.
In this scenario the first if-statement is true So the debugger goes into the If-Block, the second and third If-Statements are False, so no Return-Statement is executed and the debugger correctly jumps into the "End If" Line. But after that, I'd expect it to Jump to End Select. Instead, it jumps to 'Return False' in the Line above.
Edit:
After some further testing with different builds (even clean release builds) this occurs almost randomly. I'm getting the impression, that the compiler is having some instability that is somehow exploited by the Projects context. I'm going with not Mixing Cases that definitely return stuff and Cases that might return stuff.
Edit 2:
In the meantime, I realised that my release build tests have not been representable - That means I found said behaviour only while debugging. This frees the compiler of my premature accusations about instability ^^"
btw: I'm using Visual Studio 2017

VB.net For Each Loop with If Statement Error

When this code is ran, Visual Studio gives the error:
InvalidOperationException was unhandled
List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.
Dim counter As Integer
For Each x In lstWinners.Items
If x = lstWinners.SelectedItem Then
counter += 1
End If
Next
Here's a screenshot:
http://i.cubeupload.com/lIoWDg.png
EDIT:
This can be fixed by adding this line at the beginning:
Dim anything as string = lstWinners.Text
But why does this error happen, and why does this fix it?
When you are going over the list with for each it kind of "locks" the array. A good way around this is to just copy the array for the iteration.
Just use Array.Copy(source, target, target.Length) where your source would be lstWinners.Items and target is an array you declare. Then do your for each loop on the array. Something like:
Dim counter As Integer
Dim tempcopy(lstWinners.Items.Count) As String
Array.Copy(lstWinners.Items, tempcopy, tempcopy.Length)
For Each x In tempcopy
If x = lstWinners.SelectedItem Then
counter += 1
End If
Next
Perhaps it is seeing the = operator as the assignment operator instead.
Try this code instead:
Dim counter As Integer
For Each x In lstWinners.Items
If x Is lstWinners.SelectedItem Then
counter += 1
End If
Next
Is explicitly compares equality on objects, so it removes any potential ambiguity.
Assuming that your items are strings and you want to count the items with the same text then you could use
Dim counter As Integer
Dim x = lstWinners.SelectedItem.ToString()
counter = lstWinners.Items.Cast(Of String).Count(Function(z) z = x)
However, your code should not give that error unless there is something else that is running and modify your list (a separate thread?)

understanding VB code

i m working on translating code from VB to C#, though there are lots of great conversion websites, i still find the code ambiguous to me, since the documentation is really poor it approaches to useless, i thought about posting it here to see if i can get it clearer.
i need some elaboration on the code given below, and whats the use of the Buffer[] in the method below:
Function hexToBin(ByVal str As String, ByRef Buffer() As Byte)
Dim strRemain As String
Dim firstChar As Boolean
Dim i, count, inputLen, remainLen As Integer
i = 0
count = 0
firstChar = True
strRemain = str
While Len(strRemain) > 0
If Mid(strRemain, 1, 1) = " " Then
firstChar = True
strRemain = Mid(strRemain, 2)
ElseIf firstChar = True Then
If Len(strRemain) = 1 Then
Buffer(count) = myVal(strRemain)
ElseIf Len(strRemain) >= 2 Then
Buffer(count) = myVal(Mid(strRemain, 1, 1)) * 16 + myVal(Mid(strRemain, 2, 1))
strRemain = Mid(strRemain, 3)
End If
count = count + 1
firstChar = False
Else
strRemain = Mid(strRemain, 2)
End If
Wend
hexToBin = count
End Function
see, i know this code converts from hex to binary as the name suggests, yet i cant really figure the use of the Buffer[] in the context, i looked up the Mid function in string VB, but still can't figure out the use of the Buffer[] in this function, i would appreciate if someone explained the use of the buffer.
In the code above, the parentheses are used to access elements of an array. So, Buffer(i) refers to the ith element of the array Buffer.
You can learn more about arrays in VB from any text book, or indeed from MSDN: http://msdn.microsoft.com/en-us/library/wak0wfyt.aspx
As for why the array Buffer is used in the first place, well that's to store the output of the function. The function takes a hex string as input and populates the byte array Buffer with the binary equivalent.
This does seem to be rather inefficient code though. And it presents a somewhat clumsy interface because it asks the caller to allocate the array. Rather than translating it, I think I would start here: How can I convert a hex string to a byte array?

Understanding assignment/comparison vb.net

This is my first time on Stack Overflow and I am trying to understand what '=' means in the last line of this code:
Dim label As Label = Me.labels.Item(String.Concat(New Object() { movimiento.Sector1.ID, "-", movimiento.X1, "-", movimiento.Y1 }))
Dim dictionary As Dictionary(Of Label, Integer)
Dim label3 As Label
dictionary = Me.demandas2.Item(label3 = label) = (dictionary.Item(label3) - 1)
Any kind of help will be welcome, thanks in advance!
The equals sign (=) is used for two entirely different operators in VB.NET. It is used as the assignment operator as well as for the equality test operator. The operator, to which the character evaluates, depends on the context. So, for instance, in this example:
Dim x As Integer = 1
Dim y As Integer = 2
Dim z As Integer = x = y
You might think, as in other languages, such as C#, that after executing that code, x, y, and z would all equal 2. However, VB treats the second equals sign as an equality test operator. Therefore, in actuality, it's doing this:
If x = y Then
z = True
Else
z = False
End If
You'll notice, though, that we are then trying to assign a boolean value to an integer variable. If you have Option Strict On (as you should), it would not allow you to do that. If that's really what you wanted to do, it would force you to cast it to an integer, which makes it slightly more obvious:
z = CInt(x = y)
However, it's still confusing, so typically, this kind of thing is discouraged in VB.NET. So, I suspect that the code you posted wouldn't even compile if Option Strict was turned on. But, this is what it's actually trying to do:
Dim temp1 As Boolean = (label3 = label) ' Evaluates to False
Dim temp2 As Boolean = (Me.demandas2.Item(temp1) = (dictionary.Item(label3) - 1)) ' Likely evaluates to False
dictionary = temp2 ' Couldn't possibly be a valid assignment
Let's look at this line of code:
dictionary = Me.demandas2.Item(label3 = label) = (dictionary.Item(label3) - 1)
The first = is an assignment. So we assign the right part to the dictionary. Now for the right part:
Me.demandas2.Item(label3 = label) = (dictionary.Item(label3) - 1)
The = between the two expressions is a comparison, so it returns a Boolean. So the supposed "dictionary" is assigned a boolean value. If we check the left part of that expression:
Me.demandas2.Item(label3 = label)
Once again, the = sign here is doing a comparison, so if label3 is the same as label, then the code would be equivalent to Me.semandas2.Item(True). This seems strange.
Overall, this code doesn't make much sense, and I'd be surprised if it compiled, considering it tries to assign a boolean to a dictionary. It certainly wouldn't compile with Option Strict On.
Thanks a lot, everyone. The snippet was result of decompile a dll. I was trying to help a partner.
.Net reflector decompiled based on VB.Net code, that was a mistake.
Finally we see that first it should decompile using C# code, that gives a complete different meaning to the code:
if (movimiento.Contenedor.Demanda2)
{
Dictionary<Label, int> dictionary;
Label label3;
(dictionary = this.demandas2)[label3 = label] = dictionary[label3] - 1;
if (this.demandas2[label] == 0)
{
label.ForeColor = Color.Black;
}
(dictionary = this.demandas2)[label3 = label2] = dictionary[label3] + 1;
label2.ForeColor = Color.DarkOrange;
}

Error Reading string into array

I'm in the process of creating a tile based game. This game requires a method to load a text file and write the numbers between the delimiter "-", to a multidimensional array. However, an error message "object not set to an instance occurs.
'Load map
Public Sub LoadMap(ByVal URI As String)
Using reader As New System.IO.StreamReader(URI)
For x As Integer = 0 To 13
Dim line = reader.ReadLine()
Dim tokens() As String = line.Split("-")
'adds values to multidimensional array
For y As Integer = 0 To 16
Me.map(x, y) = Integer.Parse(tokens(y))
Next y
Next x
End Using
End Sub
Example map - numbers represent image id's
2-2-2-0-0-0-0-0-0-0-0-3-3-5-5-5-5
2-2-2-0-0-0-0-0-0-0-0-3-3-5-5-5-5
2-2-2-0-0-0-0-0-0-0-0-3-3-2-2-2-5
2-2-2-0-0-0-0-0-0-0-0-3-3-2-2-2-5
2-2-2-0-0-0-0-0-0-0-0-3-3-2-2-2-5
0-0-0-0-0-0-0-0-0-0-0-3-3-2-2-2-5
3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3-3
4-4-4-4-4-3-4-4-4-4-4-4-4-2-2-2-2
4-4-4-4-4-3-4-4-4-4-4-4-4-2-2-2-2
4-4-4-4-4-3-4-4-4-4-4-4-4-2-2-2-2
4-4-4-4-4-3-4-4-4-4-4-4-4-2-2-2-2
4-4-4-4-4-3-4-4-4-4-4-4-4-2-2-2-2
4-4-4-4-4-3-4-4-4-4-4-4-4-2-2-2-2
4-4-4-4-4-3-4-4-4-4-4-4-4-2-2-2-2
I can't seem to establish the problem. Thanks in advance...
Always use Option Strict On! (Your code shouldn’t even compile, you need to parse the input integers, or your map stores strings, which is equally as bad since you logically store numbers.)
Name your variables properly.
Omit vacuous comments. Only comment things that aren’t obvious from the code.
Don’t hard-code magic numbers (what’s 11? 8?)
Don’t declare variables before initialising. Declare on first use.
The outer loop in your code makes no sense.
Don’t close streams manually, always use a Using block to ensure the program still works in the face of exceptions.
Initialise map.
Which leaves us with:
Public Sub LoadMap(ByVal URI As String)
Const MapHeight As Integer = 12
Const MapWidth As Integer = 9
Me.map = New Integer(MapHeight, MapWidth) { }
Using reader As New System.IO.StreamReader(URI)
For x As Integer = 0 To MapHeight - 1
Dim line = reader.ReadLine()
Dim tokens() As String = line.Split("-")
For y As Integer = 0 To MapWidth - 1
Me.map(x, y) = Integer.Parse(tokens(y))
Next y
Next x
End Using
End Sub
Bonus: Check for errors: what if the map doesn’t have the predefined width/height? Why hard-code this at all?
osRead.ReadLine() returns Nothing if the end of the input stream is reached. You call Peek to see if you're at the end of the input, but then, you proceed to read 12 lines without checking if you're at the end of the input in-between. If you have less than 12 more lines, you'll get the error you mentionned on temp.Split("-"), because temp will have a value of Nothing, so you can't call methods on it.
Also, just noticed something... your Map is 11x8, but you're reading 12 lines, and going through 9 values, you probably want to do:
For x As Integer = 0 To 10
or
For x As Integer = 1 To 11
Same thing for your other loop.
If temp is null (Nothing) in VB.Net, then you can't call methods on it.
Do a check for Nothing before attempting to do anything with the value.
So Just to be sure you have updated your code to look something like this:
If temp IsNot Nothing
'tempLine stores the split read line
Dim tempLine() As String
'splits readline into - ERROR Not set to an instance
tempLine = temp.Split("-")
'adds values to multidimensional array
For y As Integer = 0 To 8
Me.map(x, y) = tempLine(y)
Next y
End If
And you are STILL getting the null reference exeption?
also make sure that your StreamReader is properly initialized... if the initialization fails (perhaps because of a bad URI) then attempting to call osRead.peek() will throw the "object not set to an instance" error.