Why one over another: UBound or Length - vb.net

Could there be any specific reason why one can choose UBound over Length?
Here is the code and 1-dimension is passed as second parameter.
For iIndex = 0 To UBound(myList)
If Left(Request.ServerVariables("REMOTE_ADDR"), Len(myList(iIndex))) = saIPList(iIndex) Then
bAuth = True
Exit For
End If
Next
Any performance gain against Length

They do different things! UBound gives you the last index in the array, while Length gives you the length. Those are not the same, because usually UBound will be Length - 1.

Ubound exists mainly for backwards compatibility to old code. I haven't seen anything saying it's deprecated just yet, but at the same time I recognize it's not really aligned with the way they have been taking the language in recent years. The same is true for the Len() and Left() functions in that code; they are the way of the past, not the future. The sooner you adapt, the happier you will be.
For what it's worth, the argument is largely moot. More "modern" ways to write that code look entirely different. Here's one example:
bAuth = myList.Zip(saIPList, Function(a,b) New With {.Length = a.Length, .saIP = b} ) _
.Any(Function(i) Request.ServerVariables("REMOTE_ADDR").ToString().SubString(0,i.Length) = i.saIP)

For performance gain I was also interested in what function has the best performance.
It seems that the length -1 is much faster than the UBound. I expected UBound to be faster somehow.
After 100.000.000 times it seems the time for length -1 is 952ms and for UBound: 5844ms.
(length -1) is ~6 times faster than UBound
Code used for testing
Private Sub UboundlengthToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UboundlengthToolStripMenuItem.Click
ListBox1.Items.Clear()
ListBox1.Items.Add("BEGIN")
'set required vars
Dim ints() As Integer = {1, 2, 3, 4, 5}
'end vars setting
Dim t As New Stopwatch
Dim gt As New Stopwatch
Dim time1 As Integer
Dim temp As Integer
Dim d As Double = GC.GetTotalMemory(False)
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.GetTotalMemory(False)
ListBox1.Items.Add("Free Memory: " & d)
gt.Start()
t.Reset()
'starting test---------------------------------------
'single test---------------------------------------
t.Start()
For i As Integer = 0 To TextBox1.Text
temp = ints(ints.Length - 1)
Next
t.Stop()
time1 = t.ElapsedMilliseconds
ListBox1.Items.Add("arr.length - 1")
ListBox1.Items.Add("Func1 total time: " & time1)
ListBox1.Items.Add("Func1 single time: " & time1 / TextBox1.Text)
t.Reset()
'single test---------------------------------------
'single test---------------------------------------
t.Start()
For i As Integer = 0 To TextBox1.Text
temp = ints(UBound(ints))
Next
t.Stop()
time1 = t.ElapsedMilliseconds
ListBox1.Items.Add("UBound:")
ListBox1.Items.Add("Func1 total time: " & time1)
ListBox1.Items.Add("Func1 single time: " & time1 / TextBox1.Text)
t.Reset()
'single test---------------------------------------
'Finishing test--------------------------------------
gt.Stop()
ListBox1.Items.Add("Total time " & gt.ElapsedMilliseconds)
d = GC.GetTotalMemory(True) - d
ListBox1.Items.Add("Total Memory Heap consuming (bytes)" & d)
ListBox1.Items.Add("END")
End Sub
Tried different things to eliminate possible optimalisations of the compiler, all with the same result as stated above.

It's carried over from earlier VB days. UBound can give you the highest index of any single dimension in a multi-dimensional array. Length only gives you the total number of elements.
If you declare:
' A 5x10 array:
Dim array(4, 9) As Integer
The values are:
array.Length = 50 ' Total number of elements.
array.Rank = 2 ' Total number of dimensions.
array.LBound(0) = 0 ' Minimum index of first dimension.
array.LBound(1) = 0 ' Minimum index of second dimension.
array.UBound(0) = 4 ' Maximum index of first dimension.
array.UBound(1) = 9 ' Maximum index of second dimension.

