这个循环怎么了? [英] Whats wrong with this loop?
本文介绍了这个循环怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Public Class Form1
Dim str() As String
Dim x As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For x = 0 To 5
str(x) = " D"
Next
End Sub
End Class
我以前可能发誓要通过一个循环将字符串添加到数组中,而现在却出错了.
I could have sworn to adding strings to an array in the past through a loop worked and now it just errors out.
推荐答案
您尚未定义数组大小. br/> 您已声明一个未初始化的数组,从而导致错误.
使用
You have not defined the array size.
You have declared an array that is then not initialized thus causing errors.
Use
Dim str(4) As String
,您应该没事.
字符串是不可变的,请使用StringBuilder
从文档复制:
字符串是Unicode字符的顺序集合,通常用于表示文本,而String是表示字符串的System.Char对象的顺序集合. String的值是顺序集合的内容,并且值是不可变的.
字符串被称为不可变的,因为一旦创建它的值就无法对其进行修改.看似修改String的方法实际上返回一个包含修改的新String.如果有必要修改类似字符串的对象的实际内容,请使用System.Text.StringBuilder类.
问候
Espen Harlinn
String is immutable, use StringBuilder
Copied from documentation:
A string is a sequential collection of Unicode characters, typically used to represent text, while a String is a sequential collection of System.Char objects that represents a string. The value of the String is the content of the sequential collection, and the value is immutable.
A String is called immutable because its value cannot be modified once it has been created. Methods that appear to modify a String actually return a new String containing the modification. If it is necessary to modify the actual contents of a string-like object, use the System.Text.StringBuilder class.
Regards
Espen Harlinn
另一种方法是:
Another way to do is:
Dim MyList as new list(of string)
For X = 0 to 5
MyList.add " D"
Next
''Now you can just use MyList.ToArray()
祝你好运! :thumbsup:
埃里克.
Good luck! :thumbsup:
Erik.
这篇关于这个循环怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文