Results 1 to 7 of 7

Thread: [RESOLVED] How to find last record in a two-dimensaional array

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    125

    Resolved [RESOLVED] How to find last record in a two-dimensaional array

    I have a three field array that starts empty but can have random additions. Problem is identifying/finding an index for the last element to adds a new one. TIA JP

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: How to find last record in a two-dimensaional array

    You can get the upper-bounds of the collection for both dimensions, which will give you the last item in the collection. Here is an example:
    Code:
    Dim uBoundsX As Integer = MyArray.GetUpperBound(0)
    Dim uBoundsY As Integer = MyArray.GetUpperBound(1)
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: How to find last record in a two-dimensaional array

    I'd suggest considering using a List(of T) containing some kind of object. If you are dynamically sizing an array, it won't be as efficient as a List, and VB doesn't handle multi dimensional arrays very well at all when it comes to dynamic resizing.
    My usual boring signature: Nothing

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to find last record in a two-dimensaional array

    2D arrays have their place but it's quite rare that they are the best choice and they certainly aren't in this case. It seems like you are misusing an array like many people do and using each "row" to represent a record and each column to represent a field of a record.

    If you want to do that in a generic way then you should use a DataTable. You can add DataColumns to it with names and data types and you can add DataRows to it efficiently, without having to copy the entire current contents as you would with an array.

    If you want to do it in a more specific way then you should first define a type, i.e. a class, that has three properties. You can then do as Shaggy said and create a List(Of T) where T is that type. The List(Of T) will handle growing and shrinking automatically and efficiently, compared to you doing it manually and inefficiently with an array.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    125

    Re: How to find last record in a two-dimensaional array

    Thanks to all for the kind suggestions. I am an old student, 2 chapters away from Tables (like I used in Access!?) and from what little I know of tables I could not agree more!
    Web Applications follows Arrays and Collections. I fear I will miss something if I skip past a chapter and subsequent problems utilize past subject matter.

    My question arose from the fact that someday I will learn how to make additions to arrays that are kept between sessions, hence my study problem of setting the index to -1
    and increment it as the first step of my Lookup would no longer work.

    On the List(of T) suggestion, I've been pondering that function for a long while but have not yet figured out how to code it.
    Code:
    Public Shared Function Find(Of T) (
    	array As T(),
    	match As Predicate(Of T)
            ) As T
    Thanks again! JP

  6. #6
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: How to find last record in a two-dimensaional array

    Functions like that aren't hard, but if you're working your way through a book you might not have seen them yet. I'm sort of surprised you haven't seen it sooner, but let's start at the first confusing things and work our way up pretty quick.

    The first weird thing is probably (Of T). That is called the "generic type definition". Generics are a special kind of class/method in .NET that take a special "type variable" so they can work on any type. So anything with "(Of T)" on the end is saying, "I do something on many types, and 'T' represents the type that will be used." This is easier by example.

    The first parameter is array as T(). That means it is a parameter named 'array' that has the type "array of T". Since T is a type parameter, we dont know quite what it will be until runtime. Let's ignore match for now, it's the next tricky thing.

    Let's say I have an Integer array:
    Code:
    Dim numbers() As Integer = { 1, 2, 3 }
    The right way to call Find() for that array is:
    Code:
    Array.Find(Of Integer)(numbers, ???)
    Do you see how I replaced 'T' with 'Integer'? That tells Find() that anywhere it expected 'T' as a type, it should expect 'Integer'. Since the first parameter is "array of T", for this call I need to provide "array of Integer". The array 'numbers' is an array of Integer, so it works.

    So List(Of T) is a generic type. 'T' represents the items it holds. Unlike an array, you can add and remove items from it easily. Here's one way I could remake the 'numbers' variable as a List(Of T):
    Code:
    Dim numbers As New List(Of Integer)()
    numbers.Add(1)
    numbers.Add(2)
    numbers.Add(3)
    Note again, I replaced 'T' with 'Integer', because I wanted a list that can hold Integers. The definition for Add() looks something like this:
    Code:
    Public Sub Add(ByVal newItem As T)
    Since 'T' is defined as 'Integer' for this type, that means Add() expects to take an Integer, too.

    Now, for the second tricky thing:
    Code:
    match As Predicate(Of T)
    You can make variables that hold Subs/Functions. We call variables that hold them "Delegates". You can make a "Delegate type" that describes what kind of Sub/Function the variable holds. Predicate(Of T) is defined this way:
    Code:
    Public Delegate Function Predicate(Of T)(ByVal T As Integer) As Boolean
    In English this says:
    The Delegate Type 'Predicate(Of T)' represents Functions that take a single parameter of type T and returns a Boolean value indicating if that parameter meets some condition.
    Basically that means any function that looks like this can "match" Predicate(Of T):
    Code:
    Function ReturnSomething(ByVal obj As ???) As Boolean
    If '???' is Integer, that's Predicate(Of Integer). We use Predicates to answer questions like, "Is this the object I'm looking for?"

    The only trick is VB wants you to use the AddressOf operator so it knows you mean "treat this name like a delegate" rather than "call this function". So let's put it all together.

    Remember 'numbers'? Suppose I'm curious if the number 2 is in the array. I could write:
    Code:
    Public Sub Example()
        Dim numbers() As Integer = { 1, 2, 3 }
        Dim result As Integer = Array.Find(Of Integer)(numbers, AddressOf IsTwo)
        If result = 2 Then
            MessageBox.Show("I found it!")
        End If
    End Sub
    
    Private Function IsTwo(ByVal number As Integer) As Boolean
        Return number = 2
    End Function
    There are syntax shortcuts to make this less verbose, but they take longer to explain. This example looks kind of goofy and raises questions like, 'What happens if I want to find 0, since 0 is what is returned if the object is NOT found.' The answer: Array.Find() isn't very useful for this kind of array, it's more useful for arrays of complex objects.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    125

    Re: How to find last record in a two-dimensaional array

    WOW! Thanks a lot, I will study your comments and try using them in my code. Thanks JP

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
  •  



Click Here to Expand Forum to Full Width