开发者

How to initialize a multidimensional array in VB.NET

开发者 https://www.devze.com 2023-03-20 01:08 出处:网络
I am trying to initialize a multidimensional array.Here is my syntax; this does not cre开发者_如何学Cate errors but it does not store all values either.Although it correctly prints out all records in

I am trying to initialize a multidimensional array. Here is my syntax; this does not cre开发者_如何学Cate errors but it does not store all values either. Although it correctly prints out all records in this snippet,

        dFirstWeek = CDate(FirstWeek)
        dFirstWeek = DateAdd(DateInterval.WeekOfYear, -1, dFirstWeek)

        Dim dFirstDay As Date
        Dim arrWeekYear(5000, 1) As Date
        Dim i As Integer = 0
        Dim j As Integer = 0
        dFirstDay = dFirstDay.AddDays(1)

        While dFirstWeek <= dLastWeek
            dFirstDay = dFirstWeek
            dFirstWeek = dFirstWeek.AddDays(7)

            While dFirstDay < dFirstWeek
                arrWeekYear(i, j) = (dFirstWeek)
                arrWeekYear(i, j + 1) = (dFirstDay)

                Response.Write(arrWeekYear(i, j).ToString("d"))
                Response.Write("           ;")
                Response.Write(arrWeekYear(i, j + 1).ToString("d"))
                Response.Write("<br>")
                dFirstDay = dFirstDay.AddDays(1)
                j = 0
            End While
            i = i + 1
        End While

later in this code, I try to reprint this array as follows:

            i = 0
            j = 0
            Dim k As Integer = 0
            'Response.Write(arrWeekYear.GetLength(0))

            While k < arrWeekYear.GetLength(0) - 2
                Response.Write(arrWeekYear(i, j).ToString("d"))
                Response.Write("           ;")
                Response.Write(arrWeekYear(i, j + 1).ToString("d"))
                Response.Write("<br>")
                j = 0
                i = i + 1
                k = k + 1
            End While

but this time, only one "j" record appears per "i" record. Why is this? And then after these few records, many dates resembling null dates appear: "1/1/0001 "

So why do all records appear in upper section, but not from lower? Did I insert values wrongly into this array? And it does not have to have fixed number of rows, just a fixed number of columns.


Dim arrWeekYear(5000, 1) As Date

That’s a multi-dimensional array all right, but the second dimension only has a size of 2! And you access it via j, which is always 0 in your code. That makes no sense. Is that really what you want? I suggest you use a data structure inside your array instead of a multi-dimensional array.

In fact, you almost never want to use a multi-dimensional array. At all. Complex objects are almost always more appropriate. The only real exception is when you actually want to store a mathematical matrix.


Increment i inside the inner loop, rather than inside the outer loop.

And even simpler code (requires visual studio 2010):

Dim baseDate As Datetime = Convert.ToDatetime(FirstWeek).AddDays(-7)

For Each item In Enumerable.Range(0, (dLastWeek - baseDate).TotalDays / 7) _
      .Select(Function(i) New DateTime() _
           {baseDate.AddDays(i*7), baseDate.AddDays(i*7 + 7) })

    Response.Write(string.Format("{0:d}      ;{1:d}<br/>", item(0), item(1)))
Next item

And since Response.Write() is frowned upon in asp.net, you can take this a step further and assign the Enumerable.Range() call as the datasource for an asp:repeater control, if you're using webforms rather than mvc.


In the first piece you fill the array writing 7 times a value in the same array line. In a test (from 01/01/2011 to 03/03/2011) the first 21 assignments are:

i | j | value | j+1 | value

0 | 0 | 01/01/2011 | 1 | 25/12/2010

0 | 0 | 01/01/2011 | 1 | 26/12/2010

0 | 0 | 01/01/2011 | 1 | 27/12/2010

0 | 0 | 01/01/2011 | 1 | 28/12/2010

0 | 0 | 01/01/2011 | 1 | 29/12/2010

0 | 0 | 01/01/2011 | 1 | 30/12/2010

0 | 0 | 01/01/2011 | 1 | 31/12/2010 final value in array (0,0) and (0,1)

1 | 0 | 08/01/2011 | 1 | 01/01/2011

1 | 0 | 08/01/2011 | 1 | 02/01/2011

1 | 0 | 08/01/2011 | 1 | 03/01/2011

1 | 0 | 08/01/2011 | 1 | 04/01/2011

1 | 0 | 08/01/2011 | 1 | 05/01/2011

1 | 0 | 08/01/2011 | 1 | 06/01/2011

1 | 0 | 08/01/2011 | 1 | 07/01/2011 final value in array (1,0) and 1,1)

2 | 0 | 15/01/2011 | 1 | 08/01/2011

2 | 0 | 15/01/2011 | 1 | 09/01/2011

2 | 0 | 15/01/2011 | 1 | 10/01/2011

2 | 0 | 15/01/2011 | 1 | 11/01/2011

2 | 0 | 15/01/2011 | 1 | 12/01/2011

2 | 0 | 15/01/2011 | 1 | 13/01/2011

2 | 0 | 15/01/2011 | 1 | 14/01/2011 final value in array (2,0) and (2,1)

etc.

In the 2nd part you get the values written : i | j | value | j+1 | value

0 | 0 | 01/01/2011 | 1 | 31/12/2010

1 | 0 | 08/01/2011 | 1 | 07/01/2011

2 | 0 | 15/01/2011 | 1 | 14/01/2011

3 | 0 | 22/01/2011 | 1 | 21/01/2011

4 | 0 | 29/01/2011 | 1 | 28/01/2011

5 | 0 | 05/02/2011 | 1 | 04/02/2011

6 | 0 | 12/02/2011 | 1 | 11/02/2011

7 | 0 | 19/02/2011 | 1 | 18/02/2011

8 | 0 | 26/02/2011 | 1 | 25/02/2011

9 | 0 | 05/03/2011 | 1 | 04/03/2011

10 | 0 | 01/01/0001 | 1 | 01/01/0001 and this is the value in the rest of the array.

I think you forgot to increment some value in part 1?!?

0

精彩评论

暂无评论...
验证码 换一张
取 消