循环字符串数组 [英] Looping for string array

查看:154
本文介绍了循环字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个使用For ... Next循环的小函数。我试图将字符串数组中的循环结果放入

。我的代码如下:


Dim i as Integer

Dim str()as String

i = 0 to products .Items(0).Item.Length - 1

str()= New String(){products.Items(0).Item(i).Name}

接下来


我正在尝试用循环做这个:


Dim str()as String = New String() {products.Items(0).Item(0).Name,

products.Items(0).Item(1).Name,etc.}

我无法让For ... Next循环正常工作的原因。什么是

我做错了?任何帮助将不胜感激。


谢谢,

Roshawn

Hi,

I have a small function that uses a For...Next loop. I am trying to place
the results of the loop in a string array. My code is as follows:

Dim i as Integer
Dim str() as String
For i = 0 to products.Items(0).Item.Length - 1
str() = New String() {products.Items(0).Item(i).Name}
Next

What I''m trying to do with the loop is this:

Dim str() as String = New String() {products.Items(0).Item(0).Name,
products.Items(0).Item(1).Name, etc.}
For some reason I cannot get the For...Next loop to work correctly. What am
I doing wrong? Any help will be appreciated.

Thanks,
Roshawn

推荐答案

2004年8月24日星期二22:38:41 -0500,Roshawn写道:
On Tue, 24 Aug 2004 22:38:41 -0500, Roshawn wrote:


我有一个小功能,使用For .. 。下一个循环。我试图将循环的结果放在一个字符串数组中。我的代码如下:

Dim i as Integer
Dim str()as String
对于i = 0到products.Items(0).Item.Length - 1
str()= New String(){products.Items(0).Item(i).Name}
下一页

我想用循环做什么是这样的:

Dim str()as String = New String(){products.Items(0).Item(0).Name,
products.Items(0).Item( 1).Name等。}由于某种原因,我无法让For ... Next循环正常工作。我做错了什么?任何帮助将不胜感激。

谢谢,
Roshawn
Hi,

I have a small function that uses a For...Next loop. I am trying to place
the results of the loop in a string array. My code is as follows:

Dim i as Integer
Dim str() as String
For i = 0 to products.Items(0).Item.Length - 1
str() = New String() {products.Items(0).Item(i).Name}
Next

What I''m trying to do with the loop is this:

Dim str() as String = New String() {products.Items(0).Item(0).Name,
products.Items(0).Item(1).Name, etc.}
For some reason I cannot get the For...Next loop to work correctly. What am
I doing wrong? Any help will be appreciated.

Thanks,
Roshawn




您的数组是一个空引用(它不指向任何事情)。你需要

需要使用边界声明你的数组或使用ReDim / ReDim Preserve来动态扩大数组。虽然,我相信你可能会发现

System.Collections.Specialized.StringCollection更简单...


Imports System.Collections.Specialized


....

Dim strings As New StringCollection()


For i As Integer = 0 To products.Items( 0).Item.Length - 1

strings.Add(products.Items(0).Item(0).Name)

Next i

-

Tom Shelton [MVP]



Your array is a null reference (it doesn''t point to anything). You would
need to declare your array with bounds or use ReDim/ReDim Preserve to
enlarge the array dynamically. Though, I believe you might find
System.Collections.Specialized.StringCollection to be simpler...

Imports System.Collections.Specialized

....
Dim strings As New StringCollection()

For i As Integer = 0 To products.Items(0).Item.Length - 1
strings.Add(products.Items(0).Item(0).Name)
Next i
--
Tom Shelton [MVP]


Tom ,.


你是怎么做的?找到这个,我从来没有看过它?

在我看来可能有用而不是arraylist因为

可能你不需要拆箱。


Cor
Tom,.

How did you find this one, I never saw it?
Can be usefull in my opinion instead of the arraylist for this because
probably you do not have to unbox.

Cor


Roshawn,


作为汤姆很好的答案的另一种选择,因为你事先知道你阵列的

长度。


Dim i as Integer

Dim str(products.Items(0) .Item.Length)as String

i = 0 to products.Items(0).Item.Leng th - 1

str()= New String(){products.Items(0).Item(i).Name}

Next

Cor

Roshawn,

As an alternative for Tom''s nice answer because you know in advance the
length of your array.

Dim i as Integer
Dim str(products.Items(0).Item.Length) as String
For i = 0 to products.Items(0).Item.Length - 1
str() = New String() {products.Items(0).Item(i).Name}
Next

Cor


这篇关于循环字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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