|
-
Feb 24th, 2020, 12:22 PM
#1
Thread Starter
Junior Member
[RESOLVED] Resizing Array in VB.NET
Hello,
I have set up a little test as I need an Array to be updated with values from another Array.
When I monitor AddToArray all seems to go well, but after I run TestArray, I expect MainArray to have 3 new elements and it doesn't.
What am I missing?
Code:
Dim MainArray() As Object = {"Hello World", 12D, 16UI, "A"c, "blob"}
Dim AdditionalArray() As Object = {"carriage", 200, "Friend"}
Sub TestArray()
Dim i As Integer
AddToArray(MainArray, AdditionalArray) ' UBound of MainArray was 7 in TestArray
MsgBox("Ubound MA = " & UBound(MainArray)) ' here UBound of MainArray is 4 again
For i = LBound(MainArray) To UBound(MainArray)
MsgBox(i & ": " & MainArray(i))
Next
End Sub
Sub AddToArray(MainArray As Object, AdditionalArray As Object)
Dim i As Long
MsgBox("Initial UBound MA = " & UBound(MainArray))
For i = LBound(AdditionalArray) To UBound(AdditionalArray)
ReDim Preserve MainArray(UBound(MainArray) + 1)
MsgBox("New UBound MA = " & UBound(MainArray))
MainArray(UBound(MainArray)) = AdditionalArray(i)
Next
MsgBox("Final UBound MA = " & UBound(MainArray))
End Sub
-
Feb 24th, 2020, 12:56 PM
#2
Re: Resizing Array in VB.NET
VB provides a Concat method for arrays as part of Linq you should be able to use something like
Code:
MainArray = MainArray.Concat(AdditionalArray).ToArray
to combine the two arrays into a single array.
However you might find something like a List(Of ...) might make life easier as Lists are designed to grown and shrink without the issues associated with arrays.
More of an issue though is why are you creating an array that is mixing completely different data types in that way? If you could give an exampel of what you are trying to achieve as your end result there might be a better way that doesn't involve arrays.
-
Feb 24th, 2020, 02:18 PM
#3
Thread Starter
Junior Member
Re: Resizing Array in VB.NET
 Originally Posted by PlausiblyDamp
VB provides a Concat method for arrays as part of Linq you should be able to use something like
Code:
MainArray = MainArray.Concat(AdditionalArray).ToArray
to combine the two arrays into a single array.
However you might find something like a List(Of ...) might make life easier as Lists are designed to grown and shrink without the issues associated with arrays.
More of an issue though is why are you creating an array that is mixing completely different data types in that way? If you could give an exampel of what you are trying to achieve as your end result there might be a better way that doesn't involve arrays.
ThanX PlausiblyDamp,
MainArray = MainArray.Concat(AdditionalArray).ToArray does the trick! I wish we had had that one in VBA ;-)
The reason why I make an array of mixed objects is only "because I can" since Dim MainArray() As Object permits us to do that.
-
Feb 24th, 2020, 02:54 PM
#4
Re: Resizing Array in VB.NET
You might find that using List is easier to work with.
Code:
Dim MainArray As New List(Of Object)
Dim initA() As Object = {"Hello World", 12D, 16UI, "A"c, "blob"}
MainArray.AddRange(initA)
Dim AdditionalArray() As Object = {"carriage", 200, "Friend"}
MainArray.AddRange(AdditionalArray)
-
Feb 24th, 2020, 03:06 PM
#5
Re: Resizing Array in VB.NET
 Originally Posted by BartH_NL
ThanX PlausiblyDamp,
MainArray = MainArray.Concat(AdditionalArray).ToArray does the trick! I wish we had had that one in VBA ;-)
The reason why I make an array of mixed objects is only "because I can" since Dim MainArray() As Object permits us to do that.

