value of type 'string' cannot be converted to '1-dimensional array of object'

时间:2015-10-29 15:57:29

标签: arrays vb.net string

I've looked everywhere and I can't find help for my problems, its multiple errors.

Ok so what I'm trying to do is take 2 strings "FirstNameList" and "LastNameList" and put them together to make 1 word, a full name.

Public Class HorizonNameGenerator

    Dim UsableChars() As String
    Dim RandomClass() As Random
    Dim FirstNameList() = "Noah, Liam, Mason, Zack, James, Jack, Derick, Owen, Blain, Borris, Johnny, Joe, Joseph, Terry, William, Rory, John, Jason, Daryl, Alex, Josh, Wu, Gareth, Carl, Martin, Marty, Vincent, Vince, Vinny, Bobby, Bob, Paris, Jake, Gage, Jacob, Ethan, Simon, Elijah, Michael, Daniel, Thomas, Tom, Matthew, Declan, Logan, Lucas, Benjamain, Aiden, David, Andrew, Andy, Sam, Samuel, Carter, Henry, Nathan, Ed, Eddy"
    Dim LastNameList() = "Smith, Johnson, Williams, Jones, Brown, Davis, Miller, Wilson, Moore, Taylor, Anderson, Thomas, Jackson, White, Maris, Martin, Thompson, Garcia, Martinez, Robinson, Clark, Rodriguez, Lewis, Lee, Walker, Hall, Allen, Young, Hernandez, King, Wright, Lopez, Hill, Scott, Greens, Adams, Baker, Gonzalez, Nelson, Carter, Mitchell, Perez, Roberts, Turner, Phillips, Campbell, Parker, Evans, Edwards, Collins, Stewert"
    Dim FinalName() = ""
    Dim Number As Integer = 0

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        UsableChars = Split(FirstNameList, ",")
        UsableChars = Split(LastNameList, ",")
        FinalName = ""
        Number = 0
        Number = RandomClass.Next(0, 59)
        FinalName = FinalName + UsableChars
    End Sub

End Class

However at bit "Noah, Liam, ect..." it says

value of type 'string' cannot be converted to '1-dimensional array of object'

and same with the "Smith, Johnson, etc.."

also at the Dim FinaleName() = "" it say is under the

" "

At the

UsableChars = Split(FirstNameList, ",")
UsableChars = Split(LastNameList, ",")

it says this

value of type 'string' cannot be converted to '1-dimensional array of String'

under the "FirstNameList" and "LastNameList"

ALSO, under the

RandomClass.Next(0, 59)

it says

Next is not a member of 'System.Aray'

and finally under "FinalName + UsableChars"

it says

Operator of "+" is not defined for types '1-dimensional array of object' and '1-dimensional array of String'

Can someone help me with this, I am new to this.

5 个答案:

答案 0 :(得分:1)

你最好编写数组而不是使用split,这样就可以得到类似的结果。

Dim FirstNames() As String = {"Noah", "Liam"}
Dim LastNames() As String = {"Bloggs", "Smith"}

然后你可以使用随机数生成器来获得与此类似的姓氏名字。

Dim MyRandom As New Random

Dim FirstNameIndex As Integer = MyRandom.Next(0, FirstNames.Count)
Dim LastNameIndex As Integer = MyRandom.Next(0, LastNames.Count)

然后结合两者。

Dim FullName As String = FirstNames(FirstNameIndex) & " " & LastNames(LastNameIndex)

希望这是你所追求的。 :)

答案 1 :(得分:1)

您将FirstNameList和LastNameList定义为对象数组。它们应该只是字符串,因为你在按钮点击处理程序中将它们分开。

Dim FirstNameList As String = "Noah, Liam, Mason, Zack, James, Jack, Derick, Owen, Blain, Borris, Johnny, Joe, Joseph, Terry, William, Rory, John, Jason, Daryl, Alex, Josh, Wu, Gareth, Carl, Martin, Marty, Vincent, Vince, Vinny, Bobby, Bob, Paris, Jake, Gage, Jacob, Ethan, Simon, Elijah, Michael, Daniel, Thomas, Tom, Matthew, Declan, Logan, Lucas, Benjamain, Aiden, David, Andrew, Andy, Sam, Samuel, Carter, Henry, Nathan, Ed, Eddy"
Dim LastNameList As String = "Smith, Johnson, Williams, Jones, Brown, Davis, Miller, Wilson, Moore, Taylor, Anderson, Thomas, Jackson, White, Maris, Martin, Thompson, Garcia, Martinez, Robinson, Clark, Rodriguez, Lewis, Lee, Walker, Hall, Allen, Young, Hernandez, King, Wright, Lopez, Hill, Scott, Greens, Adams, Baker, Gonzalez, Nelson, Carter, Mitchell, Perez, Roberts, Turner, Phillips, Campbell, Parker, Evans, Edwards, Collins, Stewert"

您还可以在按钮单击处理程序中连续两次使用UsableChars。您将需要使用不同的字符串数组,以便可以连接名字和姓氏。

或者,您可以将数组初始化为:

