pass 1d array of length > 2^16 to an excel column with vb.net - vb.net

If I have a 1 dimensional array of 1 million indexes, is there a way to do a one-liner to pass the array into a sheet?
This works for arr.length < 2^16
wk.Range(String.Concat("a1:a", arr.Length)).Value = XlApp.Application.transpose(arr)
but not for arr.length = 1 million. I've done this in VBA, but it takes several lines of code and isn't very efficient.
I've also seen a one-liner done in Python, so I was wondering if Vb.net could do it.

Related

replacing data in existing arrays with new data

I want to replace a section of a vector with another vector.
In VBA or FORTRAN it would be done something like this.
Dim v1(10000)
Dim v2(1000)
'something to fill both vectors with floating point numbers'
For n = 1 to 1000 jj = 1000 + n v1(jj) = v2(n) next n
How do I do this in python? Seems like very common engineering stuff to me. Trivial in old languages.
I tried lots of things, including simply overwriting, deleting a section of the larger vector and then appending the smaller one, breaking the larger vector into three pieces then combine: part1 + v2 + part3.
the deleting operations seemed to remove the data but not the original indices

What is the best/fastest method of filling an array with numbers between two values?

I've got a background thread which adds numbers to an array, this call happens often so i'm just curious if there's a faster method of adding the numbers to the array?
I need to add all the numbers between two values (values will be different depending on the situation), example 1 to 4 which would add 1,2,3 & 4.
However, when I make the call, it would be adding much larger arrays similar to 500 to 1000 etc which sometimes takes a little longer depending on how many numbers need to be added.
At the moment i'm using something similiar to this:
Dim ListOfNumbers As New List(Of Integer)
For i = 1 To 100000
If ListOfNumbers.Contains(i) = False Then
ListOfNumbers.Add(i)
End If
Next
Is there any other method that I could use which might be faster?
(Avoiding duplicate values in the array if possible)
I doubt this is driving your program's performance. Moreover, the best option may depend on how you plan to use these numbers, where you may find you actually can noticeably improve performance by using something like IEnumerable throughout your code, rather than an array.
With that in mind, I suggest this option:
int start = 1; int stop = 100000;
var list = Enumerable.Range(start, stop - start).ToArray()
Such that you can easily remove the ToArray() later if desired.
As for the existing code... You control both the List and the loop. Checking .Contains()is not necessary, and probably taking up a significant part of the execution time here. Just remove that check. You can also optimize some by pre-setting the size of the list:
Dim size As Integer = 100000
Dim ListOfNumbers As New List(Of Integer)(size)
For i = 1 To size
ListOfNumbers.Add(i)
Next

How can I transfer the values of a 4D jagged array to a 2D standard array?

I have EuroCPCrap mapped as (j,0)(i,0) and want to put this into an array EuroCPConsol mapped as (j,i). I tried:
For j = 0 to CPIndex 'CPIndex is a global count of variables in matrix j references
For i = 0 to UBound (EuroCPCrap,3) 'i in the (now known to be) jagged
EuroCPConsol(j+1,i+1)= EuroCPCrap(j,0,i,0) 'add one since I'm base 1 but function that produced this matrix outputted base zero
Next i
Next j
I get a subscript error on the UBound statement, and I realised it's because there is no 3rd dimension in the referenced array.
First of all, I don't understand why this EuroCPCrap(j,0,i,0) would work if it's a jagged array as you describe in your first sentence. On the looks of it, it should be EuroCPCrap(j,0)(i,0).
You have a parent two-dimensional array of children two-dimensional arrays. The "third" dimension you're looking for is actually the first dimension of each child array. So something like this should work:
For i = 0 to UBound(EuroCPCrap(j,0),1)
Actually, iterating from LBound to UBound is even better practice to ensure that the entire array is traversed regardless of your Option Base or how the array is "Dimmed":
For i = LBound(EuroCPCrap(j,0),1) to UBound(EuroCPCrap(j,0),1)
Does EuroCPCrap really need to be jagged? Why not make it a 4-dimensional array? EuroCPConsol is not jagged... Is it dimensioned correctly to accept the contents of the largest of the children array? These are things to think about...

Visual Basic 6 Dynamic Variables

