string() 类型的值不能转换为字符串 [英] Value of type string() cannot be converted to string
问题描述
我一直收到这个错误,我尽我所能,但它仍然说String()的值类型不能转换为字符串."
I keep getting this error, I tried all I could but it still says "Value type of String() cannot be converted to string."
代码如下:
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Sub New()
InitializeComponent()
RAN = New Random
WB = New WebClient
End Sub
Private Const IDNum As String = "https://example.com/Data.php"
Private WB As WebClient
Private RAN As Random
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Account As String() = WB.DownloadString(IDNum).Split(Environment.NewLine)
AccSplit(Account(RAN.Next(1, Account.Length)))
End Sub
Private Sub AccSplit(ByVal Account As String)
TextBox2.Text = Account.Split()
End Sub
推荐答案
当你在这里调用 Split
时:
When you call Split
here:
TextBox2.Text = Account.Split()
您正在返回一个 String
数组.不带参数调用 Split
将在空白字符上拆分 String
.例如,这个:
You are getting a String
array back. Calling Split
with no arguments will split the String
on whitespace characters. For instance, this:
Dim arr = "Hello World".Split()
相当于:
Dim arr = {"Hello", "World"}
TextBox
的 Text
属性是 String
类型,所以你不能分配一个 String
数组到它.那没有意义.如果你想煎鸡蛋,你把鸡蛋盒放在锅里吗?正确的行动方针取决于您实际想要达到的目标.如果您只希望 String
显示在 TextBox
中,请执行以下操作:
The Text
property of a TextBox
is type String
, so you can't assign a String
array to it. That doesn't make sense. If you want to fry an egg, do you put am egg carton in the pan? The correct course of action depends on what you're actually trying to achieve. If you just want that String
displayed in the TextBox
then do this:
TextBox2.Text = Account
你也可以这样做:
TextBox2.Lines = Account.Split()
显示带有 TexTbox
中不同行元素的数组,假设您已将其 Multiline
属性设置为 True
.
to display the array with the elements on separate lines in the TexTbox
, which assumes that you have set its Multiline
property to True
.
这篇关于string() 类型的值不能转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!