locked
array of type point

    Question

  • hi all

    i am a beginner to vb

    i am having a problem with handling the dynamic 1d array of points

    i am getting a index out of range error
     
    can anybody help me to solve this problem in vb 2005

     
    • Edited by saumya_kuanr Thursday, December 03, 2009 5:57 AM
    Wednesday, December 02, 2009 2:18 PM

Answers

  • once you initialize an array points1(k) = Dot_point  you make the size of points(1) or points(0) and points(1) . Next time through the loop you try to set points(2) since you added 1 to k . You can't re size the array like that after you give it a value you have to use Redim preserve points(k) first to re size points to points(2) . A list is easier as Cor said or an arraylist which can increase it's size dynamically .
    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
    Thursday, December 03, 2009 8:47 AM
  • FYI....you can redimension the array on the fly, and actually...at the end of the day, that is what is happening "behind the scenes" with any sort of list, or other collection type variable. I think that the way that you are attempting this makes sense for the data you are dealing with...its not complex data, and it is not a collection of classes, so...an array of structures is okay. When you get a large number of them, however...redimensioning with preserve is costly...it is best to find out how many you want first, and redimension the array once without preserving it...then stuff the data in the array afterwards. This will be the fastest way, I think.

    At the point of the error is where you can try this in your code. [However, start k at 0, and not 1. When you dimension an array, the number in the parenthesis is the LAST index, not the count.]

    ReDim Preserve points1(k)

    Thursday, December 03, 2009 5:42 PM

All replies

  • I think many will,

    But try a List(of Point) much easier
    Success
    Cor
    Wednesday, December 02, 2009 5:41 PM
  • "i am getting a index out of range error "

    Then you are trying to access an element of the array which does not exist...

    But without any idea of the code you are using, it is impossible to say what you've done wrong.  Always inlcude a short sample of the code which is giving you an exception, indicate the line of code which raises the exception, and include the exception details.
    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    Wednesday, December 02, 2009 8:58 PM
    Moderator
  • This is probably the most common beginner error, in my experience.

    Array indices start at 0...not 1. (Which also means that the last index is the array's length, or count....minus 1.)

    Wednesday, December 02, 2009 9:52 PM
  • thanks for your reply
    <!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} -->

     '''''graph plotting part
            Dim Dot_point As New Point(LeftMargin, TopMargin)
            Dim points1() As Point
            points1 = New Point() {}

            Dim k As Integer = 1
            'for iner curve
            For i As Integer = 0 To 4
                For j As Integer = 0 To 9
                    If matrix(i, j) = 4 Then
                        ' points1(k) = New Point(Dot_point.X, Dot_point.Y)
                        points1(k) = Dot_point   ''AT this point i am getting that error


                        k += 1

                    End If

                    Dot_point.X += HorzGap

                Next

                Dot_point.X = LeftMargin
                Dot_point.Y += VertGap
            Next
            e.Graphics.FillClosedCurve(Brushes.Cyan, points1)

    Thursday, December 03, 2009 6:17 AM
  • once you initialize an array points1(k) = Dot_point  you make the size of points(1) or points(0) and points(1) . Next time through the loop you try to set points(2) since you added 1 to k . You can't re size the array like that after you give it a value you have to use Redim preserve points(k) first to re size points to points(2) . A list is easier as Cor said or an arraylist which can increase it's size dynamically .
    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
    Thursday, December 03, 2009 8:47 AM
  • Quiet obious the array is empty 
    points1 = New Point() {}

    If you had followed my advice you could have done

    Dim kpoints as new List(of Point)
    kpoints.Add(new point(Dot_point.X,Dot_point.Y))

    For the rest you can handle it as a simple array.



    Success
    Cor
    Thursday, December 03, 2009 11:49 AM
  • FYI....you can redimension the array on the fly, and actually...at the end of the day, that is what is happening "behind the scenes" with any sort of list, or other collection type variable. I think that the way that you are attempting this makes sense for the data you are dealing with...its not complex data, and it is not a collection of classes, so...an array of structures is okay. When you get a large number of them, however...redimensioning with preserve is costly...it is best to find out how many you want first, and redimension the array once without preserving it...then stuff the data in the array afterwards. This will be the fastest way, I think.

    At the point of the error is where you can try this in your code. [However, start k at 0, and not 1. When you dimension an array, the number in the parenthesis is the LAST index, not the count.]

    ReDim Preserve points1(k)

    Thursday, December 03, 2009 5:42 PM