Results 1 to 16 of 16

Thread: Sorting Array

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    8

    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.

  2. #2

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    8

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    8

    Re: Sorting Array

    that was Integer, not Integ

    Sorry

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    8

    Re: Sorting Array

    Never mind, I used ArrayList instead and all is good.

  5. #5
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,401

    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.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,657

    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

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,875

    Re: Sorting Array

    Quote Originally Posted by TaraLee View Post
    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:
    1. Dim numbers(2) As Integer
    2.  
    3. numbers(0) = 2
    4. numbers(1) = 1
    5. numbers(2) = 3
    6.  
    7. Console.WriteLine(String.Join(",", numbers))
    8.  
    9. Array.Sort(numbers)
    10.  
    11. Console.WriteLine(String.Join(",", numbers))
    outputs the following for me:
    2,1,3
    1,2,3
    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.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,875

    Re: Sorting Array

    Quote Originally Posted by PlausiblyDamp View Post
    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.

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    8

    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.

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    8

    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.

  11. #11
    Hyperactive Member
    Join Date
    Jun 2019
    Posts
    310

    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.

  12. #12

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    8

    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.

  13. #13
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,252

    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.

  14. #14

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    8

    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.

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,657

    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.

  16. #16
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,333

    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
  •  



Click Here to Expand Forum to Full Width