[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.
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.
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
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!
Quote:
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
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:
Public Class Part
Public PartNumber As String
Public Description As String
Public Vendor As String
Public Quantity As Integer
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:
Dim Parts As New List(Of Part)
Private Sub AddPart(ByVal partNumber As String, ByVal description As String, ByVal quantity As Integer, ByVal vendor As String)
Dim newPart As New Part With {.PartNumber = partNumber, .Description = description, .Quantity = quantity, .Vendor = vendor}
Parts.Add(newPart)
End Sub
Private Function FindPart(ByVal partNumber As String) As Part
Return Parts.Where(Function(f) f.PartNumber = partNumber).FirstOrDefault
End Function
Private Sub RemovePart(ByVal partNumber As String)
Dim partToRemove As Part = FindPart(partNumber)
If partToRemove IsNot Nothing Then Parts.Remove(partToRemove)
End Sub
Private Function SortPartsByPartNumber() As List(Of Part)
Return Parts.OrderBy(Function(f) f.PartNumber).ToList
End Function
Now its so easy to handle our parts collection.
e.g.
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'-- add some parts
AddPart("part1", "description of part1", 10, "vendor1")
AddPart("part3", "description of part3", 20, "vendor3")
AddPart("part2", "description of part2", 15, "vendor2")
AddPart("part7", "description of part7", 11, "vendor7")
AddPart("part4", "description of part4", 6, "vendor4")
'-- sort parts by part number
Parts = SortPartsByPartNumber()
'-- lets see whether they were really sorted
For Each part In Parts
Debug.WriteLine(part.PartNumber)
Next
End Sub
Re: Best way to store and arrange data
Quote:
Originally Posted by
Pradeep1210
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.
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.