-
Mar 20th, 2021, 02:19 PM
#1
Thread Starter
New Member
Sorting Array
Hi, I’m new to VB, and trying to sort an array.
The array populates properly within the program, but gets a 424 error when I try to sort. There are no typos.
Also, I have to use SYSTEM.ARRAY.SORT(array name) If I use just ARRAY.SORT(array name), I get a compiler error. Don’t know if that’s an issue.
-
Mar 20th, 2021, 02:26 PM
#2
Thread Starter
New Member
Re: Sorting Array
I used this simple code and got the same error:
Code:
Dim thisNumber(2) As Integer
thisNumber(0) = 1
thisNumber(1) = 2
thisNumber(2) = 3
System.Array.Sort (thisNumber)
Last edited by dday9; Mar 23rd, 2021 at 08:26 AM.
Reason: Added Code Tags
-
Mar 20th, 2021, 02:27 PM
#3
Thread Starter
New Member
Re: Sorting Array
that was Integer, not Integ
Sorry
-
Mar 20th, 2021, 07:02 PM
#4
Thread Starter
New Member
Re: Sorting Array
Never mind, I used ArrayList instead and all is good.
-
Mar 20th, 2021, 08:20 PM
#5
Re: Sorting Array
I am guessing this is VB.Net from the syntax rather than VB Script...
Firstly what error where you getting? VB.Net throws exceptions, "Error 424" doesn't really mean anything - what was the error message / exception?
Secondly, Array is a class that lives in the System namespace. If you want to use just Array.Sort then you could add an "Imports System" to the top of the file so you don't need to fully qualify the name.
Finally, unless you are using a really old version of VB.Net you probably should avoid ArrayList and use List(Of Integer) instead, the List(Of Integer) will be more type safe and will also perform a lot better.
-
Mar 21st, 2021, 02:09 AM
#6
Re: Sorting Array
Welcome to VBForums
I have moved this thread from the 'VBScript' forum (for .vbs files etc) to the 'VB.Net' (VB2002 and later) forum
-
Mar 21st, 2021, 02:21 AM
#7
Re: Sorting Array
Originally Posted by TaraLee
Never mind, I used ArrayList instead and all is good.
No, all is not good. You should basically never use an ArrayList for anything. If you want a fixed-length list of items then an array is what you should use. If you want a variable-length list then you should use a List(Of T), where T would be Integer in your example. If you're not able to call Array.Sort then your system is broken so that is not an issue that you should ignore. This code:
vb.net Code:
Dim numbers(2) As Integer
numbers(0) = 2
numbers(1) = 1
numbers(2) = 3
Console.WriteLine(String.Join(",", numbers))
Array.Sort(numbers)
Console.WriteLine(String.Join(",", numbers))
outputs the following for me:
just as you would expect. You should tell us exactly what it actually does for you.
If you're getting a compilation error when you don't qualify the name Array then it is likely because you have used that name for your project, and therefore the default namespace of your application, or some type within the project. You should avoid using names that are likely to be used elsewhere. I would suggest that you settle on a "business name" and use that in all your projects. For instance, I was a sole trader for a while and my business name was Wunnell Development (my middle name is Philip with one L) and I still name all my projects Wunnell.SomeProject so there will never be a clash between my default namespace and some existing type. Even if you don't do that, you can use names like ArrayTest or ArrayDemo to minimise the chance of name clashes.
Last edited by jmcilhinney; Mar 22nd, 2021 at 07:00 PM.
-
Mar 21st, 2021, 02:23 AM
#8
Re: Sorting Array
Originally Posted by PlausiblyDamp
If you want to use just Array.Sort then you could add an "Imports System" to the top of the file so you don't need to fully qualify the name.
The System namespace is imported at the project level by default, so there should never be a need to import it at the file level.
-
Mar 22nd, 2021, 05:26 PM
#9
Thread Starter
New Member
Re: Sorting Array
No, all is not good.
You are right.
I come from the old IBM S3x, As400, iSeries days. I know there is a disconnect between my understanding of the newer languages and how I manage to get the job done. There are things I try just to get a good compile, and I have no idea why it satisfies the compiler. Yet somehow, with my decades of programming experience, I get the program to do what I want.
Anyway, I thank you much for your response and I have already read it over several times. Little by little, hopefully all will seep in.
-
Mar 22nd, 2021, 05:54 PM
#10
Thread Starter
New Member
Re: Sorting Array
Also, why is ArrayList so bad? I could send you my whole code and what it’s supposed to do. I think it’s too lengthy to post here.
-
Mar 22nd, 2021, 06:08 PM
#11
Re: Sorting Array
It is bad because it is not recommended and is obsolete for very long time (quoted from Microsoft docs):
Important
We don't recommend that you use the ArrayList class for new development. Instead, we recommend that you use the generic List<T> class. The ArrayList class is designed to hold heterogeneous collections of objects. However, it does not always offer the best performance. Instead, we recommend the following:
- For a heterogeneous collection of objects, use the List<Object> (in C#) or List(Of Object) (in Visual Basic) type.
- For a homogeneous collection of objects, use the List<T> class.
See Performance Considerations in the List<T> reference topic for a discussion of the relative performance of these classes. See Non-generic collections shouldn't be used on GitHub for general information on the use of generic instead of non-generic collection types.
And also described in the documentation on GitHub: https://github.com/dotnet/platform-c...docs/DE0006.md
Last edited by peterst; Mar 22nd, 2021 at 06:12 PM.
-
Mar 22nd, 2021, 06:32 PM
#12
Thread Starter
New Member
Re: Sorting Array
Thank you. I will play around with all that. I don’t write for a job, just my own personal stuff, so luckily I don’t have pressure.
I am always grateful when people take the time to be helpful.
-
Mar 23rd, 2021, 08:31 AM
#13
Re: Sorting Array
Generic List(Of T) are incredibly useful when you have a collection that does not have a fixed length (i.e. it can grow/shrink).
I wrote a VB.NET tutorial a while back and go over some of the more common collections in lesson 3 here: https://vblessons.com/lessons.html I would highly recommend you check it out.
I also wrote an FAQ here on VBForums titled Visual Basic .NET - Which Collection Should I Use? here: https://www.vbforums.com/showthread....n-Should-I-Use again, this is one that I would highly recommend reading.
-
Mar 27th, 2021, 12:15 PM
#14
Thread Starter
New Member
Re: Sorting Array
Well, color me red-faced. A few days ago it finally dawned on me that even though I selected Visual Basic in the Excel thing I was creating, it is actually VBA. Which is also NOT VB.net, nor VBscript. No wonder a lot of the syntax I found online wasnÂ’t working! For some reason I thought that was ALL Visual Basic. Duhhh
Thanks, si_the_geek for moving my original post. That was my first hint.
-
Mar 27th, 2021, 12:28 PM
#15
Re: Sorting Array
You certainly aren't the first to make that mistake - the naming of variations is all a bit confusing!
I have now moved this thread to the 'Office Development/VBA' forum.
-
Mar 28th, 2021, 01:56 AM
#16
Re: Sorting Array
No wonder a lot of the syntax I found online wasnÂ’t working!
so is this now resolved, you can sort your array? or you still need help with it?
in vba there is no built in function to sort an array you will have to use code to create your own sort function
search on quicksort or bubblesort
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
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
|