如何在两组文本vb.net之间添加随机数

时间:2017-09-01 07:02:32

标签: .net vb.net text numbers

好吧所以我一直在尝试为C ++制作一个垃圾代码生成器(vb.net的新手只是试图熟悉不同的语言)而且我仍然能够在两行文本之间调用随机数字 目前我被困在

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles 
Button4.Click
Dim value As Integer = CInt(Int((10000 * Rnd()) + 9999999))
Me.RichTextBox1.Text = (
" float pJunkcode = " + value + ";" +
" if (pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
"If(pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" If (pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" If (pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" If (pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" If (pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";" +
" pJunkcode = " + value + ";")`

我得到的错误是

  

从字符串“pJunkcode”到“Double”类型的转换无效。

1 个答案:

答案 0 :(得分:3)

当您来自C语言时,很难注意到代码的问题。在VB中 +运算符具有不同的行为,具体取决于所涉及的表达类型。在这种情况下,一个是字符串,另一个是数字,因此+运算符正在尝试首先将String解析为Double,最后添加2个数字。您在此处获得了所有这些信息:+ Operator

  

一个表达式是数字数据类型,另一个是字符串:

     

如果Option Strict为On,则生成编译器错误。

     

如果Option Strict为Off,则隐式将String转换为Double并添加。

     

如果String无法转换为Double,则抛出InvalidCastException异常。

在您的情况下,您可以使用字符串连接运算符& Operator来解决问题:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles 
 Button4.Click
   Dim value As Integer = CInt(Int((10000 * Rnd()) + 9999999))
    Me.RichTextBox1.Text = (
    " float pJunkcode = " & value & ";" &
    " if (pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    "If(pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " If (pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " If (pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " If (pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " If (pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";" &
    " pJunkcode = " & value & ";")