Interesting UBounds is quite a bit faster in this example
Dim startTick As Long
For j As Integer = 1 To 5
Dim rnd As New Random(Now.Subtract(Now.Date).TotalSeconds)
Dim RandomMax As Integer = rnd.Next(10000, 100000)
Dim cumbersome() As Integer = New Integer() {rnd.Next}
'Ubound is better than Length. Using Length takes as much time as redimensioning the entire array.
startTick = Environment.TickCount
For i As Integer = 1 To RandomMax - 1
ReDim Preserve cumbersome(UBound(cumbersome) + 1)
cumbersome(UBound(cumbersome)) = Rnd.Next
Next
Debug.Print("{0}) Array Method UBound: {1} Ticks to construct {2} integers", j, Environment.TickCount - startTick, RandomMax)
'Length is slow don't use it
startTick = Environment.TickCount
For i As Integer = 1 To RandomMax - 1
ReDim Preserve cumbersome(cumbersome.Length)
cumbersome(cumbersome.Length - 1) = Rnd.Next
Next
Debug.Print("{0}) Array Method Length: {1} Ticks to construct {2} integers", j, Environment.TickCount - startTick, RandomMax)
Next

Related

VBA List all possible combination of variable number of items (number of nested loops as variable)

gentleman! I am having trouble with figuring out a way to define the number of elements as variable when listing all possible combinations. I have a hard coded example of this where number of elements = 3
'Declare variables
Dim a as long
Dim b as Long
Dim C as Long
Dim ElementsArray as variant
'Array
ElementsArray = array("1400","1900","2400")
'Loop through combinations
for a = lbound(ElementsArray) to ubound(ElementsArray)
for B= lbound(ElementsArray) to ubound(ElementsArray)
for c = lbound(ElementsArray) to ubound(ElementsArray)
debug.print(ElementsArray(a) & " - " & ElementsArray(b) & " - " & ElementsArray(c))
next c
next b
next a
But What I am looking for is a code in which perhaps the number of nested For loops is a variable or some other ways to permutate through all possible combinations. Please help solve this problem.
Here is an example of a recursive implementation. Just be warned that you shouldn't make your array too large as you will get n to the power of n solutions - for 4 elements, that's 256, for 5 elements 3'125, for 6 you get 46'656 and for 7 already 823'543 - don't complain if the program takes a long time to execute. And of course you need a way to do something with every permutation.
Option Explicit
Sub test()
Dim ElementsArray As Variant
ElementsArray = Array("1400", "1900", "2400")
ReDim SolutionArray(LBound(ElementsArray) To UBound(ElementsArray))
recursion ElementsArray, SolutionArray, LBound(ElementsArray)
End Sub
Sub recursion(elements, solution, level As Long)
Dim i As Long
For i = LBound(elements) To UBound(elements)
solution(level) = elements(i)
If level = UBound(elements) Then
Debug.Print Join(solution, " - ")
Else
recursion elements, solution, level + 1
End If
Next i
End Sub
Update: This is the result:
Update
Still not sure if I understand. The following code will create a list of n-Tupel out of an array of values.
In the example (test), we have an array of 4 values and set n to 3 (defined as constant).
Sub test()
Const n = 3
Dim ElementsArray As Variant
ElementsArray = Array("1400", "1900", "2400", "9999")
ReDim SolutionArray(0 To n - 1)
recursion ElementsArray, SolutionArray, LBound(ElementsArray)
End Sub
Sub recursion(elements, solution, level As Long)
Dim i As Long
For i = LBound(elements) To UBound(elements)
solution(level) = elements(i)
If level = UBound(solution) Then
Debug.Print Join(solution, " - ")
Else
recursion elements, solution, level + 1
End If
Next i
End Sub

VB.net, Linq -- How to compare items in List of Strings

