Total Nos. of items in array variable - vb.net

I have an array variable (string type). It contains certain no. of items, that I donot know how many they are.
I need to run a loop for that many nos. that the array contains. I tried LBound and UBound loop but it says my array is not a system array.
How can I know how many items my array contains?
Thanks
Furqan

You can use the Length property of the array object.
From MSDN (Array.Length Property):
Gets a 32-bit integer that represents the total number of elements in all the dimensions of the Array.
Read about arrays in VB.NET and the Array class for better understanding of arrays in VB.NET and the .NET framework.
Update:
However, for looping over an array you should simply use a For Each loop (as an array is treated like any other collection in .NET) - this way you will not make any silly mistakes with array bounds and off by ones:
For Each item As arrayItemType in MyArray
' do stuff with item
Next
See the example on this page.

You look at the length:
To get the number of items in the first dimension: arrayName.GetLength(0)
If you need the index, use GetUpperBound(0)
Some helpful examples here.

Like Oded said, you can use the Length-propery of the Array. This would look something like this:
Dim data As String() = {"one", "two", "three", "four"}
For i = 0 To data.Length - 1
Console.WriteLine(data(i))
Next
If you just want to loop all strings in your array, you can use For Each as well:
For Each s As String In data
Console.WriteLine(s)
Next

If the compiler is telling you that your variable is not a system array, then chances are, it's not an array. If it's not an array, you won't be able to get its bounds through any means.
Inspect the variable in the locals window and verify that your variable is of the type that you think it is. It's probably not an array after all.

Related

Visual Basic Array Push

I am currently iterating through a loop and redimensionalising my array every time I need to add a value (as you can imagine this takes some time for a redim every loop) is there a way I can implement a push similar to ruby or java? This would need to save the processing time needed to redimensionalise the array every time I need to add a value to it.
Cheers
Martin
You'd be better off using a List (Of Type). Then you can just call the Add method.
For example:
Dim foo As New List(Of String)
foo.Add("Bar")
You can concat the array, with a array containing only the new item, or multiple.
Array.Concat({Item}).ToArray

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 add to Array dynamically and sort

Still having trouble can you please help?
This needs to be written in Visual Basic
Here is a statement from the Main part of the program...
mylist.ForEach(AddressOf ProcessLink)
What this statement says is the following.... "For each item in the ArrayList "mylist" send item to the sub program "ProcessLink"
Note that ProcessLink is going to receive multiple groups of data from the ArraList "mylist"
ProcessLink then takes each value sent to it and turns it into "P.myName" and P.myValue"
I need ProcessLink to then add these values to an array. And each time it receives a batch of data from the ArrayList "mylist" it will add those values to the same Array. ProcessLink will then sort the array based on "P.Value"
I then need ProcessLink to output the name value pairs in the array and output the result as...
Response.Write("<tr><td>" & P.myName & "</td><td>" & P.myValue & "</td></tr>")
What should the code in ProcessLink look like?
I really reccomend using generic object lists rather than Arrays. You will get all of the functionality you need + 10x more. Sorting, adding, etc are way easier. With generic lists you don't have to worry about declaring the Array size, or any of the difficulties when working with Arrays containing objects. Take a look to the following for more information including the code sample:
.NET Object Collections Using Generics 101:
http://allen-conway-dotnet.blogspot.com/2009/11/net-object-collections-using-generics.html
List(Of T) Class:
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

Array in VB .Net

I have an array Newstr(20) When I fill it, I need to know how many of its indexes has been filled ? and find out the values.
I need to know how many of its indexes has been filled ?
Arrays don't keep that information. They only know how many spots you allocated. You have to track how many you assigned yourself. More than that, if you're working with a collection where you don't know how many items there will be, arrays are really the wrong choice in the first place. You should use a List(Of T) instead.
You could populate the array with a known string, then test for that string to see how many elements in your array are filled.
I would - however - suggest using an array list. You can get the number of elements added to the list from the Count property. This is the MSDN entry for Array Lists.
In order to find which of the elements have been filled, you can use a LINQ construction like this:
Dim input() = New String() {"abc", "def", "ghi", "", Nothing}
Dim output = input.Where(Function(i) Not String.IsNullOrEmpty(i)).ToArray
When you run this code, the output array will contain "abc", "def" and "ghi".
You can modify the selector of the Where to suit your preference if you're coding for a different type of array.
For instance the selector for Integer? will be:
input.Where(Function(i) (Not i Is Nothing) Or (i <> 0)).ToArray
Of course, you'll have to be coding in .NET 3.5+ in order to get access to LINQ.

Resizing a two dimensional Array

How can I resize the two dimensional array size without affecting its value?
Use ReDim with the Preserve modifier. VB.NET will make sure the original values are untouched. Didn't read the documentation right. ReDim Preserve will only allow you to change the length of the last dimension of the array.
You need to allocate a new array (with the correct size) and manually copy the elements from the first array to the second.
As Adam said, you can't resize 2D arrays dynamically. You can easily copy the existing array into a bigger one like so:
Dim smaller(1, 1) As Byte
Dim bigger(2, 2) As Byte
Array.Copy(smaller, bigger, smaller.length)
Try using array.resize if you are on .net 2 framework or above.
For example:
Dim MyArray() as string
Array.Resize(myarray,12)