What does += mean in Visual Basic? - vb.net

I tried to google the answer for this but could not find it. I am working on VB.Net. I would like to know what does the operator += mean in VB.Net ?

It means that you want to add the value to the existing value of the variable. So, for instance:
Dim x As Integer = 1
x += 2 ' x now equals 3
In other words, it would be the same as doing this:
Dim x As Integer = 1
x = x + 2 ' x now equals 3
For future reference, you can see the complete list of VB.NET operators on the MSDN.

a += b
is equivalent to
a = a + b
In other words, it adds to the current value.

It is plus equals. What it does is take the same variable, adds it with the right hand number (using the + operator), and then assigns it back to the variable. For example,
Dim a As Integer
Dim x As Integer
x = 1
a = 1
x += 2
a = a + 2
if x = a then
MsgBox("This will print!")
endif

those 2 lines compiled produce the same IL code:
x += 1
and
x = x + 1

Just makes code more efficient -
Dim x as integer = 3
x += 1
'x = 4
is the same as
x = x + 1
'x = 4
It can also be used with a (-):
x -= 1
' x = 2
Is the same as
x = x - 1
'x = 2

Related

Simple program to return all factors of a given input integer

I am working on a simple program to return all factors of a given input integer. factors of 32 with while loop array Unfortunately I am stuck.
Code
Dim x As Integer
x = txtInput.Text
Dim factor As Integer
factor = CInt(txtInput.Text) - 1
Dim i As Integer
i = 1
While factor > 0
Do Until i = x
If factor * i = x Then
ListBox1.Items.Add(factor)
i = i + 1
Else
i = i + 1
End If
Loop
factor = factor - 1
End While

For Loop running error

This code doesn't find the correct output
for say n= 1 (although it gives the correct output for say n= 2,3,4..etc.)
if we put n= 1 to find x then the i loop will continue from 1 to 0, hence the first term in x should vanish and leftover should be the second term 5; but it gives 0 ?
Is there any limitation on the input n to run the for loop ?I would appreciate any help.
Function math(n As Integer) As Double
Dim i As Integer
Dim x As Double
For i = 1 To n - 1
x = (n - 1) * 2 + 5
sum = sum + x
Next i
math = sum
End Function
Why not simply:
Function math(n As Integer) As Double
Math = ((n - 1) * 2 + 5) * Abs((n - 1) - (n = 1))
End Function
???
if the answer is correct then Math = (n * 2 + 3) * Abs((n - 1) - (n = 1)) would be easier to understand and make much more sense
In the for loop, if you don't precise the Step, the variable will only increment by 1.
And here, you start at 1 to go to 0, so the loop won't execute, you need to test n to cover both cases :
Function math(n As Integer) As Double
If n < 0 Then Exit Function
Dim i As Integer
Dim x As Double
Dim Summ As Double
Select Case n
Case Is > 1
For i = 1 To n - 1
x = (i - 1) * 2 + 5
Summ = Summ + x
Next i
Case Is = 1
Summ = (n - 1) * 2 + 5
Case Is = 0
Summ = 5
Case Else
MsgBox "This case is not supported", vbInformation + vbOKOnly
Exit Function
End Select
math = Summ
End Function
If n = 1, you end up with For i = 1 To 0 which is incorrect and
should be expressed For i = 1 To 0 STEP -1.
So I suggest you add the STEP BYand make sure it is either 1 to -1 depending on N.

Randomly divide a whole number m into n parts such that the parts are whole numbers and each part lies between x and y

As an example. I want to randomly hand out 100 chocolates to 25 kids. I cannot give any kid more than 10 chocolates.
So here m = 100, n = 25, x = 1 and y = 12.
I have checked these questions.
Dividing a number into m parts uniformly randomly
Dividing a number into random unequal parts
They do give some idea but in these questions x and y are not specified.
So basically,
1) Total No. of Chocolates = 100
2) I can only give minimum 1 and maximum 12 chocolates to each kid
3) Chocolates should be distributed between 25 kids
4) I do not want any distribution (uniform or normal) - it should be purely random. (I am willing to exclude this condition if all else fails.)
Private Function divideUniformlyRandomly(n As Integer, m As Integer) As Integer()
Dim rRandom As New Random
Dim fences As Integer() = New Integer(m - 2) {}
For i As Integer = 0 To m - 3
fences(i) = rRandom.Next(0, n - 1)
Next
[Array].Sort(fences)
Dim result As Integer() = New Integer(m - 1) {}
result(0) = fences(0)
For i As Integer = 1 To m - 3
result(i) = fences(i + 1) - fences(i)
Next
result(m - 1) = n - 1 - fences(m - 2)
Return result
End Function
This does work but I get 0 and 13 as well. I cannot ensure x and y here.
Give each child x chocolate. This will leave you with m - (n * x) to distribute randomly. Keep distributing to children that have less than y chocolates, until there are no more chocolates.
Private Function divideUniformlyRandomly(n As Integer, m As Integer, x As Integer, y As Integer) As Integer()
Dim rRandom As New Random
Dim aResult As Integer() = New Integer(n - 1) {}
Dim i As Integer = 0
Dim remaining As Integer = m
' Every n must have a min of x.
For i = 0 To n - 1
aResult(i) = x
remaining -= x
Next
' distribute the remaining m over the children randomly
While remaining > 0
' pick a child randomly
i = rRandom.Next(0, n)
' if the child has less than y, give them one
If aResult(i) < y Then
aResult(i) += 1
remaining -= 1
End If
End While
' Debug
Dim sum As Integer = 0
For i = 0 To n - 1
Console.WriteLine("{0}: {1}", i, aResult(i))
sum += aResult(i)
Next
Console.WriteLine("Sum: {0}", sum)
divideUniformlyRandomly = aResult
End Function

Trying to make a For Loop that adds something in Visual Basic; not getting right answer

I need to make a console app for a class, and it has to compute the following using a For Next loop: 4 + 8 + 12 + 16 + 20 .... + 208
Here's what I have:
Dim x As Integer = 0
Dim z As Integer = 4
For x = 0 To 208 Step 4
z = z + 4
Console.WriteLine(z)
Next
I have no idea what I'm doing wrong.
On each iteration, you are adding 4 to z, so you are actually computing 4 + 4 + 4 + ... + 4. What you really want to do is to add x to z:
Dim x As Integer = 0
Dim z As Integer = 4
For x = 0 To 208 Step 4
z = z + x
Console.WriteLine(z)
Next

to maintain variable value in double datatype using .net

Datatable contain some values like 0.0,10000.00,54678.94 . am getting that values using for loop and store it in a variable in double datatype. i want to add these nos and store it in a variable.
each time variable value changed.
for loop
Dim ds5 As dataset1.pro_dtsumDataTable = TA5.GetData(TextBox1.Text, users)
If (ds5.Rows.Count > 0) Then
Dim y As Double
y = Double.Parse(ds5(0)("sum(fld_primary)").ToString())
Dim y1 As Double
y1 = 0 + y
End If
Next
first time y1=0.0
next time the value of y1 is not added to previous value.
i want the result y1=64679.34
am new to vb.net. please help to do this?
for loop
Dim ds5 As dataset1.pro_dtsumDataTable = TA5.GetData(TextBox1.Text, users)
If (ds5.Rows.Count > 0) Then
Dim y As Double
y = Double.Parse(ds5(0)("sum(fld_primary)").ToString())
Dim y1 As Double
y1 += 0 + y
End If
Next
You keep assigning y to y1. I think what you want to do is increment the value. eg
y1 += 0 + y
The += is the same as:
y1 = y1 + 0 + y