I have got a list of Strings looking like this:
The items of the list of Strings are formated like this "#,#" where # stands for an integer number and # stands for a string or number.
I need to find the index of the first occurrence where the integer number is lower than the integer number of the previous item.
I am able to find it using a loop over all entries like this:
For X = 0 To UBound(TempList.ToArray) - 1
If Val(Left(TempList(X), InStr(TempList(X), ",") - 1)) > Val(Left(TempList(X + 1), InStr(TempList(X + 1), ",") - 1)) Then
Dim Result As String = TempList(X) & " -- " & TempList(X + 1)
End If
Next
I am sure that this can be done much smarter using linq - but my very poor knowledge regarding linq is not enough ...
Can someone please give me a hint?
Linq is cool but it is not necessarily faster. It is looping internally.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim TempList As New List(Of String) From {"450,245.5", "510,1", "520,1", "703,1", "704,0", "705,0", "706,0", "901,1.244", "921,3", "1,1", "2,1", "3,0"}
Dim Result As String = ""
For i As Integer = 1 To TempList.Count - 1
If CInt(TempList(i).Split(","c)(0)) < CInt(TempList(i - 1).Split(","c)(0)) Then
Result = $"{TempList(i)} At index {i} -- {TempList(i - 1)}"
Exit For 'Stop looping when found
End If
Next
MessageBox.Show(Result)
End Sub
You can use LINQ for the loop, but I do think a For / For Each is sufficient - see Mary's answer.
But at least, you could use LINQ to parse your list into something other than strings to make it more manageable when it's used. This will decouple your code a bit and will make it clearer. I'll hold the pair of data in a Tuple(Of Double, String). If I knew the first item was distinct, I would use a Dictionary(Of Double, String).
So for fun, you can use TakeWhile instead of a For / For Each.
Dim TempList = { "450,245.5", "510,1", "520,1", "701,0", "702,0", "703,1", "704,0", "705,0", "706,0", "720,0", "901.2,455", "921,3", "1,1", "2,1", "3,0"}
Dim parsedList = TempList.
Select(Function(s) s.Split(","c)).
Select(Function(ss) (CDbl(ss(0)), ss(1)))
Dim lastValue As Double = Double.NaN
Dim index = parsedList.TakeWhile(
Function(t)
Dim result = Double.IsNaN(lastValue) OrElse t.Item1 > lastValue
lastValue = t.Item1
Return result
End Function).Count()
Dim item = parsedList(index)
Console.WriteLine($"Index: {index}, number: {item.Item1}, string: {item.Item2}")
Index: 12, number: 1, string: 1
This is basically just making a For Each with LINQ. It's probably not necessary, and a simple loop is arguably more readable, and you won't gain any benefit in performance either.

VBA. "Type mismatch: array or user-defined type expected"

I am completely new to VBA. I need to write a program, which will generate an array of integer and will find an index of the minimal element. I got this error
"Type mismatch: array or user-defined type expected." I looked into many similar questions, but couldn't figure out what is wrong.
Function random_integer(Min As Integer, Max As Integer)
random_integer = Int((Max - Min + 1) * Rnd + Min)
End Function
Function generate_array(Size As Integer)
Dim Arr(Size)
For i = 0 To UBound(Arr)
Arr(i) = random_integer(i - 10, i + 10)
Next
generate_array = Arr
End Function
Function find_index_of_min_elem(ByRef Arr() As Variant)
Dim Min As Integer
Min = Arr(0)
Dim MinIndex As Integer
MinIndex = 0
For i = 1 To UBound(Arr)
If Arr(i) < Min Then
Min = Arr(i)
MinIndex = i
End If
Next
find_index_of_min_elem = MinIndex
End Function
Sub task6()
A = generate_array(20)
IndexOfMinElemInA = find_index_of_min_elem(A)
MsgBox IndexOfMinElemInA
End Sub
The problem is the function Function find_index_of_min_elem(ByRef Arr() As Integer) is expecting an Integer as a parameter and you are passing a as a Variant
a = generate_array(20)
IndexOfMinElemInA = find_index_of_min_elem(a)
The next error that you will get is on Dim Arr(Size) As Integer. You cannot dimension an array like that.
I would recommend reading up on arrays.
There may be other errors but I have not checked those.
What about this? Make a second column of index numbers (in sequence from small to large) and then order the two rows by your original column. I've included the example below to illustrate:
A = the original column of numbers,
B = the column of index numbers
D & E are the result of sorting A & B by column A
The answer is then: the lowest number "0" was at index 7

