[RESOLVED] Best way to store and arrange data-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] Best way to store and arrange data

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Posts
    163

    Resolved [RESOLVED] Best way to store and arrange data

    I'm currently porting a VBA script to VB.net and this program gets a list of files and information about each file. Currently the VBA script is using a two dimensional array to store the data. I'm having some issues porting some of the code since VBA apparently does arrays a little differently. So I want to step back and see if there is a better way to store this data before moving on.

    I'm storing the following information about files and then sorting it afterwards based on the part number.

    Part number, Description, Quantity, Vendor, etc

    What other methods does VB.net have for storing two dimension information like this and do any of those have built in ways to sort the info? I know VB.net can sort one dimension arrays but I had to find a custom function to sort my two dimensional array.

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    3,806

    Re: Best way to store and arrange data

    In .NET, you would make a class with properties for each bit of information you want to store. Then you don't need a multidimensional array, you can make a 1D array of the class type.
    Nothing I post is production-ready. It is provided as-is, use it at your own risk.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Posts
    163

    Re: Best way to store and arrange data

    Do you have any example code you could share or links to good examples. Between probably not searching the right terms and never creating classes before I haven't found any good examples. Thanks

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,567

    Re: Best way to store and arrange data

    I'm constantly amazed at how bad people are at determining keywords for web searches. You're far from the only one, but I think that that makes it more concerning rather than less. You actually wrote the keywords in your own post!
    Between probably not searching the right terms and never creating classes before I haven't found any good examples.
    So you want to know how to create a class in VB.NET? What's wrong with searching for create class vb.net? I just searched for that and this was the first result:

    https://msdn.microsoft.com/en-us/library/ms973814.aspx
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,600

    Re: Best way to store and arrange data

    Dealing with multi-dimentional arrays (in fact, arrays in general) is a bit cumbersome in VB.NET. We have much easier ways here. Instead of arrays use List or Dictionary/HashTable etc. as the case may be.

    1. Create a class with fields/properties that can hold the values.

    vb.net Code:
    1. Public Class Part
    2.     Public PartNumber As String
    3.     Public Description As String
    4.     Public Vendor As String
    5.     Public Quantity As Integer
    6. End Class

    2. In your class/form declare a variable as List(Of Part) to keep our Parts. We avoid arrays, since they require a lot of DIM and REDIM to add/remove items from the collection. We also add a few helper methods to add/remove/find
    vb.net Code:
    1. Dim Parts As New List(Of Part)
    2.  
    3.     Private Sub AddPart(ByVal partNumber As String, ByVal description As String, ByVal quantity As Integer, ByVal vendor As String)
    4.         Dim newPart As New Part With {.PartNumber = partNumber, .Description = description, .Quantity = quantity, .Vendor = vendor}
    5.         Parts.Add(newPart)
    6.     End Sub
    7.  
    8.     Private Function FindPart(ByVal partNumber As String) As Part
    9.         Return Parts.Where(Function(f) f.PartNumber = partNumber).FirstOrDefault
    10.     End Function
    11.  
    12.     Private Sub RemovePart(ByVal partNumber As String)
    13.         Dim partToRemove As Part = FindPart(partNumber)
    14.         If partToRemove IsNot Nothing Then Parts.Remove(partToRemove)
    15.     End Sub
    16.  
    17.     Private Function SortPartsByPartNumber() As List(Of Part)
    18.         Return Parts.OrderBy(Function(f) f.PartNumber).ToList
    19.     End Function

    Now its so easy to handle our parts collection.
    e.g.
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         '-- add some parts
    3.         AddPart("part1", "description of part1", 10, "vendor1")
    4.         AddPart("part3", "description of part3", 20, "vendor3")
    5.         AddPart("part2", "description of part2", 15, "vendor2")
    6.         AddPart("part7", "description of part7", 11, "vendor7")
    7.         AddPart("part4", "description of part4", 6, "vendor4")
    8.  
    9.         '-- sort parts by part number
    10.         Parts = SortPartsByPartNumber()
    11.  
    12.         '-- lets see whether they were really sorted
    13.         For Each part In Parts
    14.             Debug.WriteLine(part.PartNumber)
    15.         Next
    16.     End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." Charles F. Kettering

    Read articles on My Blog 101 LINQ Samples JSON Validator XML Schema Validator "How Do I" videos on MSDN VB.NET and C# Comparison Good Coding Practices VBForums Reputation Saver String Enum Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,567

    Re: Best way to store and arrange data

    Quote Originally Posted by Pradeep1210 View Post
    Dealing with multi-dimentional arrays (in fact, arrays in general) is a bit cumbersome in VB.NET. We have much easier ways here. Instead of arrays use List or Dictionary/HashTable etc. as the case may be.
    Use arrays where an array is appropriate. Use a particular type of collection when that's appropriate. A multi-dimensional array is the best choice when creating a matrix, which is not something that needs doing often. An simple array is the best choice when dealing with a fixed-length list. If you're not doing either of those things then a collection of some sort is the appropriate option.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Posts
    163

    Re: Best way to store and arrange data

    Thank Pradeep1210, that was a really easy to follow example. I shouldn't have a problem building off of that. I won't get to work on this for a bit as my main job isn't programming. So I might be back in after a few days if get stuck anywhere. I've never had any programming classes or training so I went with arrays since that was something I knew and classes were something I didn't know at all.

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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.