Just because you can doesn't always mean you should if this is any kind of real application mixing data types like that is going to almost certainly hurt you down the line - it makes it impossible to access any array element safely without wrapping every access in type checks and / or error handling.
-
Feb 24th, 2020, 05:23 PM
#6
Re: Resizing Array in VB.NET
That looks like VB6 code, not .NET. If you are going to use .NET, one rule you might as well keep in mind is that if you EVER write Redim Preserve, you're doing it wrong. If you write Redim...you might be doing it wrong, but Redim Preserve is right out.
Arrays can't change size. Sure, you have Redim to set a size, but that doesn't change the size of the array. It creates a new array of the new size. If you have the Preserve word in there, then it copies the data from the old array to the new array. Either way, in the end, the old array is thrown out. That last step isn't so costly, because nothing is likely to happen at the moment. However, getting those new arrays is costly. There's a best way to do that, which has been understood for decades, and Redim ain't it. Therefore, the List was added to .NET back in 2005 (and before that was the ArrayList, which is essentially like a List(of Object)) so that you wouldn't have to deal with that. The List has .Add, Insert, Remove, and RemoveAt methods, along with many more. These allow you to add and remove items to and from the list however you need to. It does wrap an array, and that array is no more sizable than any other array, but it will resize in a very efficient fashion. Therefore, it is like using Redim Preserve, except better in every way, so Redim Preserve shouldn't be used.
If you're wondering what it does, it doubles the size of the array every time it needs to. Thus, if your array starts off with 16 elements, as soon as you add the 17th element, the array doubles to 32 elements, so you can add the 17th through 32nd. Once you add the 33rd element, it doubles again to 64 elements, and so forth. This means that the most costly step, that of acquiring the memory happens as infrequently as possible.
So, use a List unless your arrays will never change size.
Also, arrays in .NET don't have variable lower bounds. They start at 0, and only at 0. The upper bound can be obtained in a variety of ways, too, so the UBound command, while still around, is an anachronism. Since all arrays start at 0, the upper bound is the length minus one, and can't be anything other than that. That's true for Lists as well as arrays, since Lists wrap arrays. Therefore, you can dispense with LBound in all cases, and UBound if you want to.
My usual boring signature: Nothing
 
-
Feb 24th, 2020, 05:29 PM
#7
Thread Starter
Junior Member
Re: Resizing Array in VB.NET
 Originally Posted by PlausiblyDamp
Just because you can doesn't always mean you should if this is any kind of real application mixing data types like that is going to almost certainly hurt you down the line - it makes it impossible to access any array element safely without wrapping every access in type checks and / or error handling.
Yep PlausiblyDamp, ThnX, I get that. I certainly will use string or integer if that is more applicable than object.
-
Feb 24th, 2020, 05:32 PM
#8
Thread Starter
Junior Member
Re: Resizing Array in VB.NET
 Originally Posted by dbasnett
You might find that using List is easier to work with.
Code:
Dim MainArray As New List(Of Object)
Dim initA() As Object = {"Hello World", 12D, 16UI, "A"c, "blob"}
MainArray.AddRange(initA)
Dim AdditionalArray() As Object = {"carriage", 200, "Friend"}
MainArray.AddRange(AdditionalArray)
ThnX dbasnett, that looks very usable. I'll look into that. .AddRange is very handy here.
-
Feb 24th, 2020, 05:35 PM
#9
Thread Starter
Junior Member
Re: Resizing Array in VB.NET
Hi Shaggy Hiker,
That is indeed where I come from: VBA, VBS and VB6 ;-) ThnX for the clear explanation. I'll use that.
-
Feb 24th, 2020, 06:58 PM
#10
Re: Resizing Array in VB.NET
 Originally Posted by BartH_NL
Yep PlausiblyDamp, ThnX, I get that. I certainly will use string or integer if that is more applicable than object.
The problem really is the fact you are mixing data types in a single array, that is always going to make having proper data types difficult. Is there a real reason why you need to mix datatypes in an array in this fashion?
-
Feb 24th, 2020, 08:25 PM
#11
Re: Resizing Array in VB.NET
The lower bound of arrays and lists is zero in VB.Net as Shaggy told you. The exact .Net replacement for UBound is the GetUpperBound(dimension) extension function.
For example...
Code:
Dim a() As String = {"one", "two", "three"}
MsgBox(a.GetUpperBound(0)) ' returns 2
Which is equivalent to...
Code:
Dim a() As String = {"one", "two", "three"}
MsgBox(UBound(a)) ' returns 2
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 24th, 2020, 09:40 PM
#12
Re: Resizing Array in VB.NET
 Originally Posted by .paul.
The exact .Net replacement for UBound is the GetUpperBound(dimension) extension function.
It's not an extension. It's an instance method of the Array class. That means that it is only available for arrays, not collections. That makes sense, given that it requires a dimension to be specified. Arrays can have multiple dimensions while collections cannot.
-
Feb 24th, 2020, 09:45 PM
#13
Re: Resizing Array in VB.NET
 Originally Posted by jmcilhinney
