Solved

GetUpperBound and GetLowerbound

Posted on 2007-04-08
6
517 Views
Last Modified: 2008-02-01
I need help understanding  GetUpperBound Method and GetLowerbound Method. Could you provide a simple sample on both functions?
0
Comment
Question by:VBdotnet2005
  • 3
  • 3
6 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18874102
You pass the dimension of the array and the GetUpperBound() method returns the upper bound of that dimension.  The first dimension starts at 0 (zero).  The Array.Rank() method will tell you how many dimensions an array has.

GetLowerBound() always returns 0 (zero) assuming you have passed in a valid dimension for that array.

Here are some examples:

        Dim arr1(9) As Integer
        Debug.Print(arr1.Rank) ' 1
        Debug.Print(arr1.GetUpperBound(0)) ' 9

        Dim arr2(4, 5) As Integer
        Debug.Print(arr2.Rank) ' 2
        Debug.Print(arr2.GetUpperBound(0)) ' 4
        Debug.Print(arr2.GetUpperBound(1)) ' 5

        Dim arr3(7, 2, 6) As Integer
        Debug.Print(arr3.Rank) ' 3
        Debug.Print(arr3.GetUpperBound(0)) ' 7
        Debug.Print(arr3.GetUpperBound(1)) ' 2
        Debug.Print(arr3.GetUpperBound(2)) ' 6
0
 

Author Comment

by:VBdotnet2005
ID: 18880317
It looks like get the highest for GetUpperBound and get the lowest for GetLowerbound in array. Am I on the right track?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18880342
Yes...where "highest" means get the greatest index number or slot number for the specified dimension.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:VBdotnet2005
ID: 18880398
Idle_Mind,

cool. I got that down. One thing I don't understand is Ok we know what the greatest index or lowerest index, knowing just what the index is how is that helping us?  Could you give me what we do exactly in the "real world"?
sample

       Dim arr1() As String = {"a", "b", "c"}
       
        For Each mystring As String In arr1

            MessageBox.Show(mystring) ' just for the hack of it


        Next

        MessageBox.Show(arr1.GetUpperBound(0)) '2  a is 0 b is 1 c is 2
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18891956
"how is that helping us?  Could you give me what we do exactly in the "real world"?"

Lol...a common usage is to setup a For...Next loop to iterate over the elements of an array:

        Dim arr1() As String = {"a", "b", "c"}
        Debug.Print("arr1")
        For i As Integer = 0 To arr1.GetUpperBound(0)
            Debug.Print("Index " & i & " = " & arr1(i))
        Next

        Dim arr2(,) As String = {{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}, {"j", "k", "l"}}
        Debug.Print("arr2")
        For i As Integer = 0 To arr2.GetUpperBound(0)
            For j As Integer = 0 To arr2.GetUpperBound(1)
                Debug.Print("(" & i & "," & j & ") = " & arr2(i, j))
            Next
        Next
0
 

Author Comment

by:VBdotnet2005
ID: 18895606
Idle_Mind:

Thank you again & again :)

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.net and sql server 4 45
VB.NET 2008 - Windows Protected Your PC 7 41
vb.net winforms sizing/resolution? 4 42
VB.NET Textbox input validation 4 37
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question