1

I am trying hard to understand arrays and read around the subject, but much of the literature is not easy to get your head around when you've only just started to program and there's no one you can ask to explain. This is my two dimensional array:

        'Declare 2-diensional array of Strings
    Dim cars(,) As String =
    New String(,) {{"BMW", "Coupe", "Reg:2015", "5 Door"},
           {"Ford", "Focus", "Reg:2015", "3 Door"},
           {"Land Rover", "Discovery", "Reg:2014", "5 Door"},
           {"Vauxhall", "Astra", "Reg:2014", "3 Door"},
           {"SEAT", "Ibiza", "Reg:2013", "5 Door"}}

    ' Get bounds of the array.
    Dim bound0 As Integer = cars.GetUpperBound(0)
    Dim bound1 As Integer = cars.GetUpperBound(1)

    ' Loop over all elements.
    For i As Integer = 0 To bound0
        For x As Integer = 0 To bound1
            ' Get element.
            Dim s1 As String = cars(i, x)
            Console.ForegroundColor = ConsoleColor.Green
            Console.Write(s1 & ", ")
        Next
        Console.WriteLine()
    Next
    Console.ReadKey()
    Console.WriteLine("Please enter the name of the record you wish to view")
    Dim s = Console.ReadLine()
    Dim value As String = Array.Find(cars, Function(x) (x.StartsWith(s)))
    Console.WriteLine(value)
    Console.ReadKey()

This is the line that is causing the problem

Dim value As String = Array.Find(cars, Function(x) (x.StartsWith(s)))

Visual Studio suggests that the error is because "Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error." I cant get my head around what this error means. Please can someone please explain it as though talking to a 10 year old Or perhaps a website that might help me understand this problem. Thanks

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
MissDizy
  • 115
  • 2
  • 10
  • my suspicion is that the find function is expecting a single dimension array. in other words it will not search the array of arrays you have there. instead of just cars in the find argument, pass in cars(0). If you are just beginning, I'd recommend to not use the extension methods until you understand how they work. – Jeremy Dec 08 '15 at 19:20
  • 1
    2D arrays do represent a problem for quite a few in-built functionalities (like some functions in Array). Even LINQ has problems with them. You should be using arrays only when required (i.e., when efficiency/memory issues are very important) and when the contained information is not expected to be modified in a relevant way. For other situations, you should better user more friendly-of-modifications collections (lists, dictionaries, etc.), where you can use LINQ methods unrestrictedly. Also relying on 2D collection should be avoided as much as possible. – varocarbas Dec 08 '15 at 19:22
  • 1
    A class such as `Car {Make, Model, Year, Color, Style}` would keep the data for one object altogether. Then a `List(Of Car)` would be easier to work with than arrays. Your time and brain cells would be better spent learning classes and lists which makes using and storing objects much easier – Ňɏssa Pøngjǣrdenlarp Dec 08 '15 at 19:22
  • There is nothing the OP wants to do that cant be done in VB – Ňɏssa Pøngjǣrdenlarp Dec 08 '15 at 19:23
  • What suggests Plutonix is what you should focus on rather than on old (and less friendly) programming approaches. In any case, bear in mind that arrays are very useful and the most efficient collections; so you might want to use them in certain situations (I do use them a lot). – varocarbas Dec 08 '15 at 19:37

1 Answers1

9

The key lies in the fact that the data is related. Rather than breaking up your "car" into pieces to store in different arrays, a Class would allow you to create a Car object, and store various cars in a typed List:

Five Minute Intro to Classes and Lists

Public Class Car
    Public Property Id As Int32
    Public Property Make As String
    Public Property Model As String
    Public Property Year As Int32
    '... etc
End Class

Now you have a container to save all the info for one car. This is like a blueprint for what a Car object will look like. A Class can also contain methods (Sub or Function) to manage the data they store so that everything relating to a Car or Employee or Order can be managed by that class.

Dim c As New Car          ' create a new car object
c.Make = "Mazda"
c.Model = "Miata"
c.Year = 2013

Or initialize when you declare it:

Dim c As New Car With {.Make = "Mazda", .Model = "Miata" ...}

Now, the New Millennium version of arrays, is a List. These are much easier to work with because they size themselves:

Dim Cars As New List(Of Car)

The Cars collection can only store car objects, each car it stores keeps the data together for each one. There are many other collection types such as Dictionary you will eventually want to get familiar with. Add the Mazda to the List:

' c is the car object created above
Cars.Add(c)

Unlike arrays there is no need to know how many cars you will be working with because they resize themselves. To reference one, Cars(n) will refer to a car object:

' n is the index of a car in the list
Dim str = Cars(n).Make & " is " & Cars(n).Color

Iterate the list, using a temp Car variable:

For Each c As Car In Cars   
    ' c will be Cars(0), Cars(1) etc as we step thru
    Console.WriteLine("Index {0} is a BEAUTIFUL {1} {2}",
           Cars.IndexOf(c), c.Year, c.Model)
    ' e.g
    ' "Index 4 is a BEAUTIFUL 2015 Camry"
Next

Find one or the first of a kind:

Dim myCar = Cars.FirstOrDefault(Function (f) f.Make = "Mazda" AndAlso f.Year = 2013)

A List(Of T) can be used as a DataSource for some controls:

myDGV.DataSource = Cars

The DataGridView will create a column for each Property in the Car class, and add a row for each car object in the list - simple!

Or:

myListBox.DataSource
myList.DisplayMember = "Make"
myList.ValueMember = "Id"

The user will see the Make in the ListBox (or whatever you define). SelectedValue will be the Id of the car object they selected and SelectedItem will be the entire car object. No need to go rifling thru different arrays to find related data - it is always together in one spot.

Community
  • 1
  • 1
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • Thank you so much for all the advice and information. I will look into all your suggestions seriously. There is much to contemplate here!! – MissDizy Dec 08 '15 at 19:46
  • An array of strings makes it easy to put one in the wrong slot. CLass properties make mistakes more obvious: `Make = "Blue"` is more easier to spot as wrong than `myArray(x, 6) = "Blue"`. Besides which a Class allows mixed types to be used: string, int, DateTime, etc etc – Ňɏssa Pøngjǣrdenlarp Dec 08 '15 at 19:49