如何从函数返回数组... [英] How to return an array from a function...

查看:60
本文介绍了如何从函数返回数组...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个返回二维数组的函数...

I have created a function that returns a 2 dimensional array...

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.

<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





但是当我尝试循环遍历字符串数组[numclasses]时它只返回100行返回10.我可以看到长度为100但只有10行有值。我是非常非常新的ASP.NET,所以我确信我错过了一些简单的东西,但我已经搜索并玩了2天。此外,这只是练习代码。



谢谢

-Grajek



我尝试过什么:



我写了另一个函数,它返回行数并用



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

For 1 = 0 to intRowCount - 1 Next

哪个有效,但我更喜欢只有一个此任务的函数。

which works but I prefer to have just one function for this task.

推荐答案

这与ASP.NET无关,而是与处理返回数组的方式有关。



您可以枚举您认为二维数组中的行集合。这不是这些工作的方式。



如果你在这样的数组上使用ForEach,那就没有行了。将返回每个元素。所以,如果你的数组是10行10列(0到9),你将得到100行。



待如果数组为二维数组,则必须在数组的每个维度中使用索引器:

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


通过使用调试器,你可能会发现问题不在你认为的地方。

我认为问题在于你没有显示的代码。



当你不理解你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器查看你的代码是在做。它允许你逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
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.


这篇关于如何从函数返回数组...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