It's not an extension. It's an instance method of the Array class. That means that it is only available for arrays, not collections. That makes sense, given that it requires a dimension to be specified. Arrays can have multiple dimensions while collections cannot.
I knew it wasn’t to be used with Lists. I didn’t intend to imply it could be used with Lists, though I can see the ambiguity...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 24th, 2020, 10:05 PM
#14
Re: Resizing Array in VB.NET
 Originally Posted by .paul.
I knew it wasn’t to be used with Lists. I didn’t intend to imply it could be used with Lists, though I can see the ambiguity...
I'm not sure that there are any standard extension methods that work on arrays but nothing else. The centre of LINQ is the IEnumerable(Of T) interface and that is what methods written to support LINQ originally extended, hence their being members of the Enumerable class. Obviously extension methods have grown in popularity well beyond LINQ but I'm still not aware of anything standard that works solely on arrays. I may just have missed it though.
-
Feb 25th, 2020, 02:47 AM
#15
Thread Starter
Junior Member
Re: Resizing Array in VB.NET
@PlausiblyDamp
No, I don't need to mix data types, I was only interested to see how they could be mixed.
What I am really trying to do is to select only the images in a folder and collect them for display in a form.
I have found that I can collect the FileInfo (for which I would need the Object type in the previous example) or the FilePath (which is obviously String).
That's why I came to Object in Array.
-
Feb 25th, 2020, 03:24 AM
#16
Re: Resizing Array in VB.NET
 Originally Posted by BartH_NL
@PlausiblyDamp
No, I don't need to mix data types, I was only interested to see how they could be mixed.
What I am really trying to do is to select only the images in a folder and collect them for display in a form.
I have found that I can collect the FileInfo (for which I would need the Object type in the previous example) or the FilePath (which is obviously String).
That's why I came to Object in Array.
You can declare an Array of Type FileInfo - there is no need to use Object if you know the datatype. Then agani it would probably be easier to work with a List(Of FileInfo) rather than an array as SH mentioned earlier.
-
Feb 25th, 2020, 04:20 AM
#17
Thread Starter
Junior Member
Re: Resizing Array in VB.NET
 Originally Posted by PlausiblyDamp
You can declare an Array of Type FileInfo - there is no need to use Object if you know the datatype. Then agani it would probably be easier to work with a List(Of FileInfo) rather than an array as SH mentioned earlier.
Well PlausiblyDamp,
I got to this now:
Code:
Private Sub FilesCollect(StrPath$)
Dim i As Long
Dim MyExtensions As New List(Of String)({"*.jpg", "*.jpeg", "*.png", "*.gif", "*.tiff", "*.bmp", "*.svg"})
FileList.Clear()
For Each F In MyExtensions
Dim files() As FileInfo = New DirectoryInfo(StrPath).GetFiles(F)
For i = 0 To files.GetUpperBound(0)
FileList.Add(files(i))
Next
Next
End Sub
and that is a whole lot less code than what I had before.
ThanX for pointing that out to me.
-
Feb 25th, 2020, 04:47 AM
#18
Re: Resizing Array in VB.NET
Code:
Private Sub FilesCollect(StrPath$)
Dim MyExtensions = {"*.jpg", "*.jpeg", "*.png", "*.gif", "*.tiff", "*.bmp", "*.svg"}
Dim folder As New DirectoryInfo(StrPath)
FileList.Clear()
For Each F In MyExtensions
FileList.AddRange(folder.GetFiles(F))
Next
End Sub
-
Feb 25th, 2020, 04:59 AM
#19
Re: Resizing Array in VB.NET
@jm I’m fairly sure that can be done in one line with LINQ. Which was (just) available in VB2008
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 25th, 2020, 05:18 AM
#20
Re: Resizing Array in VB.NET
 Originally Posted by .paul.
@jm I’m fairly sure that can be done in one line with LINQ. Which was (just) available in VB2008
A SelectMany would do the job, but that might be a bit much for the OP at this stage.
-
Feb 25th, 2020, 05:35 AM
#21
Thread Starter
Junior Member
Re: Resizing Array in VB.NET
 Originally Posted by jmcilhinney
Code:
Private Sub FilesCollect(StrPath$)
Dim MyExtensions = {"*.jpg", "*.jpeg", "*.png", "*.gif", "*.tiff", "*.bmp", "*.svg"}
Dim folder As New DirectoryInfo(StrPath)
FileList.Clear()
For Each F In MyExtensions
FileList.AddRange(folder.GetFiles(F))
Next
End Sub
Neat!!!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|