Solved

GetUpperBound and GetLowerbound

Posted on 2007-04-08
6
507 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now