Creating parallel multiple tasks with variable parameter [duplicate] - vb.net

This question already has answers here:
Is there a reason for C#'s reuse of the variable in a foreach?
(4 answers)
Starting Tasks In foreach Loop Uses Value of Last Item [duplicate]
(2 answers)
Closed 5 years ago.
This is a cumbersome behaviour when creating several task in a for loop:
Private Sub DoSomeTasks()
For i = 1 to 3
Dim oTest as cTest = New cTest
Dim oTask As Task = Task.Factory.StartNew(Sub() oTest.DoSimulation(i))
Next
End Sub
This code will create all tasks with the same i value (= 3). Anyone has experienced something similar? Anyone has a clue on how to solve it?

Related

How to overcome "Index outside the bounds of array"? [duplicate]

This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
IndexOutOfRangeException in VB.NET
(2 answers)
What does IndexOutofRangeException mean?
(3 answers)
“IndexOutOfRangeException was unhandled” [duplicate]
(2 answers)
Closed 4 years ago.
I have this code:
Module Module11
Sub Main()
Dim mess As String
Dim out As String
Dim num, p As UInteger
Dim q As Integer = -1
Console.Write("Enter a message: ")
mess = Console.ReadLine()
While p < 9
q += 1
num = Asc(mess(p + q)) + 5
out = Chr(num)
Console.Write(out)
End While
Console.ReadKey()
End Sub
End Module
But on Line num = Asc(mess(p + q)) + 5, it is showing 'index out of the bounds of array'.
I was actually trying to create code that could change every character (even blank space) to the next sixth character (with reference to ASCII codes) of whatever character we input.
It shows error even after giving the correct output (in the black console).
Please help.
I think I have the answer (or at least AN answer). I'm not familiar with visual basic, but I am familiar with Python and this seems to be extremely similar. After doing some quick researching, I learned that the "UInteger" data type cannot be negative. I would recommend changing the while loop to read "While p > 0 And p < 9" and then the rest of the code. It looks to me like your answer is coming out negative. This would make sense considering what your error message says. However, I'm not sure how the output would still come out correctly, but it's worth a shot. I would try that, and let me know how it goes!

Wend without While not working (no if/else/endif present) [duplicate]

This question already has answers here:
Break out of a While...Wend loop
(4 answers)
Closed 5 years ago.
I can't get the While/Wend loop to work at all. Even the tiniest possible case, such as below:
Sub TestingWhile()
Dim i As Integer
i = 0
Do While i < 10
i = i + 1
Wend
End Sub
The result of that code is an error message window saying "Compile error: Wend without While".
I found people having this error, but only because they always had some wrong if/else statements. This obviously is not the case. What's going on?
By the way, I'm using Microsoft Visual Basic for Applications 7.1 while in Excel 2013.
EDIT - Question solved. Syntax, beginner mistake: Do While is closed by Loop, and While is closed by Wend
Do While is closed by Loop and While is closed by Wend :
Sub TestingWhile()
Dim i As Integer
i = 0
Do While i < 10
i = i + 1
Loop
End Sub

How to check a image byte array is empty in VB.Net? [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 6 years ago.
Hello I need to check if a variable which loads a photo is empty. It's define as Byte(). IDE throwing error when I do this - If Photo.Length = 0 then. I really need your help. Thanks.
Perhaps you need to perform a null check on the array before you attempt to access any properties. You'll need to post more code if you want better help.
If Photo IsNot Nothing AndAlso Photo.Length > 0 Then
'Photo isn't empty
End If

IF Without End IF in VBA [duplicate]

This question already has answers here:
IF statements in VBA
(5 answers)
Closed 7 years ago.
see the code below.
IF Weight=0 Then CallFunction
IF Weight=100 Then
Heavy=True
Else
Heavy= False
End If
How is the program working when I have not ended a if statement?
its VBA's functionality to end up if statements in a single line..
If Weight is equal to zero, then CallFunction is executed (if Weight is not equal to zero, a CallFunction is not executed), then the code is going to another if statement. That's all! Debug the program to find out ;)

Is there a way to refer to the item "With" which I am working? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to access the object itself in With … End With
Suppose I would like to do something like this:
With grid.Columns("Reviewed")
CType(<the with object>, DataGridViewCheckBoxColumn).ThreeState = False
...
End With
Basically, I would like to have the equivalent of the following in the above statement:
CType(grid.Columns("Reviewed"), DataGridViewCheckBoxColumn).ThreeState = False
Is there some keyword that I can use to refer to the object that I am "WITH"?
To my knowledge there is no way to do exactly that, but depending on what you are going to be doing it might just be easier to make another variable to reference that column.
Dim ReviewColumn as DataGridViewCheckBoxColumn = grid.Columns("Reviewed")
With ReviewColumn
.ThreeState = False
''etc
End With
Honestly I wouldn't even use the With block at that point.