I am doing a program using vb6 which is stored datas from mouseclick in term of coordinates. I succeeded doing the first stage which is displaying the clicked coordinates. My problem now is I need to save the coordinate in term of variables so i can call them back to use for another purpose for example to find distance between two points.
if its just two coordinates its easier to find the distance. but when it comes to many coordinates I am stuck. I tried to do an array to stored the data inside loop
1. InputX(ListNum, 0) = Int(x)
2. InputY(ListNum, 1) = Int(y)
3. ListNum=ListNum+1
when I try to call for InputX(2,0) = Text1.Text or Text1.Text=InputX(2,0) none of them working. It seems that the data will be erased after I do a mouseclick
Is there any way which I can set the dynamic variables which stored each my clicked coordinates such as Input1,Input2,Input3 ...InputN
I do this in VB6.
The problem you're having is that you're using a two dimensional array there. A two dimensional array looks like a table. That's not what you want though. You want a list of pairs of points. So, create a structure with two integers in it, x and y, and make an array of those structures:
'Right underneath your Class Form1 declaration:
Structure Point
Dim x As Integer
Dim y As Integer
End Structure
Dim length As Integer = 10
Dim Points(length) As Point
'When you want to start using your points put this in the method:
Points(0).x = 10
Points(0).y = 10
Points(1).x = 20
Points(1).y = 40
Dynamic variables in VB6
First you declare the variable without giving size:
Dim InputX() As String
Then you give for the first time size to your array using ReDim:
ReDim InputX(5)
If you want to preserve whatever data is already in your array you use ReDim Preserve:
ReDim Preserve InputX(10)
I hope this is what you need.
it appears that the first method
Text1.Text=InputX(2,0)
is working. I just need to declare x and y As Single

Size of array in Visual Basic?

I've tried this code in VB:
Dim a(1) As Byte
Console.WriteLine(a.Length)
The output is "2". Anyone any idea why?
If you are used to C/C++/C# languages you are used that when declaring an array to initialize it with the number of elements in the array.
C# : byte a[] = new byte[1]
will declare a byte array with 1 element (upperBound = 0)
The behavior is different in VB where, when declaring an array the parameter used in initialization represents the UpperBound of the array.
VB.NET: Dim a(1) As Byte
will declare a byte array with 2 elements (upperBound = 1)
In Visual Basic, the size of an array is declared with the array's upper bound, where most languages declare the size of an array by specifying the number of elements in the array. If you aren't aware of this, then your Visual Basic arrays end up being 1 element longer than you expected:
VB.NET:
Dim a(1) as Byte ' under the hood, translated to byte[2]
Console.WriteLine("{0}", a.Length) ' output 2
a(0) = 7 ' No error, element exists
a(1) = 7 ' No error, element exists, array length is 2
a(a.Length) = 7 ' error: Index was outside the bounds of the array.
C#:
byte[] a = new byte[1];
Console.WriteLine("{0}", a.Length); // output 1
a[0] = 7 // No error, element exists
a[1] = 7 // error: Index was outside of bounds of the array. (because array length is 1)
a[a.Length] = 7; // error: Index was outside the bounds of the array.
The reason why Microsoft designed VB.NET to size arrays based on upper bound rather than array length is to make it easier to port code from VB6 to VB.NET. The initial index of a VB6 array is 1, unless you declare Option Base 0. It was common to loop through an array of size N using For i = 1 To N. By designing VB.NET to interpret an array's sizing argument as an upper bound rather than the number of elements in the array, old VB6 code that looped from 1 to N could be ported directly to VB.NET. The array in VB.NET will have one extra element compared to what the array had in VB6 (the element at index 0) but otherwise behaves as it did in VB6.
You'll sometimes see people claim that Visual Basic creates a "wasted" element. This is only true when porting legacy VB6 code which didn't expect an element at index 0. When writing new code, you just have to remember what the sizing parameter means (upper bound, not element count), and declare your arrays accordingly. Just reduce your sizing parameters by one compared to what you'd see in C#. The resulting array will have elements from a(0) to a(a.Length-1), just like a C# array.
Array starts from position 0. You are defining two positions.
If you want only 1 position, then:
Dim a(0) As Byte
and you will get a.Length as 1.
Dimension Length The index of each
dimension is 0-based, which means it
ranges from 0 through its upper bound.
Therefore, the length of a given
dimension is greater by 1 than the
declared upper bound for that
dimension.
Array Size in Visual Basic
The previous answers each have pieces of the correct answer, but not the full correct answer.
When you declare an array (Like with your code: Dim a(1) As Byte) the number you put in the array declaration (in this case, 1) is NOT a declaration of how many entries in the array, it is a declaration of the upper boundary of the array.
So, in your declaration, you're creating an array with 2 entries: a(0) and a(1)