Dim FirstNameList() As String = {"Noah", " Liam", " Mason", " Zack", " James", " Jack", " Derick", " Owen", " Blain", " Borris", " Johnny", " Joe", " Joseph", " Terry", " William", " Rory", " John", " Jason", " Daryl", " Alex", " Josh", " Wu", " Gareth", " Carl", " Martin", " Marty", " Vincent", " Vince", " Vinny", " Bobby", " Bob", " Paris", " Jake", " Gage", " Jacob", " Ethan", " Simon", " Elijah", " Michael", " Daniel", " Thomas", " Tom", " Matthew", " Declan", " Logan", " Lucas", " Benjamain", " Aiden", " David", " Andrew", " Andy", " Sam", " Samuel", " Carter", " Henry", " Nathan", " Ed", " Eddy"}
Dim LastNameList() As String = {"Smith", " Johnson", " Williams", " Jones", " Brown", " Davis", " Miller", " Wilson", " Moore", " Taylor", " Anderson", " Thomas", " Jackson", " White", " Maris", " Martin", " Thompson", " Garcia", " Martinez", " Robinson", " Clark", " Rodriguez", " Lewis", " Lee", " Walker", " Hall", " Allen", " Young", " Hernandez", " King", " Wright", " Lopez", " Hill", " Scott", " Greens", " Adams", " Baker", " Gonzalez", " Nelson", " Carter", " Mitchell", " Perez", " Roberts", " Turner", " Phillips", " Campbell", " Parker", " Evans", " Edwards", " Collins", " Stewert"}

并避免必须执行拆分功能。

答案 2 :(得分:0)

对于随机错误...

变化:

Dim RandomClass() As Random

要:

Dim RandomClass As New Random

当您处理的是单个Random实例时,不是随机数组。

答案 3 :(得分:0)

Dim UsableChars() As String
Dim MyRandom As New Random
Dim FirstNameList() As String = {"Noah, Liam, Mason, Zack, James, Jack, Derick, Owen, Blain, Borris, Johnny, Joe, Joseph, Terry, William, Rory, John, Jason, Daryl, Alex, Josh, Wu, Gareth, Carl, Martin, Marty, Vincent, Vince, Vinny, Bobby, Bob, Paris, Jake, Gage, Jacob, Ethan, Simon, Elijah, Michael, Daniel, Thomas, Tom, Matthew, Declan, Logan, Lucas, Benjamain, Aiden, David, Andrew, Andy, Sam, Samuel, Carter, Henry, Nathan, Ed, Eddy"}
Dim LastNameList() As String = {"Smith, Johnson, Williams, Jones, Brown, Davis, Miller, Wilson, Moore, Taylor, Anderson, Thomas, Jackson, White, Maris, Martin, Thompson, Garcia, Martinez, Robinson, Clark, Rodriguez, Lewis, Lee, Walker, Hall, Allen, Young, Hernandez, King, Wright, Lopez, Hill, Scott, Greens, Adams, Baker, Gonzalez, Nelson, Carter, Mitchell, Perez, Roberts, Turner, Phillips, Campbell, Parker, Evans, Edwards, Collins, Stewert"}
Dim FinalName() As String = {""}
Dim Number As Integer = 0
Dim FirstNameIndex As Integer = MyRandom.Next(0, FirstNameList.Count - 1)
Dim LastNameIndex As Integer = MyRandom.Next(0, LastNameList.Count - 1)
Dim FullName As String = FirstNameList(FirstNameIndex) & " " & LastNameList(LastNameIndex)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox1.Text = FullName
End Sub

现在这是我的代码,它有点工作,我希望能够从" FirstNameList"中获取1个名字。和#34; LastNameList"将它放在一起并输出到" TextBox1"我怎么做。

到目前为止,它只是写出LastNameList和FirstnameList

中的所有名称

答案 4 :(得分:0)

请参阅我之前的评论,但这就是您想要的。

Dim MyRandom As New Random
Dim FirstNameList() As String = {"Noah", "Liam", "Mason", "Zack", "James", "Jack", "Derick", "Owen", "Blain", "Borris", "Johnny", "Joe", "Joseph", "Terry", "William", "Rory", "John", "Jason", "Daryl", "Alex", "Josh", "Wu", "Gareth", "Carl", "Martin", "Marty", "Vincent", "Vince", "Vinny", "Bobby", "Bob", "Paris", "Jake", "Gage", "Jacob", "Ethan", "Simon", "Elijah", "Michael", "Daniel", "Thomas", "Tom", "Matthew", "Declan", "Logan", "Lucas", "Benjamain", "Aiden", "David", "Andrew", "Andy", "Sam", "Samuel", "Carter", "Henry", "Nathan", "Ed", "Eddy"}
Dim LastNameList() As String = {"Smith", "Johnson", "Williams", "Jones", "Brown", "Davis", "Miller", "Wilson", "Moore", "Taylor", "Anderson", "Thomas", "Jackson", "White", "Maris", "Martin", "Thompson", "Garcia", "Martinez", "Robinson", "Clark", "Rodriguez", "Lewis", "Lee", "Walker", "Hall", "Allen", "Young", "Hernandez", "King", "Wright", "Lopez", "Hill", "Scott", "Greens", "Adams", "Baker", "Gonzalez", "Nelson", "Carter", "Mitchell", "Perez", "Roberts", "Turner", "Phillips", "Campbell", "Parker", "Evans", "Edwards", "Collins", "Stewert"}
Dim FinalName() As String = {""}
Dim Number As Integer = 0
Dim FirstNameIndex As Integer = MyRandom.Next(0, FirstNameList.Count)
Dim LastNameIndex As Integer = MyRandom.Next(0, LastNameList.Count)
Dim FullName As String = FirstNameList(FirstNameIndex) & " " & LastNameList(LastNameIndex)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox1.Text = FullName
End Sub

如果您想在每个按钮上点击一个新名称,您只需将某些事件移动到按钮点击事件即可获得此

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FirstNameIndex As Integer = MyRandom.Next(0, FirstNameList.Count)
Dim LastNameIndex As Integer = MyRandom.Next(0, LastNameList.Count)
Dim FullName As String = FirstNameList(FirstNameIndex) & " " & LastNameList(LastNameIndex)
    TextBox1.Text = FullName
End Sub