Click here to Skip to main content
15,904,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a function that returns a 2 dimensional array...
VB
Public Function Day1ClassBlocks(intSchedID As Integer) As String(,)
...

For Each row In dsResultsTable.Rows
            txtClassBlocks(i, 0) = row("day").ToString()
            txtClassBlocks(i, 1) = row("block").ToString()
            i = i + 1
        Next

        Return txtClassBlocks

This works fine. Then on the page load event I have this code to loop trough and work with it.
VB
<pre>Dim numclasses(,) As String
        Dim intNumOfBlocks As Integer
        Dim i, x As Integer


        numclasses = Day1ClassBlocks(1572)
        
        i = 0


        For each row in numclasses
            Response.Write(">" & numclasses(i, 0).ToString() & " - " & numclasses(i, 1).ToString() & "<br />")
        Next


But when I try to loop through the string array [numclasses] it returns 100 rows when it should only return 10. I can see that the length is 100 but only 10 rows have values. I am very very new to ASP.NET so I am sure I am missing something simple but I have searched and played around with this for 2 days. Also, this is just practice code.

Thanks
-Grajek

What I have tried:

I wrote another function that returns the number of rows and used
VB
For 1 = 0 to intRowCount - 1 Next
which works but I prefer to have just one function for this task.
Posted
Updated 1-Feb-17 10:36am

This has nothing to do with ASP.NET but everything to do with how you treat the returned array.

You enumerating over what you think is a collection of rows in a two dimensional array. That's not how these work.

If you use a ForEach over an array like this, there is no such thing as a row. Each and every element will be returned. So, if you're array is 10 "rows" by 10 "columns" (0 through 9), you will get back 100 "rows".

To treat the array as a 2-dimensional array, you have to use indexers into each dimension of the array:
For i = 0 to numClasses.GetUpperBound(0)
    For j = 0 to numClasses.GetUpperBound(1)
        ...
    Next
Next
 
Share this answer
 
Comments
Grajek 1-Feb-17 17:00pm    
Okay, thanks a WHOLE BUNCH. :-) [.GetUpperBound(0)] works perfectly. I knew it was something simple. I am wondering though how the GetUpperBound(0) filters out the "Nothing" "Nothing" elements I see in my watch window and the For Each does not.

Thanks Again For the response.

Grajek
Dave Kreskowiak 1-Feb-17 17:18pm    
Filtering? There is no filtering at all.

GetUpperBound only returns a number, the largest index for a dimension of the array. That's it, nothing else.
Grajek 1-Feb-17 17:37pm    
How does the [For Each] differ from the [For i = 0 to numClasses.GetUpperBound]? When I use the [For Each] it returns 100 elements regardless of values or no values, but the [For i=0 to numClasses.GetUpperBound(0)] only returns the elements with values other than "Nothing."
Dave Kreskowiak 1-Feb-17 19:14pm    
Indexers treat the array as an array and are limited to the bounds of the array in every dimension.

ForEach uses an Enumerator which it gets from the Array.GetEnumerator method on the array. It doesn't see it as an array. To the enumerator the array is just a collection, no dimensions.
Grajek 2-Feb-17 14:12pm    
Okay, that makes sense. Thanks!

Like I said very very new to .NET. This helps more than you know!

Thanks again.
By using the debugger, you may discover that the problem is not where you think it is.
I think the problem is in the code you didn't shown.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v2
Comments
Grajek 1-Feb-17 17:23pm    
Thanks for the reply, I really, really do appreciate it. I actually did use the debugger which led me to the right question to ask. The [Watch] listed the array contents like:
{0}{0}"1", "1"
{0}{1}"1", "3"
{0}{2}"Nothing","Nothing"
{0}{3}"Nothing","Nothing"
{0}{4}...

{1}{0}"1", "1"
{1}{1}"1", "3"
{1}{2}"Nothing","Nothing"
{1}{3}"Nothing","Nothing"
...

Thanks Again
Grajek

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900