Random numbers in array without any duplicates

I'm trying to randomize an array from numbers 0 to 51 using loops but I just cannot seem to pull it off. My idea was that
Generate a Random Number
Check if this random number has been used by storing the previous in an array
If this random number has been used, generate new random number until it is not a duplicate
If it's not a duplicate, store it
My attempt:
Dim list(51) As Integer
Dim templist(51) As Integer
For i As Integer = 0 To 51 Step 1
list(i) = i
Next i
Do While counter <= 51
p = rand.Next(0, 52)
templist(counter) = p
For n As Integer = 0 To 51 Step 1
p = rand.Next(0, 52)
If templist(n) = p Then
Do While templist(n) = p
p = rand.Next(0, 52)
Loop
templist(n) = p
Else
templist(n) = p
End If
Next
counter += 1
Loop
For n As Integer = 0 To 51 Step 1
ListBox1.Items.Add(templist(n))
Next
It will be a lot easier if you just have a list of all of the possible numbers (0 to 51 in your case), then remove the number from the list so it can't be picked again. Try something like this:
Dim allNumbers As New List (Of Integer)
Dim randomNumbers As New List (Of Integer)
Dim rand as New Random
' Fill the list of all numbers
For i As Integer = 0 To 51 Step 1
allNumbers.Add(i)
Next i
' Grab a random entry from the list of all numbers
For i As Integer = 0 To 51 Step 1
Dim selectedIndex as Integer = rand.Next(0, (allNumbers.Count - 1) )
Dim selectedNumber as Integer = allNumbers(selectedIndex)
randomNumbers.Add(selectedNumber)
allNumbers.Remove(selectedNumber)
' Might as well just add the number to ListBox1 here, too
ListBox1.Items.Add(selectedNumber)
Next i
If your goal is to get the numbers into ListBox1, then you don't even need the "randomNumbers" list.
EDIT:
If you must have an array, try something like this:
Function RandomArray(min As Integer, max As Integer) As Integer()
If min >= max Then
Throw New Exception("Min. must be less than Max.)")
End If
Dim count As Integer = (max - min)
Dim randomNumbers(count) As Integer
Dim rand As New Random()
' Since an array of integers sets every number to zero, and zero is possibly within our min/max range (0-51 here),
' we have to initialize every number in the array to something that is outside our min/max range.
If min <= 0 AndAlso max >= 0 Then
For i As Integer = 0 To count
randomNumbers(i) = (min - 1) ' Could also be max + 1
Next i
End If
Dim counter As Integer = 0
' Loop until the array has count # of elements (so counter will be equal to count + 1, since it is incremented AFTER we place a number in the array)
Do Until counter = count + 1
Dim someNumber As Integer = rand.Next(min, max + 1)
' Only add the number if it is not already in the array
If Not randomNumbers.Contains(someNumber) Then
randomNumbers(counter) = someNumber
counter += 1
End If
Loop
Return randomNumbers
End Function
This is good enough for your assignment, but the computer scientist in my hates this algorithm.
Here's why this algorithm is much less desirable. If zero is in your range of numbers, you will have to loop through the array at least 2N times (so 104+ times if you are going from 0 to 51). This is a best case scenario; the time complexity of this algorithm actually gets worse as the range of numbers scales higher. If you try running it from 0 to 100,000 for example, it will fill the first few thousand numbers very quickly, but as it goes on, it will take longer and longer to find a number that isn't already in the list. By the time you get to the last few numbers, you could potentially have randomly generated a few trillion different numbers before you find those last few numbers. If you assume an average complexity of 100000! (100,000 factorial), then the loop is going to execute almost ten to the half-a-millionth power times.
An array is more difficult to "shuffle" because it is a fixed size, so you can't really add and remove items like you can with a list or collection. What you CAN do, though, is fill the array with your numbers in order, then go through a random number of iterations where you randomly swap the positions of two numbers.
Do While counter <= 51
p = rand.Next(0, 52)
While Array.IndexOf(list, p) = -1
p = rand.Next(0, 52)
End While
counter += 1
Loop
Haven't written VB in about 5 years, but try this out:
Function GetRandomUniqueNumbersList(ByVal fromNumber As Integer, ByVal toNumber As Integer) As List(Of Integer)
If (toNumber <= fromNumber) Then
Throw New ArgumentException("toNumber must be greater than fromNumber", toNumber)
End If
Dim random As New Random
Dim randomNumbers As New HashSet(Of Integer)()
Do
randomNumbers.Add(random.Next(fromNumber, toNumber))
Loop While (randomNumbers.Count < toNumber - fromNumber)
Return randomNumbers.ToList()
End Function
Ok, that was painful. Please someone correct it if I made any mistakes. Should be very quick because it's using a HashSet.
First response to forum on stackoverflow - be gentle.
I was looking for a way to do this but couldn't find a suitable example online.
I've had a go myself and eventually got this to work:
Sub addUnique(ByRef tempList, ByVal n, ByRef s)
Dim rand = CInt(Rnd() * 15) + 1
For j = 0 To n
If tempList(j) = rand Then
s = True
End If
Next
If s = False Then
tempList(n) = rand
Else
s = False
addUnique(tempList, n, s)
End If
End Sub
Then call the sub using:
Dim values(15) As Byte
Dim valueSeen As Boolean = False
For i = 0 To 15
addUnique(values, i, valueSeen)
Next
This will randomly add the numbers 1 to 16 into an array. Each time a value is added, the previous values in the array are checked and if any of them are the same as the randomly generated value, s is set to true. If a value is not found (s=false), then the randomly generated value is added. The sub is recursively called again if s is still true at the end of the 'For' loop. Probably need 'Randomize()' in there somewhere.
Apologies if layout is a bit wobbly.

Populating 2 dimensional array using a For loop

Currently I'm trying to fill a 3x3 square with random x's and o's to make a tic tac toe
game. Unfortunately the game doesn't seem to output all the x's and o's. Logically, from what I can see, it should be able to but it's not. Any help would be appreciated.
Shared Sub twodimension()
Dim tic(2, 2) As String
Dim min As Integer
Dim x As String
Dim random As New Random()
Dim i As Integer
Dim x1 As Integer
Dim bound0 As Integer = tic.GetUpperBound(0)
Dim bound1 As Integer = tic.GetLowerBound(1)
For i = 0 To bound0
For x1 = 0 To bound1
min = random.Next(2)
If min = 0 Then
x = "x"
Console.WriteLine("{0}", x)
Else
x = "o"
Console.WriteLine("{0}", x)
End If
Console.Write(" "c)
Next
Console.WriteLine()
Next
End Sub
So presumably you've got this declaration somewhere, right?
Public Shared Tic(2, 2) As String
In your code you've got GetLowerBound which will (almost) always returns zero and instead you should have GetUpperBound().
Dim bound0 As Integer = tic.GetUpperBound(0)
Dim bound1 As Integer = Tic.GetUpperBound(1)
EDIT (in response to comment)
GetUpperBound(int) returns the highest number that you can use for the dimension that you specify.
So for the following array:
Dim MyArray(4, 6, 8) As Integer
Trace.WriteLine(MyArray.GetUpperBound(0)) ''//Returns 4
Trace.WriteLine(MyArray.GetUpperBound(1)) ''//Returns 6
Trace.WriteLine(MyArray.GetUpperBound(2)) ''//Returns 8
GetLowerBound(int) returns the lowest number that you can use for the dimension that you specify. In almost every case this is zero but in older versions of VB (and using some COM interop) you can create arrays that don't "start" at zero and instead start at whatever you wanted. So in old VB you could actually say Dim Bob(1 To 4) As Integer and GetLowerBound(0) would return 1 instead of 0. For the most part there is no reason to even be aware that GetLowerBound exists even.